Skip to content

Commit

Permalink
Custom processor to set units available from total units on new proje…
Browse files Browse the repository at this point in the history
…ct rewards
  • Loading branch information
subiabre committed Dec 16, 2024
1 parent fe90553 commit 5e11cd4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/ApiResource/Project/RewardApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use ApiPlatform\Metadata as API;
use App\Entity\Money;
use App\Entity\Project\Reward;
use App\State\ApiResourceStateProcessor;
use App\State\ApiResourceStateProvider;
use App\State\Project\RewardStateProcessor;
use AutoMapper\Attribute\MapTo;
use Symfony\Component\Validator\Constraints as Assert;

Expand All @@ -18,7 +18,7 @@
shortName: 'ProjectReward',
stateOptions: new Options(entityClass: Reward::class),
provider: ApiResourceStateProvider::class,
processor: ApiResourceStateProcessor::class
processor: RewardStateProcessor::class
)]
class RewardApiResource
{
Expand Down
2 changes: 1 addition & 1 deletion src/State/Project/ProjectStateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
/** @var Project */
$project = $this->autoMapper->map($data, Project::class);

if (!isset($data->id)) {
if (!$project->getId()) {
$owner = $this->authService->getUser();

if (!$owner) {
Expand Down
37 changes: 37 additions & 0 deletions src/State/Project/RewardStateProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\State\Project;

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use App\ApiResource\Project\RewardApiResource;
use App\Entity\Project\Reward;
use App\Mapping\AutoMapper;
use App\State\EntityStateProcessor;

class RewardStateProcessor implements ProcessorInterface
{
public function __construct(
private EntityStateProcessor $entityStateProcessor,
private AutoMapper $autoMapper,
) {}

/**
* @param RewardApiResource $data
*
* @return RewardApiResource|null
*/
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = [])
{
/** @var Reward */
$reward = $this->autoMapper->map($data, Reward::class);

if (!$reward->getId()) {
$reward->setUnitsAvailable($reward->getUnitsTotal());
}

$reward = $this->entityStateProcessor->process($reward, $operation, $uriVariables, $context);

return $this->autoMapper->map($reward, $data);
}
}

0 comments on commit 5e11cd4

Please sign in to comment.