Passed
Pull Request — master (#2144)
by Alan
03:26
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
 * @experimental
28
 *
29
 * @author Alan Poulain <[email protected]>
30
 */
31
final class CollectionDataProvider implements CollectionDataProviderInterface, RestrictedDataProviderInterface
32
{
33
    private $managerRegistry;
34
    private $collectionExtensions;
35
36
    /**
37
     * @param AggregationCollectionExtensionInterface[] $collectionExtensions
38
     */
39
    public function __construct(ManagerRegistry $managerRegistry, iterable $collectionExtensions = [])
40
    {
41
        $this->managerRegistry = $managerRegistry;
42
        $this->collectionExtensions = $collectionExtensions;
43
    }
44
45
    public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
46
    {
47
        return null !== $this->managerRegistry->getManagerForClass($resourceClass);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     *
53
     * @throws RuntimeException
54
     */
55
    public function getCollection(string $resourceClass, string $operationName = null, array $context = [])
56
    {
57
        $manager = $this->managerRegistry->getManagerForClass($resourceClass);
58
59
        $repository = $manager->getRepository($resourceClass);
60
        if (!method_exists($repository, 'createAggregationBuilder')) {
61
            throw new RuntimeException('The repository class must have a "createAggregationBuilder" method.');
62
        }
63
64
        /** @var Builder $aggregationBuilder */
65
        $aggregationBuilder = $repository->createAggregationBuilder();
66
        foreach ($this->collectionExtensions as $extension) {
67
            $extension->applyToCollection($aggregationBuilder, $resourceClass, $operationName, $context);
68
69
            if ($extension instanceof AggregationResultCollectionExtensionInterface && $extension->supportsResult($resourceClass, $operationName, $context)) {
70
                return $extension->getResult($aggregationBuilder, $resourceClass, $operationName, $context);
71
            }
72
        }
73
74
        $aggregationBuilder->match();
75
76
        return $aggregationBuilder->hydrate($resourceClass)->execute();
77
    }
78
}
79