-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |