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

ItemDataProvider::getItem()   B

Complexity

Conditions 10
Paths 16

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 18
nc 16
nop 4
dl 0
loc 34
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Common\Util\IdentifierManagerTrait;
17
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Extension\AggregationItemExtensionInterface;
18
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Extension\AggregationResultItemExtensionInterface;
19
use ApiPlatform\Core\DataProvider\DenormalizedIdentifiersAwareItemDataProviderInterface;
20
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
21
use ApiPlatform\Core\Exception\RuntimeException;
22
use ApiPlatform\Core\Identifier\IdentifierConverterInterface;
23
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
24
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
25
use Doctrine\Common\Persistence\ManagerRegistry;
26
use Doctrine\ODM\MongoDB\Aggregation\Builder;
27
use Doctrine\ODM\MongoDB\DocumentManager;
28
29
/**
30
 * Item data provider for the Doctrine MongoDB ODM.
31
 */
32
final class ItemDataProvider implements DenormalizedIdentifiersAwareItemDataProviderInterface, RestrictedDataProviderInterface
33
{
34
    use IdentifierManagerTrait;
35
36
    private $managerRegistry;
37
    private $itemExtensions;
38
39
    /**
40
     * @param AggregationItemExtensionInterface[] $itemExtensions
41
     */
42
    public function __construct(ManagerRegistry $managerRegistry, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, iterable $itemExtensions = [])
43
    {
44
        $this->managerRegistry = $managerRegistry;
45
        $this->propertyNameCollectionFactory = $propertyNameCollectionFactory;
46
        $this->propertyMetadataFactory = $propertyMetadataFactory;
47
        $this->itemExtensions = $itemExtensions;
48
    }
49
50
    public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
51
    {
52
        return null !== $this->managerRegistry->getManagerForClass($resourceClass);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     *
58
     * @throws RuntimeException
59
     */
60
    public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])
61
    {
62
        $manager = $this->managerRegistry->getManagerForClass($resourceClass);
63
64
        if (!\is_array($id) && !($context[IdentifierConverterInterface::HAS_IDENTIFIER_CONVERTER] ?? false)) {
65
            $id = $this->normalizeIdentifiers($id, $manager, $resourceClass);
0 ignored issues
show
Bug introduced by
It seems like $manager can also be of type null; however, parameter $manager of ApiPlatform\Core\Bridge\...:normalizeIdentifiers() does only seem to accept Doctrine\Common\Persistence\ObjectManager, maybe add an additional type check? ( Ignorable by Annotation )

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

65
            $id = $this->normalizeIdentifiers($id, /** @scrutinizer ignore-type */ $manager, $resourceClass);
Loading history...
66
        }
67
        $id = (array) $id;
68
69
        $fetchData = $context['fetch_data'] ?? true;
70
        if (!$fetchData && $manager instanceof DocumentManager) {
71
            return $manager->getReference($resourceClass, reset($id));
72
        }
73
74
        $repository = $manager->getRepository($resourceClass);
75
        if (!method_exists($repository, 'createAggregationBuilder')) {
76
            throw new RuntimeException('The repository class must have a "createAggregationBuilder" method.');
77
        }
78
        /** @var Builder $aggregationBuilder */
79
        $aggregationBuilder = $repository->createAggregationBuilder();
80
81
        foreach ($id as $propertyName => $value) {
82
            $aggregationBuilder->match()->field($propertyName)->equals($value);
83
        }
84
85
        foreach ($this->itemExtensions as $extension) {
86
            $extension->applyToItem($aggregationBuilder, $resourceClass, $id, $operationName, $context);
87
88
            if ($extension instanceof AggregationResultItemExtensionInterface && $extension->supportsResult($resourceClass, $operationName, $context)) {
89
                return $extension->getResult($aggregationBuilder, $resourceClass, $operationName, $context);
90
            }
91
        }
92
93
        return $aggregationBuilder->hydrate($resourceClass)->execute()->current();
94
    }
95
}
96