Passed
Pull Request — master (#2144)
by Alan
03:18
created

CollectionDataProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getCollection() 0 22 5
A supports() 0 3 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
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm;
15
16
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Extension\AggregationCollectionExtensionInterface;
17
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Extension\AggregationResultCollectionExtensionInterface;
18
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
19
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
20
use ApiPlatform\Core\Exception\RuntimeException;
21
use Doctrine\Common\Persistence\ManagerRegistry;
22
use Doctrine\ODM\MongoDB\Aggregation\Builder;
23
24
/**
25
 * Collection data provider for the Doctrine MongoDB ODM.
26
 */
27
final class CollectionDataProvider implements CollectionDataProviderInterface, RestrictedDataProviderInterface
28
{
29
    private $managerRegistry;
30
    private $collectionExtensions;
31
32
    /**
33
     * @param AggregationCollectionExtensionInterface[] $collectionExtensions
34
     */
35
    public function __construct(ManagerRegistry $managerRegistry, iterable $collectionExtensions = [])
36
    {
37
        $this->managerRegistry = $managerRegistry;
38
        $this->collectionExtensions = $collectionExtensions;
39
    }
40
41
    public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
42
    {
43
        return null !== $this->managerRegistry->getManagerForClass($resourceClass);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     *
49
     * @throws RuntimeException
50
     */
51
    public function getCollection(string $resourceClass, string $operationName = null, array $context = [])
52
    {
53
        $manager = $this->managerRegistry->getManagerForClass($resourceClass);
54
55
        $repository = $manager->getRepository($resourceClass);
56
        if (!method_exists($repository, 'createAggregationBuilder')) {
57
            throw new RuntimeException('The repository class must have a "createAggregationBuilder" method.');
58
        }
59
60
        /** @var Builder $aggregationBuilder */
61
        $aggregationBuilder = $repository->createAggregationBuilder();
62
        foreach ($this->collectionExtensions as $extension) {
63
            $extension->applyToCollection($aggregationBuilder, $resourceClass, $operationName, $context);
64
65
            if ($extension instanceof AggregationResultCollectionExtensionInterface && $extension->supportsResult($resourceClass, $operationName, $context)) {
66
                return $extension->getResult($aggregationBuilder, $resourceClass, $operationName, $context);
67
            }
68
        }
69
70
        $aggregationBuilder->match();
71
72
        return $aggregationBuilder->hydrate($resourceClass)->execute();
73
    }
74
}
75