Completed
Pull Request — master (#717)
by Antoine
23:28 queued 20:06
created

CollectionDataProvider::getQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
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
namespace ApiPlatform\Core\Bridge\Doctrine\Orm;
13
14
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
15
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryResultExtensionInterface;
16
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGenerator;
17
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
18
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
19
use ApiPlatform\Core\Exception\RuntimeException;
20
use Doctrine\Common\Persistence\ManagerRegistry;
21
use Doctrine\ORM\Query;
22
use Doctrine\ORM\QueryBuilder;
23
24
/**
25
 * Collection data provider for the Doctrine ORM.
26
 *
27
 * @author Kévin Dunglas <[email protected]>
28
 * @author Samuel ROZE <[email protected]>
29
 */
30
class CollectionDataProvider implements CollectionDataProviderInterface
31
{
32
    private $managerRegistry;
33
    private $collectionExtensions;
34
    private $decorated;
35
36
    /**
37
     * @param ManagerRegistry                     $managerRegistry
38
     * @param QueryCollectionExtensionInterface[] $collectionExtensions
39
     */
40
    public function __construct(ManagerRegistry $managerRegistry, array $collectionExtensions = [])
41
    {
42
        $this->managerRegistry = $managerRegistry;
43
        $this->collectionExtensions = $collectionExtensions;
44
    }
45
46
    /**
47
     * Allow to customize the Query object
48
     * For example you can force doctrine to not load lazy relations by using:
49
     * $query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true).
50
     *
51
     * @param QueryBuilder
52
     *
53
     * @return Query
54
     */
55
    public function getQuery(QueryBuilder $queryBuilder): Query
56
    {
57
        return $queryBuilder->getQuery();
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function getCollection(string $resourceClass, string $operationName = null)
64
    {
65
        $manager = $this->managerRegistry->getManagerForClass($resourceClass);
66
        if (null === $manager) {
67
            throw new ResourceClassNotSupportedException();
68
        }
69
70
        $repository = $manager->getRepository($resourceClass);
71
        if (!method_exists($repository, 'createQueryBuilder')) {
72
            throw new RuntimeException('The repository class must have a "createQueryBuilder" method.');
73
        }
74
75
        $queryBuilder = $repository->createQueryBuilder('o');
76
        $queryNameGenerator = new QueryNameGenerator();
77
        foreach ($this->collectionExtensions as $extension) {
78
            $extension->applyToCollection($queryBuilder, $queryNameGenerator, $resourceClass, $operationName);
79
80
            if ($extension instanceof QueryResultExtensionInterface) {
81
                if ($extension->supportsResult($resourceClass, $operationName)) {
82
                    return $extension->getResult($queryBuilder);
83
                }
84
            }
85
        }
86
87
        return $this->getQuery($queryBuilder)->getResult();
88
    }
89
}
90