Passed
Pull Request — master (#2144)
by Alan
04:12
created

CollectionDataProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 16
dl 0
loc 44
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A supports() 0 3 1
A getCollection() 0 20 5
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\MongoDB;
15
16
use ApiPlatform\Core\Bridge\Doctrine\MongoDB\Extension\ContextAwareQueryCollectionExtensionInterface;
17
use ApiPlatform\Core\Bridge\Doctrine\MongoDB\Extension\QueryCollectionExtensionInterface;
18
use ApiPlatform\Core\Bridge\Doctrine\MongoDB\Extension\QueryResultExtensionInterface;
19
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
20
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
21
use ApiPlatform\Core\Exception\RuntimeException;
22
use Doctrine\Common\Persistence\ManagerRegistry;
23
use Doctrine\ODM\MongoDB\Aggregation\Builder;
24
25
/**
26
 * Collection data provider for the Doctrine MongoDB ODM.
27
 */
28
final class CollectionDataProvider implements CollectionDataProviderInterface, RestrictedDataProviderInterface
29
{
30
    private $managerRegistry;
31
    private $collectionExtensions;
32
33
    /**
34
     * @param QueryCollectionExtensionInterface[]|ContextAwareQueryCollectionExtensionInterface[] $collectionExtensions
35
     */
36
    public function __construct(ManagerRegistry $managerRegistry, iterable $collectionExtensions = [])
37
    {
38
        $this->managerRegistry = $managerRegistry;
39
        $this->collectionExtensions = $collectionExtensions;
40
    }
41
42
    public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
43
    {
44
        return null !== $this->managerRegistry->getManagerForClass($resourceClass);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     *
50
     * @throws RuntimeException
51
     */
52
    public function getCollection(string $resourceClass, string $operationName = null, array $context = [])
53
    {
54
        $manager = $this->managerRegistry->getManagerForClass($resourceClass);
55
56
        $repository = $manager->getRepository($resourceClass);
57
        if (!method_exists($repository, 'createAggregationBuilder')) {
58
            throw new RuntimeException('The repository class must have a "createAggregationBuilder" method.');
59
        }
60
61
        /** @var Builder $aggregationBuilder */
62
        $aggregationBuilder = $repository->createAggregationBuilder();
63
        foreach ($this->collectionExtensions as $extension) {
64
            $extension->applyToCollection($aggregationBuilder, $resourceClass, $operationName, $context);
65
66
            if ($extension instanceof QueryResultExtensionInterface && $extension->supportsResult($resourceClass, $operationName, $context)) {
0 ignored issues
show
Unused Code introduced by
The call to ApiPlatform\Core\Bridge\...rface::supportsResult() has too many arguments starting with $context. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
            if ($extension instanceof QueryResultExtensionInterface && $extension->/** @scrutinizer ignore-call */ supportsResult($resourceClass, $operationName, $context)) {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
67
                return $extension->getResult($aggregationBuilder, $resourceClass, $operationName, $context);
0 ignored issues
show
Unused Code introduced by
The call to ApiPlatform\Core\Bridge\...nInterface::getResult() has too many arguments starting with $operationName. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
                return $extension->/** @scrutinizer ignore-call */ getResult($aggregationBuilder, $resourceClass, $operationName, $context);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
68
            }
69
        }
70
71
        return $aggregationBuilder->hydrate($resourceClass)->execute()->toArray();
72
    }
73
}
74