Add match strategy api resource
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\ApiResource\Matchfunding;
|
||||
|
||||
use ApiPlatform\Metadata as API;
|
||||
use App\State\Matchfunding\MatchStrategyStateProvider;
|
||||
|
||||
/**
|
||||
* A MatchStrategy is a predefined code implementation for matching funds in Transactions under a MatchCall.
|
||||
* MatchStrategies can be chosen by the managers in a MatchCall and their behaviour fine-tuned.
|
||||
*/
|
||||
#[API\ApiResource(
|
||||
shortName: 'MatchStrategy',
|
||||
provider: MatchStrategyStateProvider::class
|
||||
)]
|
||||
class MatchStrategyApiResource
|
||||
{
|
||||
#[API\ApiProperty(identifier: true, writable: false)]
|
||||
public string $name;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\State\Matchfunding;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\ApiResource\Matchfunding\MatchStrategyApiResource;
|
||||
use App\Matchfunding\MatchStrategy\Exception\MatchStrategyNotFoundException;
|
||||
use App\Matchfunding\MatchStrategy\MatchStrategyInterface;
|
||||
use App\Matchfunding\MatchStrategy\MatchStrategyLocator;
|
||||
|
||||
class MatchStrategyStateProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private MatchStrategyLocator $matchStrategyLocator,
|
||||
) {}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
|
||||
{
|
||||
if (\array_key_exists('name', $uriVariables)) {
|
||||
try {
|
||||
$strategy = $this->matchStrategyLocator->get($uriVariables['name']);
|
||||
|
||||
return $this->getApiResource($strategy);
|
||||
} catch (MatchStrategyNotFoundException $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
$strategies = $this->matchStrategyLocator->getAll();
|
||||
|
||||
$resources = [];
|
||||
foreach ($strategies as $strategy) {
|
||||
$resources[] = $this->getApiResource($strategy);
|
||||
}
|
||||
|
||||
return $resources;
|
||||
}
|
||||
|
||||
private function getApiResource(MatchStrategyInterface $strategy): MatchStrategyApiResource
|
||||
{
|
||||
$resource = new MatchStrategyApiResource();
|
||||
$resource->name = $strategy::getName();
|
||||
|
||||
return $resource;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user