src/DataProvider/AnalysisServiceGroupDataProvider.php line 48

Open in your IDE?
  1. <?php
  2. namespace App\DataProvider;
  3. use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
  4. use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
  5. use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
  6. use App\DataProvider\Traits\CompanyRestrictedTrait;
  7. use App\DataTransformer\AnalysisServiceGroupTransformer;
  8. use App\Entity\AnalysisServiceGroup;
  9. use App\Request\AnalysisServiceGroupRequest;
  10. use Doctrine\Common\Collections\Collection;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Symfony\Component\Security\Core\Security;
  13. class AnalysisServiceGroupDataProvider implements CollectionDataProviderInterface, RestrictedDataProviderInterface, ItemDataProviderInterface
  14. {
  15. use CompanyRestrictedTrait;
  16. public function __construct(
  17. private readonly AnalysisServiceGroupTransformer $transformer,
  18. Security $security,
  19. EntityManagerInterface $entityManager,
  20. ) {
  21. $this->security = $security;
  22. $this->entityManager = $entityManager;
  23. }
  24. /**
  25. * @return array<AnalysisServiceGroupRequest>
  26. */
  27. public function getCollection(string $resourceClass, string $operationName = null): array
  28. {
  29. $company = $this->getCompany();
  30. if ($company === null) {
  31. return [];
  32. }
  33. /** @var Collection<int, AnalysisServiceGroup> $groups */
  34. $groups = $company->getPreselectedAnalysisGroups();
  35. return $groups
  36. ->map(fn(AnalysisServiceGroup $group) => $this->transformer->transform($group))
  37. ->toArray();
  38. }
  39. public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): ?AnalysisServiceGroupRequest
  40. {
  41. $company = $this->getCompany();
  42. if ($company === null) {
  43. return null;
  44. }
  45. /** @var Collection<int, AnalysisServiceGroup> $groups */
  46. $groups = $company->getPreselectedAnalysisGroups();
  47. $available = $groups->filter(
  48. fn(AnalysisServiceGroup $group) => $group->getId() === intval($id)
  49. );
  50. if ($available->isEmpty()) {
  51. return null;
  52. }
  53. return $this->transformer->transform($available->first());
  54. }
  55. public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
  56. {
  57. return AnalysisServiceGroupRequest::class === $resourceClass;
  58. }
  59. }