FederationStructureDataProvider.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace App\DataProvider\Public;
  3. use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
  4. use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
  5. use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
  6. use App\ApiResources\Public\FederationStructure;
  7. use App\Repository\Organization\OrganizationRepository;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. class FederationStructureDataProvider implements ItemDataProviderInterface, CollectionDataProviderInterface, RestrictedDataProviderInterface
  11. {
  12. public function __construct(
  13. private RequestStack $requestStack,
  14. private OrganizationRepository $organizationRepository
  15. ) {}
  16. public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
  17. {
  18. return FederationStructure::class === $resourceClass;
  19. }
  20. public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): ?FederationStructure
  21. {
  22. return $this->organizationRepository->getFederationStructureByOrganizationId($id);
  23. }
  24. public function getCollection(string $resourceClass, string $operationName = null): ArrayCollection
  25. {
  26. $request = $this->requestStack->getCurrentRequest();
  27. if ($request === null) {
  28. throw new \RuntimeException('Undefined request');
  29. }
  30. $parentId = $request->query->get('parent');
  31. if (empty($parentId) || !is_numeric($parentId)) {
  32. throw new \RuntimeException('Bad or missing parent value');
  33. }
  34. return new ArrayCollection($this->organizationRepository->getChildrenStructuresByFederationId($parentId));
  35. }
  36. }