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

ItemDataProvider   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 26
dl 0
loc 61
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getItem() 0 33 9
A supports() 0 3 1
A __construct() 0 6 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\MongoDB;
15
16
use ApiPlatform\Core\Bridge\Doctrine\MongoDB\Extension\AggregationItemExtensionInterface;
17
use ApiPlatform\Core\Bridge\Doctrine\MongoDB\Extension\AggregationResultItemExtensionInterface;
18
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\IdentifierManagerTrait;
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 (!($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
68
        $fetchData = $context['fetch_data'] ?? true;
69
        if (!$fetchData && $manager instanceof DocumentManager) {
70
            return $manager->getReference($resourceClass, reset($id));
71
        }
72
73
        $repository = $manager->getRepository($resourceClass);
74
        if (!method_exists($repository, 'createAggregationBuilder')) {
75
            throw new RuntimeException('The repository class must have a "createAggregationBuilder" method.');
76
        }
77
        /** @var Builder $aggregationBuilder */
78
        $aggregationBuilder = $repository->createAggregationBuilder();
79
80
        foreach ($id as $propertyName => $value) {
81
            $aggregationBuilder->match()->field($propertyName)->equals($value);
82
        }
83
84
        foreach ($this->itemExtensions as $extension) {
85
            $extension->applyToItem($aggregationBuilder, $resourceClass, $id, $operationName);
86
87
            if ($extension instanceof AggregationResultItemExtensionInterface && $extension->supportsResult($resourceClass, $operationName, $context)) {
88
                return $extension->getResult($aggregationBuilder, $resourceClass, $operationName, $context);
89
            }
90
        }
91
92
        return $aggregationBuilder->hydrate($resourceClass)->execute()->getSingleResult();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $aggregationBuild...te()->getSingleResult() also could return the type array which is incompatible with the return type mandated by ApiPlatform\Core\DataPro...derInterface::getItem() of null|object.
Loading history...
93
    }
94
}
95