Completed
Pull Request — master (#717)
by Antoine
28:04 queued 22:25
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
35
    /**
36
     * @param ManagerRegistry                     $managerRegistry
37
     * @param QueryCollectionExtensionInterface[] $collectionExtensions
38
     */
39
    public function __construct(ManagerRegistry $managerRegistry, array $collectionExtensions = [])
40
    {
41
        $this->managerRegistry = $managerRegistry;
42
        $this->collectionExtensions = $collectionExtensions;
43
    }
44
45
    /**
46
     * Allow to customize the Query object
47
     * For example you can force doctrine to not load lazy relations by using:
48
     * $query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true).
49
     *
50
     * @param QueryBuilder
51
     *
52
     * @return Query
53
     */
54
    public function getQuery(QueryBuilder $queryBuilder): Query
55
    {
56
        return $queryBuilder->getQuery();
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function getCollection(string $resourceClass, string $operationName = null)
63
    {
64
        $manager = $this->managerRegistry->getManagerForClass($resourceClass);
65
        if (null === $manager) {
66
            throw new ResourceClassNotSupportedException();
67
        }
68
69
        $repository = $manager->getRepository($resourceClass);
70
        if (!method_exists($repository, 'createQueryBuilder')) {
71
            throw new RuntimeException('The repository class must have a "createQueryBuilder" method.');
72
        }
73
74
        $queryBuilder = $repository->createQueryBuilder('o');
75
        $queryNameGenerator = new QueryNameGenerator();
76
        foreach ($this->collectionExtensions as $extension) {
77
            $extension->applyToCollection($queryBuilder, $queryNameGenerator, $resourceClass, $operationName);
78
79
            if ($extension instanceof QueryResultExtensionInterface) {
80
                if ($extension->supportsResult($resourceClass, $operationName)) {
81
                    return $extension->getResult($queryBuilder);
82
                }
83
            }
84
        }
85
86
        return $this->getQuery($queryBuilder)->getResult();
87
    }
88
}
89