95 lines
3.5 KiB
PHP
95 lines
3.5 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\OrganizationalUnitClassroomGroupInput;
|
|
use App\Dto\Input\OrganizationalUnitClassroomInput;
|
|
use App\Dto\Input\OrganizationalUnitClientGroupInput;
|
|
use App\Dto\Input\OrganizationalUnitInput;
|
|
use App\Dto\Input\OrganizationalUnitRootInput;
|
|
use App\Dto\Input\UserGroupInput;
|
|
use App\Dto\Output\OrganizationalUnitOutput;
|
|
use App\Entity\OrganizationalUnit;
|
|
use App\Model\OrganizationalUnitTypes;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
class OrganizationalUnitProvider implements ProviderInterface
|
|
{
|
|
public function __construct(
|
|
private readonly ProviderInterface $collectionProvider,
|
|
private readonly 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);
|
|
}
|
|
}
|
|
|
|
private function provideCollection(Operation $operation, array $uriVariables = [], array $context = []): object
|
|
{
|
|
$paginator = $this->collectionProvider->provide($operation, $uriVariables, $context);
|
|
|
|
$items = new \ArrayObject();
|
|
foreach ($paginator->getIterator() as $item){
|
|
$items[] = new OrganizationalUnitOutput($item);
|
|
}
|
|
|
|
return new TraversablePaginator($items, $paginator->getCurrentPage(), $paginator->getItemsPerPage(), $paginator->getTotalItems());
|
|
}
|
|
|
|
public function provideItem(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
|
|
{
|
|
$item = $this->itemProvider->provide($operation, $uriVariables, $context);
|
|
|
|
if (!$item) {
|
|
throw new NotFoundHttpException('Organizational unit not found');
|
|
}
|
|
|
|
return new OrganizationalUnitOutput($item);
|
|
}
|
|
|
|
public function provideInput(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
|
|
{
|
|
$input = null;
|
|
|
|
if (isset($uriVariables['uuid'])) {
|
|
$item = $this->itemProvider->provide($operation, $uriVariables, $context);
|
|
|
|
if ( $item !== null ) {
|
|
switch ($item->getType()) {
|
|
case OrganizationalUnitTypes::ORGANIZATIONAL_UNIT:
|
|
$input = new OrganizationalUnitRootInput($item);
|
|
break;
|
|
case OrganizationalUnitTypes::CLASSROOMS_GROUP:
|
|
$input = new OrganizationalUnitClassroomGroupInput($item);
|
|
break;
|
|
case OrganizationalUnitTypes::CLIENTS_GROUP:
|
|
$input = new OrganizationalUnitClientGroupInput($item);
|
|
break;
|
|
case OrganizationalUnitTypes::CLASSROOM;
|
|
$input = new OrganizationalUnitClassroomInput($item);
|
|
}
|
|
}
|
|
|
|
return $input;
|
|
}
|
|
}
|
|
}
|