• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 10972028624

21 Sep 2024 11:01AM UTC coverage: 7.833% (-0.001%) from 7.834%
10972028624

push

github

dunglas
Merge branch '3.4' into 4.0

12915 of 164879 relevant lines covered (7.83%)

27.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.0
/src/Doctrine/Orm/State/CollectionProvider.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\Doctrine\Orm\State;
15

16
use ApiPlatform\Doctrine\Common\State\LinksHandlerLocatorTrait;
17
use ApiPlatform\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
18
use ApiPlatform\Doctrine\Orm\Extension\QueryResultCollectionExtensionInterface;
19
use ApiPlatform\Doctrine\Orm\Util\QueryNameGenerator;
20
use ApiPlatform\Metadata\Exception\RuntimeException;
21
use ApiPlatform\Metadata\Operation;
22
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
23
use ApiPlatform\State\ProviderInterface;
24
use Doctrine\ORM\EntityManagerInterface;
25
use Doctrine\Persistence\ManagerRegistry;
26
use Psr\Container\ContainerInterface;
27

28
/**
29
 * Collection state provider using the Doctrine ORM.
30
 *
31
 * @author Kévin Dunglas <kevin@dunglas.fr>
32
 * @author Samuel ROZE <samuel.roze@gmail.com>
33
 */
34
final class CollectionProvider implements ProviderInterface
35
{
36
    use LinksHandlerLocatorTrait;
37
    use LinksHandlerTrait;
38

39
    /**
40
     * @param QueryCollectionExtensionInterface[] $collectionExtensions
41
     */
42
    public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, ManagerRegistry $managerRegistry, private readonly iterable $collectionExtensions = [], ?ContainerInterface $handleLinksLocator = null)
43
    {
44
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
355✔
45
        $this->handleLinksLocator = $handleLinksLocator;
355✔
46
        $this->managerRegistry = $managerRegistry;
355✔
47
    }
48

49
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): iterable
50
    {
51
        $entityClass = $operation->getClass();
351✔
52
        if (($options = $operation->getStateOptions()) && $options instanceof Options && $options->getEntityClass()) {
351✔
53
            $entityClass = $options->getEntityClass();
6✔
54
        }
55

56
        /** @var EntityManagerInterface $manager */
57
        $manager = $this->managerRegistry->getManagerForClass($entityClass);
351✔
58

59
        $repository = $manager->getRepository($entityClass);
351✔
60
        if (!method_exists($repository, 'createQueryBuilder')) {
351✔
61
            throw new RuntimeException('The repository class must have a "createQueryBuilder" method.');
×
62
        }
63

64
        $queryBuilder = $repository->createQueryBuilder('o');
351✔
65
        $queryNameGenerator = new QueryNameGenerator();
351✔
66

67
        if ($handleLinks = $this->getLinksHandler($operation)) {
351✔
68
            $handleLinks($queryBuilder, $uriVariables, $queryNameGenerator, ['entityClass' => $entityClass, 'operation' => $operation] + $context);
351✔
69
        } else {
70
            $this->handleLinks($queryBuilder, $uriVariables, $queryNameGenerator, $context, $entityClass, $operation);
×
71
        }
72

73
        foreach ($this->collectionExtensions as $extension) {
351✔
74
            $extension->applyToCollection($queryBuilder, $queryNameGenerator, $entityClass, $operation, $context);
351✔
75

76
            if ($extension instanceof QueryResultCollectionExtensionInterface && $extension->supportsResult($entityClass, $operation, $context)) {
351✔
77
                return $extension->getResult($queryBuilder, $entityClass, $operation, $context);
346✔
78
            }
79
        }
80

81
        return $queryBuilder->getQuery()->getResult();
3✔
82
    }
83
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc