<?php
namespace App\DataProvider;
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use App\DataProvider\Traits\CompanyRestrictedTrait;
use App\DataTransformer\AnalysisServiceGroupTransformer;
use App\Entity\AnalysisServiceGroup;
use App\Request\AnalysisServiceGroupRequest;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Security;
class AnalysisServiceGroupDataProvider implements CollectionDataProviderInterface, RestrictedDataProviderInterface, ItemDataProviderInterface
{
use CompanyRestrictedTrait;
public function __construct(
private readonly AnalysisServiceGroupTransformer $transformer,
Security $security,
EntityManagerInterface $entityManager,
) {
$this->security = $security;
$this->entityManager = $entityManager;
}
/**
* @return array<AnalysisServiceGroupRequest>
*/
public function getCollection(string $resourceClass, string $operationName = null): array
{
$company = $this->getCompany();
if ($company === null) {
return [];
}
/** @var Collection<int, AnalysisServiceGroup> $groups */
$groups = $company->getPreselectedAnalysisGroups();
return $groups
->map(fn(AnalysisServiceGroup $group) => $this->transformer->transform($group))
->toArray();
}
public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): ?AnalysisServiceGroupRequest
{
$company = $this->getCompany();
if ($company === null) {
return null;
}
/** @var Collection<int, AnalysisServiceGroup> $groups */
$groups = $company->getPreselectedAnalysisGroups();
$available = $groups->filter(
fn(AnalysisServiceGroup $group) => $group->getId() === intval($id)
);
if ($available->isEmpty()) {
return null;
}
return $this->transformer->transform($available->first());
}
public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
{
return AnalysisServiceGroupRequest::class === $resourceClass;
}
}