api/src/Editorial/Admin/DataProvider/EditorialCollectionDataProvider.php
<?php
/**
* Copyright (c) 2021, MOBICOOP. All rights reserved.
* This project is dual licensed under AGPL and proprietary licence.
***************************
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <gnu.org/licenses>.
***************************
* Licence MOBICOOP described in the file
* LICENSE
**************************/
namespace App\Editorial\Admin\DataProvider;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryResultCollectionExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGenerator;
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use App\Editorial\Entity\Editorial;
use Symfony\Component\HttpFoundation\RequestStack;
use Doctrine\Common\Persistence\ManagerRegistry;
/**
* Collection editorial data provider in admin context.
*
* @author Remi Wortemann <remi.wortemann@mobicoop.org>
*
*/
final class EditorialCollectionDataProvider implements CollectionDataProviderInterface, RestrictedDataProviderInterface
{
protected $request;
private $managerRegistry;
private $collectionExtensions;
public function __construct(RequestStack $requestStack, ManagerRegistry $managerRegistry, iterable $collectionExtensions)
{
$this->request = $requestStack->getCurrentRequest();
$this->managerRegistry = $managerRegistry;
$this->collectionExtensions = $collectionExtensions;
}
public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
{
return Editorial::class === $resourceClass && $operationName === "ADMIN_get";
}
public function getCollection(string $resourceClass, string $operationName = null, array $context = [])
{
$manager = $this->managerRegistry->getManagerForClass($resourceClass);
$repository = $manager->getRepository($resourceClass);
/**
* @var EntityRepository $repository
*/
$queryBuilder = $repository->createQueryBuilder('ed');
$queryNameGenerator = new QueryNameGenerator();
foreach ($this->collectionExtensions as $extension) {
$extension->applyToCollection($queryBuilder, $queryNameGenerator, $resourceClass, $operationName, $context);
if ($extension instanceof QueryResultCollectionExtensionInterface && $extension->supportsResult($resourceClass, $operationName)) {
$editorials = $extension->getResult($queryBuilder, $resourceClass, $operationName);
}
}
return $editorials;
}
}