Passed
Pull Request — master (#2162)
by Maximilian
03:14
created

CollectionDataProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

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

3 Methods

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

68
            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...
69
                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

69
                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...
Bug introduced by
$aggregationBuilder of type Doctrine\ODM\MongoDB\Aggregation\Builder is incompatible with the type Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder expected by parameter $aggregationBuilder of ApiPlatform\Core\Bridge\...nInterface::getResult(). ( Ignorable by Annotation )

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

69
                return $extension->getResult(/** @scrutinizer ignore-type */ $aggregationBuilder, $resourceClass, $operationName, $context);
Loading history...
70
            }
71
        }
72
73
        return $aggregationBuilder->hydrate($resourceClass)->execute()->toArray();
74
    }
75
}
76