Completed
Pull Request — master (#461)
by Sam
04:05 queued 13s
created

CollectionDataProvider::getCollection()   C

Complexity

Conditions 7
Paths 11

Size

Total Lines 31
Code Lines 16

Duplication

Lines 31
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 31
loc 31
rs 6.7272
cc 7
eloc 16
nc 11
nop 2
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\MongoDB;
13
14
use ApiPlatform\Core\Api\CollectionDataProviderInterface;
15
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
16
use Doctrine\Common\Persistence\ManagerRegistry;
17
use Doctrine\ODM\MongoDB\DocumentRepository;
18
use ApiPlatform\Core\Bridge\Doctrine\MongoDB\Extension\QueryResultExtensionInterface;
19
use ApiPlatform\Core\Bridge\Doctrine\MongoDB\Extension\QueryCollectionExtensionInterface;
20
21
/**
22
 * Collection data provider for the Doctrine MongoDB ODM.
23
 */
24 View Code Duplication
class CollectionDataProvider implements CollectionDataProviderInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
{
26
    private $managerRegistry;
27
    private $collectionExtensions;
28
    private $decorated;
29
30
    /**
31
     * @param ManagerRegistry                      $managerRegistry
32
     * @param QueryCollectionExtensionInterface[]  $collectionExtensions
33
     * @param CollectionDataProviderInterface|null $decorated
34
     */
35
    public function __construct(ManagerRegistry $managerRegistry, array $collectionExtensions = [], CollectionDataProviderInterface $decorated = null)
36
    {
37
        $this->managerRegistry = $managerRegistry;
38
        $this->collectionExtensions = $collectionExtensions;
39
        $this->decorated = $decorated;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getCollection(string $resourceClass, string $operationName = null)
46
    {
47
        if ($this->decorated) {
48
            try {
49
                return $this->decorated->getCollection($resourceClass, $operationName);
50
            } catch (ResourceClassNotSupportedException $resourceClassNotSupportedException) {
51
                // Ignore it
52
            }
53
        }
54
55
        $manager = $this->managerRegistry->getManagerForClass($resourceClass);
56
        if (null === $manager) {
57
            throw new ResourceClassNotSupportedException();
58
        }
59
60
        /** @var DocumentRepository $repository */
61
        $repository = $manager->getRepository($resourceClass);
62
        $queryBuilder = $repository->createQueryBuilder();
63
64
        foreach ($this->collectionExtensions as $extension) {
65
            $extension->applyToCollection($queryBuilder, $resourceClass, $operationName);
66
67
            if ($extension instanceof QueryResultExtensionInterface) {
68
                if ($extension->supportsResult($resourceClass, $operationName)) {
69
                    return $extension->getResult($queryBuilder);
70
                }
71
            }
72
        }
73
74
        return $queryBuilder->getQuery()->execute()->toArray();
75
    }
76
}
77