Skip to content

Commit

Permalink
Process reward claims
Browse files Browse the repository at this point in the history
  • Loading branch information
subiabre committed Dec 16, 2024
1 parent 5e11cd4 commit 1707256
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/ApiResource/Project/RewardClaimApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use ApiPlatform\Metadata as API;
use App\ApiResource\User\UserApiResource;
use App\Entity\Project\RewardClaim;
use App\State\ApiResourceStateProcessor;
use App\State\ApiResourceStateProvider;
use App\State\Project\RewardClaimStateProcessor;
use Symfony\Component\Validator\Constraints as Assert;

/**
Expand All @@ -17,7 +17,7 @@
shortName: 'ProjectRewardClaim',
stateOptions: new Options(entityClass: RewardClaim::class),
provider: ApiResourceStateProvider::class,
processor: ApiResourceStateProcessor::class
processor: RewardClaimStateProcessor::class
)]
class RewardClaimApiResource
{
Expand Down
3 changes: 3 additions & 0 deletions src/Entity/Project/RewardClaim.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

use App\Entity\Interface\UserOwnedInterface;
use App\Entity\User\User;
use App\Mapping\Provider\EntityMapProvider;
use App\Repository\Project\RewardClaimRepository;
use AutoMapper\Attribute\MapProvider;
use Doctrine\ORM\Mapping as ORM;

#[MapProvider(EntityMapProvider::class)]
#[ORM\Entity(repositoryClass: RewardClaimRepository::class)]
class RewardClaim implements UserOwnedInterface
{
Expand Down
36 changes: 36 additions & 0 deletions src/Service/Project/RewardService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Service\Project;

use App\Entity\Project\Reward;
use App\Entity\Project\RewardClaim;

class RewardService
{
/**
* Processes a RewardClaim for the set Reward
*
* @param Reward $reward The Reward being claimed
*
* @return RewardClaim The processed RewardClaim with updated Reward
*/
public function processClaim(RewardClaim $claim): RewardClaim
{
$reward = $claim->getReward();

if (!$reward->hasUnits()) {
return $claim;
}

$available = $reward->getUnitsAvailable();

if ($available < 1) {
throw new \Exception("The claimed Reward has no units available");
}

$reward->addClaim($claim);
$reward->setUnitsAvailable($available - 1);

return $claim->setReward($reward);
}
}
49 changes: 49 additions & 0 deletions src/State/Project/RewardClaimStateProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\State\Project;

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use App\ApiResource\Project\RewardClaimApiResource;
use App\Entity\Project\RewardClaim;
use App\Mapping\AutoMapper;
use App\Service\Auth\AuthService;
use App\Service\Project\RewardService;
use App\State\EntityStateProcessor;
use Symfony\Component\Security\Core\Exception\AuthenticationException;

class RewardClaimStateProcessor implements ProcessorInterface
{
public function __construct(
private EntityStateProcessor $entityStateProcessor,
private AutoMapper $autoMapper,
private AuthService $authService,
private RewardService $rewardService,
) {}

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

if (!$claim->getId()) {
$owner = $this->authService->getUser();

if (!$owner) {
throw new AuthenticationException();
}

$claim->setOwner($owner);
}

$claim = $this->rewardService->processClaim($claim);
$claim = $this->entityStateProcessor->process($claim, $operation, $uriVariables, $context);

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

0 comments on commit 1707256

Please sign in to comment.