@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace App\Benzina;
|
||||
|
||||
use App\Entity\Money;
|
||||
use App\Entity\Project\BudgetItem;
|
||||
use App\Entity\Project\BudgetItemType;
|
||||
use App\Entity\Project\Project;
|
||||
use App\Repository\Project\ProjectRepository;
|
||||
use Goteo\Benzina\Pump\AbstractPump;
|
||||
use Goteo\Benzina\Pump\ArrayPumpTrait;
|
||||
use Goteo\Benzina\Pump\DoctrinePumpTrait;
|
||||
|
||||
class ProjectsBudgetPump extends AbstractPump
|
||||
{
|
||||
use ArrayPumpTrait;
|
||||
use DoctrinePumpTrait;
|
||||
|
||||
private const COST_KEYS = [
|
||||
'id',
|
||||
'project',
|
||||
'cost',
|
||||
'description',
|
||||
'type',
|
||||
'amount',
|
||||
'required',
|
||||
'from',
|
||||
'until',
|
||||
'order',
|
||||
];
|
||||
|
||||
public function __construct(
|
||||
private ProjectRepository $projectRepository,
|
||||
) {}
|
||||
|
||||
public function supports(mixed $sample): bool
|
||||
{
|
||||
if (\is_array($sample) && $this->hasAllKeys($sample, self::COST_KEYS)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function pump(mixed $record): void
|
||||
{
|
||||
if (empty($record['cost']) || empty($record['amount'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$project = $this->getBudgetProject($record);
|
||||
if ($project === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$budgetItem = new BudgetItem();
|
||||
$budgetItem->setTranslatableLocale($project->getLocales()[0]);
|
||||
$budgetItem->setProject($project);
|
||||
$budgetItem->setType($this->getCostType($record));
|
||||
$budgetItem->setTitle($record['cost']);
|
||||
$budgetItem->setDescription($record['description'] ?? $record['cost']);
|
||||
|
||||
$money = new Money($record['amount'] * 100, 'EUR');
|
||||
|
||||
if ($record['required'] > 0) {
|
||||
$budgetItem->setOptimum($money);
|
||||
} else {
|
||||
$budgetItem->setMinimum($money);
|
||||
}
|
||||
|
||||
$this->persist($budgetItem);
|
||||
}
|
||||
|
||||
private function getBudgetProject(array $record): ?Project
|
||||
{
|
||||
return $this->projectRepository->findOneBy(['migratedId' => $record['project']]);
|
||||
}
|
||||
|
||||
private function getCostType(array $record): BudgetItemType
|
||||
{
|
||||
switch ($record['type']) {
|
||||
case 'task':
|
||||
return BudgetItemType::Task;
|
||||
case 'material':
|
||||
return BudgetItemType::Material;
|
||||
default:
|
||||
return BudgetItemType::Infrastructure;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace App\Benzina;
|
||||
|
||||
use App\Entity\Money;
|
||||
use App\Entity\Project\BudgetItemType;
|
||||
use App\Entity\Project\Project;
|
||||
use App\Entity\Project\Reward;
|
||||
use App\Repository\Project\ProjectRepository;
|
||||
use Goteo\Benzina\Pump\AbstractPump;
|
||||
use Goteo\Benzina\Pump\ArrayPumpTrait;
|
||||
use Goteo\Benzina\Pump\DoctrinePumpTrait;
|
||||
|
||||
class ProjectsRewardsPump extends AbstractPump
|
||||
{
|
||||
use ArrayPumpTrait;
|
||||
use DoctrinePumpTrait;
|
||||
|
||||
private const REWARD_KEYS = [
|
||||
'id',
|
||||
'project',
|
||||
'reward',
|
||||
'description',
|
||||
'type',
|
||||
'icon',
|
||||
'other',
|
||||
'license',
|
||||
'amount',
|
||||
'units',
|
||||
'fulsocial',
|
||||
'url',
|
||||
'order',
|
||||
'bonus',
|
||||
'category',
|
||||
'extra_info_message',
|
||||
'subscribable',
|
||||
];
|
||||
|
||||
public function __construct(
|
||||
private ProjectRepository $projectRepository,
|
||||
) {}
|
||||
|
||||
public function supports(mixed $sample): bool
|
||||
{
|
||||
if (\is_array($sample) && $this->hasAllKeys($sample, self::REWARD_KEYS)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function pump(mixed $record): void
|
||||
{
|
||||
if (empty($record['reward']) || empty($record['amount'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($record['type'] !== 'individual') {
|
||||
return;
|
||||
}
|
||||
|
||||
$project = $this->getBudgetProject($record);
|
||||
if ($project === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$reward = new Reward();
|
||||
$reward->setProject($project);
|
||||
$reward->setTitle($record['reward']);
|
||||
$reward->setDescription($record['description'] ?? $record['reward']);
|
||||
$reward->setMoney(new Money($record['amount'] * 100, 'EUR'));
|
||||
$reward->setHasUnits($record['units'] > 0);
|
||||
$reward->setUnitsTotal($record['units'] ?? 0);
|
||||
$reward->setUnitsAvailable($record['units'] ?? 0);
|
||||
|
||||
$this->persist($reward);
|
||||
}
|
||||
|
||||
private function getBudgetProject(array $record): ?Project
|
||||
{
|
||||
return $this->projectRepository->findOneBy(['migratedId' => $record['project']]);
|
||||
}
|
||||
|
||||
private function getCostType(array $record): BudgetItemType
|
||||
{
|
||||
switch ($record['type']) {
|
||||
case 'task':
|
||||
return BudgetItemType::Task;
|
||||
case 'material':
|
||||
return BudgetItemType::Material;
|
||||
default:
|
||||
return BudgetItemType::Infrastructure;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user