Passed
Pull Request — master (#2144)
by Alan
07:23
created

CollectionDataProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 21
dl 0
loc 48
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B getCollection() 0 29 7
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
declare(strict_types=1);
13
/*
14
 * This file is part of the API Platform project.
15
 *
16
 * (c) Kévin Dunglas <[email protected]>
17
 *
18
 * For the full copyright and license information, please view the LICENSE
19
 * file that was distributed with this source code.
20
 */
21
22
namespace ApiPlatform\Core\Bridge\Doctrine\MongoDB;
23
24
use ApiPlatform\Core\Bridge\Doctrine\MongoDB\Extension\QueryCollectionExtensionInterface;
25
use ApiPlatform\Core\Bridge\Doctrine\MongoDB\Extension\QueryResultExtensionInterface;
26
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\ContextAwareQueryCollectionExtensionInterface;
27
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
28
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
29
use Doctrine\Common\Persistence\ManagerRegistry;
30
use Doctrine\ODM\MongoDB\DocumentRepository;
31
32
/**
33
 * Collection data provider for the Doctrine MongoDB ODM.
34
 */
35
class CollectionDataProvider implements CollectionDataProviderInterface
36
{
37
    private $managerRegistry;
38
    private $collectionExtensions;
39
    private $decorated;
40
41
    /**
42
     * @param QueryCollectionExtensionInterface[]|ContextAwareQueryCollectionExtensionInterface[] $collectionExtensions
43
     */
44
    public function __construct(ManagerRegistry $managerRegistry, /* iterable */ $collectionExtensions = [], CollectionDataProviderInterface $decorated = null)
45
    {
46
        $this->managerRegistry = $managerRegistry;
47
        $this->collectionExtensions = $collectionExtensions;
48
        $this->decorated = $decorated;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function getCollection(string $resourceClass, string $operationName = null)
55
    {
56
        if ($this->decorated) {
57
            try {
58
                return $this->decorated->getCollection($resourceClass, $operationName);
59
            } catch (ResourceClassNotSupportedException $resourceClassNotSupportedException) {
60
                // Ignore it
61
            }
62
        }
63
64
        if (null === ($manager = $this->managerRegistry->getManagerForClass($resourceClass))) {
65
            throw new ResourceClassNotSupportedException();
66
        }
67
68
        /** @var DocumentRepository $repository */
69
        $repository = $manager->getRepository($resourceClass);
70
        $queryBuilder = $repository->createQueryBuilder();
71
72
        foreach ($this->collectionExtensions as $extension) {
73
            $extension->applyToCollection($queryBuilder, $resourceClass, $operationName);
74
75
            if ($extension instanceof QueryResultExtensionInterface) {
76
                if ($extension->supportsResult($resourceClass, $operationName)) {
77
                    return $extension->getResult($queryBuilder);
78
                }
79
            }
80
        }
81
82
        return $queryBuilder->getQuery()->execute()->toArray();
83
    }
84
}
85