| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- <?php
- namespace App\DataProvider\Public;
- use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
- use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
- use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
- use App\ApiResources\Public\FederationStructure;
- use App\Repository\Organization\OrganizationRepository;
- use Doctrine\Common\Collections\ArrayCollection;
- use Symfony\Component\HttpFoundation\RequestStack;
- class FederationStructureDataProvider implements ItemDataProviderInterface, CollectionDataProviderInterface, RestrictedDataProviderInterface
- {
- public function __construct(
- private RequestStack $requestStack,
- private OrganizationRepository $organizationRepository
- ) {}
- public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
- {
- return FederationStructure::class === $resourceClass;
- }
- public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): ?FederationStructure
- {
- return $this->organizationRepository->getFederationStructureByOrganizationId($id);
- }
- public function getCollection(string $resourceClass, string $operationName = null): ArrayCollection
- {
- $request = $this->requestStack->getCurrentRequest();
- if ($request === null) {
- throw new \RuntimeException('Undefined request');
- }
- $parentId = $request->query->get('parent');
- if (empty($parentId) || !is_numeric($parentId)) {
- throw new \RuntimeException('Bad or missing parent value');
- }
- return new ArrayCollection($this->organizationRepository->getChildrenStructuresByFederationId($parentId));
- }
- }
|