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

SubresourceDataProvider::buildAggregation()   B

Complexity

Conditions 10
Paths 15

Size

Total Lines 62
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 35
nc 15
nop 5
dl 0
loc 62
rs 7.6666
c 0
b 0
f 0

How to fix   Long Method    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\AggregationCollectionExtensionInterface;
18
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Extension\AggregationItemExtensionInterface;
19
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Extension\AggregationResultCollectionExtensionInterface;
20
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Extension\AggregationResultItemExtensionInterface;
21
use ApiPlatform\Core\DataProvider\SubresourceDataProviderInterface;
22
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
23
use ApiPlatform\Core\Exception\RuntimeException;
24
use ApiPlatform\Core\Identifier\IdentifierConverterInterface;
25
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
26
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
27
use Doctrine\Common\Persistence\ManagerRegistry;
28
use Doctrine\ODM\MongoDB\Aggregation\Builder;
29
use Doctrine\ODM\MongoDB\DocumentManager;
30
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
31
32
/**
33
 * Subresource data provider for the Doctrine MongoDB ODM.
34
 *
35
 * @author Antoine Bluchet <[email protected]>
36
 * @author Alan Poulain <[email protected]>
37
 */
38
final class SubresourceDataProvider implements SubresourceDataProviderInterface
39
{
40
    use IdentifierManagerTrait;
41
42
    private $managerRegistry;
43
    private $collectionExtensions;
44
    private $itemExtensions;
45
46
    /**
47
     * @param AggregationCollectionExtensionInterface[] $collectionExtensions
48
     * @param AggregationItemExtensionInterface[]       $itemExtensions
49
     */
50
    public function __construct(ManagerRegistry $managerRegistry, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, iterable $collectionExtensions = [], iterable $itemExtensions = [])
51
    {
52
        $this->managerRegistry = $managerRegistry;
53
        $this->propertyNameCollectionFactory = $propertyNameCollectionFactory;
54
        $this->propertyMetadataFactory = $propertyMetadataFactory;
55
        $this->collectionExtensions = $collectionExtensions;
56
        $this->itemExtensions = $itemExtensions;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     *
62
     * @throws RuntimeException
63
     */
64
    public function getSubresource(string $resourceClass, array $identifiers, array $context, string $operationName = null)
65
    {
66
        $manager = $this->managerRegistry->getManagerForClass($resourceClass);
67
        if (null === $manager) {
68
            throw new ResourceClassNotSupportedException(sprintf('The object manager associated with the "%s" resource class cannot be retrieved.', $resourceClass));
69
        }
70
71
        $repository = $manager->getRepository($resourceClass);
72
        if (!method_exists($repository, 'createAggregationBuilder')) {
73
            throw new RuntimeException('The repository class must have a "createAggregationBuilder" method.');
74
        }
75
76
        if (!isset($context['identifiers'], $context['property'])) {
77
            throw new ResourceClassNotSupportedException('The given resource class is not a subresource.');
78
        }
79
80
        $aggregationBuilder = $this->buildAggregation($identifiers, $context, $repository->createAggregationBuilder(), \count($context['identifiers']));
81
82
        if (true === $context['collection']) {
83
            foreach ($this->collectionExtensions as $extension) {
84
                $extension->applyToCollection($aggregationBuilder, $resourceClass, $operationName, $context);
85
                if ($extension instanceof AggregationResultCollectionExtensionInterface && $extension->supportsResult($resourceClass, $operationName, $context)) {
86
                    return $extension->getResult($aggregationBuilder, $resourceClass, $operationName, $context);
87
                }
88
            }
89
        } else {
90
            foreach ($this->itemExtensions as $extension) {
91
                $extension->applyToItem($aggregationBuilder, $resourceClass, $identifiers, $operationName, $context);
92
                if ($extension instanceof AggregationResultItemExtensionInterface && $extension->supportsResult($resourceClass, $operationName, $context)) {
93
                    return $extension->getResult($aggregationBuilder, $resourceClass, $operationName, $context);
94
                }
95
            }
96
        }
97
98
        $iterator = $aggregationBuilder->hydrate($resourceClass)->execute();
99
100
        return $context['collection'] ? $iterator->toArray() : $iterator->getSingleResult();
101
    }
102
103
    /**
104
     * @throws RuntimeException
105
     */
106
    private function buildAggregation(array $identifiers, array $context, Builder $previousAggregationBuilder, int $remainingIdentifiers, Builder $topAggregationBuilder = null): Builder
107
    {
108
        if ($remainingIdentifiers <= 0) {
109
            return $previousAggregationBuilder;
110
        }
111
112
        $topAggregationBuilder = $topAggregationBuilder ?? $previousAggregationBuilder;
113
114
        list($identifier, $identifierResourceClass) = $context['identifiers'][$remainingIdentifiers - 1];
115
        $previousAssociationProperty = $context['identifiers'][$remainingIdentifiers][0] ?? $context['property'];
116
117
        $manager = $this->managerRegistry->getManagerForClass($identifierResourceClass);
118
119
        if (!$manager instanceof DocumentManager) {
120
            throw new RuntimeException("The manager for $identifierResourceClass must be a DocumentManager.");
121
        }
122
123
        $classMetadata = $manager->getClassMetadata($identifierResourceClass);
124
125
        if (!$classMetadata instanceof ClassMetadata) {
0 ignored issues
show
introduced by
$classMetadata is always a sub-type of Doctrine\ODM\MongoDB\Mapping\ClassMetadata.
Loading history...
126
            throw new RuntimeException(
127
                "The class metadata for $identifierResourceClass must be an instance of ClassMetadata."
128
            );
129
        }
130
131
        $aggregation = $manager->createAggregationBuilder($identifierResourceClass);
132
        $normalizedIdentifiers = [];
133
134
        if (isset($identifiers[$identifier])) {
135
            // if it's an array it's already normalized, the IdentifierManagerTrait is deprecated
136
            if ($context[IdentifierConverterInterface::HAS_IDENTIFIER_CONVERTER] ?? false) {
137
                $normalizedIdentifiers = $identifiers[$identifier];
138
            } else {
139
                $normalizedIdentifiers = $this->normalizeIdentifiers($identifiers[$identifier], $manager, $identifierResourceClass);
140
            }
141
        }
142
143
        if ($classMetadata->hasAssociation($previousAssociationProperty)) {
144
            $aggregation->lookup($previousAssociationProperty)->alias($previousAssociationProperty);
145
            foreach ($normalizedIdentifiers as $key => $value) {
146
                $aggregation->match()->field($key)->equals($value);
147
            }
148
        } elseif ($classMetadata->isIdentifier($previousAssociationProperty)) {
149
            foreach ($normalizedIdentifiers as $key => $value) {
150
                $aggregation->match()->field($key)->equals($value);
151
            }
152
153
            return $aggregation;
154
        }
155
156
        // Recurse aggregations
157
        $aggregation = $this->buildAggregation($identifiers, $context, $aggregation, --$remainingIdentifiers, $topAggregationBuilder);
158
159
        $results = $aggregation->execute()->toArray();
160
        $in = array_reduce($results, function ($in, $result) use ($previousAssociationProperty) {
161
            return $in + array_map(function ($result) {
162
                return $result['_id'];
163
            }, $result[$previousAssociationProperty] ?? []);
164
        }, []);
165
        $previousAggregationBuilder->match()->field('_id')->in($in);
166
167
        return $previousAggregationBuilder;
168
    }
169
}
170