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

ItemDataProvider::getItem()   B

Complexity

Conditions 11
Paths 16

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 18
nc 16
nop 4
dl 0
loc 34
rs 7.3166
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
 * @experimental
33
 *
34
 * @author Alan Poulain <[email protected]>
35
 */
36
final class ItemDataProvider implements DenormalizedIdentifiersAwareItemDataProviderInterface, RestrictedDataProviderInterface
37
{
38
    use IdentifierManagerTrait;
39
40
    private $managerRegistry;
41
    private $itemExtensions;
42
43
    /**
44
     * @param AggregationItemExtensionInterface[] $itemExtensions
45
     */
46
    public function __construct(ManagerRegistry $managerRegistry, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, iterable $itemExtensions = [])
47
    {
48
        $this->managerRegistry = $managerRegistry;
49
        $this->propertyNameCollectionFactory = $propertyNameCollectionFactory;
50
        $this->propertyMetadataFactory = $propertyMetadataFactory;
51
        $this->itemExtensions = $itemExtensions;
52
    }
53
54
    public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
55
    {
56
        return null !== $this->managerRegistry->getManagerForClass($resourceClass);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     *
62
     * @throws RuntimeException
63
     */
64
    public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])
65
    {
66
        $manager = $this->managerRegistry->getManagerForClass($resourceClass);
67
68
        if (!\is_array($id) && !($context[IdentifierConverterInterface::HAS_IDENTIFIER_CONVERTER] ?? false)) {
69
            $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

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