81 lines
2.7 KiB
PHP
81 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\State\Provider;
|
|
|
|
use ApiPlatform\Metadata\Get;
|
|
use ApiPlatform\Metadata\GetCollection;
|
|
use ApiPlatform\Metadata\Operation;
|
|
use ApiPlatform\Metadata\Patch;
|
|
use ApiPlatform\Metadata\Put;
|
|
use ApiPlatform\State\Pagination\TraversablePaginator;
|
|
use ApiPlatform\State\ProviderInterface;
|
|
use App\Dto\Input\ClientInput;
|
|
use App\Dto\Output\ClientOutput;
|
|
use App\Repository\ClientRepository;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
readonly class ClientProvider implements ProviderInterface
|
|
{
|
|
public function __construct(
|
|
private ClientRepository $clientRepository,
|
|
private ProviderInterface $collectionProvider,
|
|
private ProviderInterface $itemProvider
|
|
)
|
|
{
|
|
}
|
|
|
|
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
|
|
{
|
|
switch ($operation){
|
|
case $operation instanceof GetCollection:
|
|
return $this->provideCollection($operation, $uriVariables, $context);
|
|
case $operation instanceof Patch:
|
|
case $operation instanceof Put:
|
|
return $this->provideInput($operation, $uriVariables, $context);
|
|
case $operation instanceof Get:
|
|
return $this->provideItem($operation, $uriVariables, $context);
|
|
}
|
|
}
|
|
|
|
public function provideCollection(Operation $operation, array $uriVariables = [], array $context = []): object
|
|
{
|
|
$organizationalUnitId = $context['filters']['organizationalUnit.id'] ?? null;
|
|
|
|
if ($organizationalUnitId) {
|
|
$clients = $this->clientRepository->findClientsByOrganizationalUnitAndDescendants((int) $organizationalUnitId);
|
|
|
|
$items = new \ArrayObject();
|
|
foreach ($clients as $client) {
|
|
$items[] = new ClientOutput($client);
|
|
}
|
|
|
|
return new TraversablePaginator($items, 1, count($clients), count($clients));
|
|
}
|
|
|
|
return $this->collectionProvider->provide($operation, $uriVariables, $context);
|
|
}
|
|
|
|
|
|
public function provideItem(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
|
|
{
|
|
$item = $this->itemProvider->provide($operation, $uriVariables, $context);
|
|
|
|
if (!$item) {
|
|
throw new NotFoundHttpException('Client not found');
|
|
}
|
|
|
|
return new ClientOutput($item);
|
|
}
|
|
|
|
public function provideInput(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
|
|
{
|
|
if (isset($uriVariables['uuid'])) {
|
|
$item = $this->itemProvider->provide($operation, $uriVariables, $context);
|
|
|
|
return $item !== null ? new ClientInput($item) : null;
|
|
}
|
|
|
|
return new ClientInput();
|
|
}
|
|
}
|