Completed
Pull Request — master (#1297)
by Antoine
02:54
created

EagerLoadingExtension::joinRelations()   F

Complexity

Conditions 23
Paths 249

Size

Total Lines 77
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 77
rs 3.9479
c 0
b 0
f 0
cc 23
eloc 44
nc 249
nop 11

How to fix   Long Method    Complexity    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\Orm\Extension;
15
16
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\EagerLoadingTrait;
17
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
18
use ApiPlatform\Core\Exception\PropertyNotFoundException;
19
use ApiPlatform\Core\Exception\ResourceClassNotFoundException;
20
use ApiPlatform\Core\Exception\RuntimeException;
21
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
22
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
23
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
24
use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface;
25
use Doctrine\ORM\Mapping\ClassMetadataInfo;
26
use Doctrine\ORM\QueryBuilder;
27
use Symfony\Component\HttpFoundation\RequestStack;
28
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
29
30
/**
31
 * Eager loads relations.
32
 *
33
 * @author Charles Sarrazin <[email protected]>
34
 * @author Kévin Dunglas <[email protected]>
35
 * @author Antoine Bluchet <[email protected]>
36
 * @author Baptiste Meyer <[email protected]>
37
 */
38
final class EagerLoadingExtension implements QueryCollectionExtensionInterface, QueryItemExtensionInterface
39
{
40
    use EagerLoadingTrait;
41
42
    private $propertyNameCollectionFactory;
43
    private $propertyMetadataFactory;
44
    private $classMetadataFactory;
45
    private $maxJoins;
46
    private $serializerContextBuilder;
47
    private $requestStack;
48
49
    /**
50
     * @TODO move $fetchPartial after $forceEager (@soyuka) in 3.0
51
     */
52
    public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, int $maxJoins = 30, bool $forceEager = true, RequestStack $requestStack = null, SerializerContextBuilderInterface $serializerContextBuilder = null, bool $fetchPartial = false, ClassMetadataFactoryInterface $classMetadataFactory = null)
53
    {
54
        $this->propertyNameCollectionFactory = $propertyNameCollectionFactory;
55
        $this->propertyMetadataFactory = $propertyMetadataFactory;
56
        $this->resourceMetadataFactory = $resourceMetadataFactory;
57
        $this->classMetadataFactory = $classMetadataFactory;
58
        $this->maxJoins = $maxJoins;
59
        $this->forceEager = $forceEager;
60
        $this->fetchPartial = $fetchPartial;
61
        $this->serializerContextBuilder = $serializerContextBuilder;
62
        $this->requestStack = $requestStack;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null)
69
    {
70
        $options = [];
71
72
        if (null !== $operationName) {
73
            $options = ['collection_operation_name' => $operationName];
74
        }
75
76
        $forceEager = $this->shouldOperationForceEager($resourceClass, $options);
77
        $fetchPartial = $this->shouldOperationFetchPartial($resourceClass, $options);
78
        $serializerContext = $this->getSerializerContext($resourceClass, 'normalization_context', $options);
79
80
        $groups = $this->getSerializerGroups($options, $serializerContext);
81
82
        $this->joinRelations($queryBuilder, $queryNameGenerator, $resourceClass, $forceEager, $fetchPartial, $queryBuilder->getRootAliases()[0], $groups, $serializerContext);
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     * The context may contain serialization groups which helps defining joined entities that are readable.
88
     */
89
    public function applyToItem(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, array $identifiers, string $operationName = null, array $context = [])
90
    {
91
        $options = [];
92
93
        if (null !== $operationName) {
94
            $options = ['item_operation_name' => $operationName];
95
        }
96
97
        $forceEager = $this->shouldOperationForceEager($resourceClass, $options);
98
        $fetchPartial = $this->shouldOperationFetchPartial($resourceClass, $options);
99
        $contextType = isset($context['api_denormalize']) ? 'denormalization_context' : 'normalization_context';
100
        $serializerContext = $this->getSerializerContext($context['resource_class'] ?? $resourceClass, $contextType, $options);
101
102
        if (isset($context['groups'])) {
103
            $groups = ['serializer_groups' => $context['groups']];
104
        } else {
105
            $groups = $this->getSerializerGroups($options, $serializerContext);
106
        }
107
108
        $this->joinRelations($queryBuilder, $queryNameGenerator, $resourceClass, $forceEager, $fetchPartial, $queryBuilder->getRootAliases()[0], $groups, $serializerContext);
109
    }
110
111
    /**
112
     * Joins relations to eager load.
113
     *
114
     * @param QueryBuilder                $queryBuilder
115
     * @param QueryNameGeneratorInterface $queryNameGenerator
116
     * @param string                      $resourceClass
117
     * @param bool                        $forceEager
118
     * @param string                      $parentAlias
119
     * @param array                       $propertyMetadataOptions
120
     * @param array                       $context
121
     * @param bool                        $wasLeftJoin             if the relation containing the new one had a left join, we have to force the new one to left join too
122
     * @param int                         $joinCount               the number of joins
123
     * @param int                         $currentDepth            the current max depth
124
     *
125
     * @throws RuntimeException when the max number of joins has been reached
126
     */
127
    private function joinRelations(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, bool $forceEager, bool $fetchPartial, string $parentAlias, array $propertyMetadataOptions = [], array $context = [], bool $wasLeftJoin = false, int &$joinCount = 0, int $currentDepth = null)
128
    {
129
        if ($joinCount > $this->maxJoins) {
130
            throw new RuntimeException('The total number of joined relations has exceeded the specified maximum. Raise the limit if necessary, or use the "max_depth" option of the Symfony serializer.');
131
        }
132
133
        $currentDepth = $currentDepth > 0 ? $currentDepth - 1 : $currentDepth;
134
        $entityManager = $queryBuilder->getEntityManager();
135
        $classMetadata = $entityManager->getClassMetadata($resourceClass);
136
        $attributesMetadata = $this->classMetadataFactory ? $this->classMetadataFactory->getMetadataFor($resourceClass)->getAttributesMetadata() : null;
137
138
        foreach ($classMetadata->associationMappings as $association => $mapping) {
139
            //Don't join if max depth is enabled and the current depth limit is reached
140
            if (isset($context['enable_max_depth']) && 0 === $currentDepth) {
141
                continue;
142
            }
143
144
            try {
145
                $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $association, $propertyMetadataOptions);
146
            } catch (PropertyNotFoundException $propertyNotFoundException) {
147
                //skip properties not found
148
                continue;
149
            } catch (ResourceClassNotFoundException $resourceClassNotFoundException) {
150
                //skip associations that are not resource classes
151
                continue;
152
            }
153
154
            // We don't want to interfere with doctrine on this association
155
            if (false === $forceEager && ClassMetadataInfo::FETCH_EAGER !== $mapping['fetch']) {
156
                continue;
157
            }
158
159
            $readable = $propertyMetadata->isReadableLink() || $propertyMetadata->isReadable() || $propertyMetadata->getAttribute('fetchEager', false);
160
161
            if (false === $readable) {
162
                continue;
163
            }
164
165
            $isNullable = $mapping['joinColumns'][0]['nullable'] ?? true;
166
            if (false !== $wasLeftJoin || true === $isNullable) {
167
                $method = 'leftJoin';
168
            } else {
169
                $method = 'innerJoin';
170
            }
171
172
            $associationAlias = $queryNameGenerator->generateJoinAlias($association);
173
            $queryBuilder->{$method}(sprintf('%s.%s', $parentAlias, $association), $associationAlias);
174
            ++$joinCount;
175
176
            if (true === $fetchPartial) {
177
                try {
178
                    $this->addSelect($queryBuilder, $mapping['targetEntity'], $associationAlias, $propertyMetadataOptions);
179
                } catch (ResourceClassNotFoundException $resourceClassNotFoundException) {
180
                    continue;
181
                }
182
            } else {
183
                $queryBuilder->addSelect($associationAlias);
184
            }
185
186
            // Avoid recursion
187
            if ($mapping['targetEntity'] === $resourceClass) {
188
                $queryBuilder->addSelect($associationAlias);
189
                continue;
190
            }
191
192
            if (isset($attributesMetadata[$association])) {
193
                $maxDepth = $attributesMetadata[$association]->getMaxDepth();
194
195
                // The current depth is the lowest max depth available in the ancestor tree.
196
                if (null !== $maxDepth && (null === $currentDepth || $maxDepth < $currentDepth)) {
197
                    $currentDepth = $maxDepth;
198
                }
199
            }
200
201
            $this->joinRelations($queryBuilder, $queryNameGenerator, $mapping['targetEntity'], $forceEager, $fetchPartial, $associationAlias, $propertyMetadataOptions, $context, $method === 'leftJoin', $joinCount, $currentDepth);
202
        }
203
    }
204
205
    private function addSelect(QueryBuilder $queryBuilder, string $entity, string $associationAlias, array $propertyMetadataOptions)
206
    {
207
        $select = [];
208
        $entityManager = $queryBuilder->getEntityManager();
209
        $targetClassMetadata = $entityManager->getClassMetadata($entity);
210
        if ($targetClassMetadata->subClasses) {
211
            $queryBuilder->addSelect($associationAlias);
212
        } else {
213
            foreach ($this->propertyNameCollectionFactory->create($entity) as $property) {
214
                $propertyMetadata = $this->propertyMetadataFactory->create($entity, $property, $propertyMetadataOptions);
215
216
                if (true === $propertyMetadata->isIdentifier()) {
217
                    $select[] = $property;
218
                    continue;
219
                }
220
221
                //the field test allows to add methods to a Resource which do not reflect real database fields
222 View Code Duplication
                if ($targetClassMetadata->hasField($property) && (true === $propertyMetadata->getAttribute('fetchable') || $propertyMetadata->isReadable())) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
223
                    $select[] = $property;
224
                }
225
226
                if (array_key_exists($property, $targetClassMetadata->embeddedClasses)) {
227
                    foreach ($this->propertyNameCollectionFactory->create($targetClassMetadata->embeddedClasses[$property]['class']) as $embeddedProperty) {
228
                        $propertyMetadata = $this->propertyMetadataFactory->create($entity, $property, $propertyMetadataOptions);
229
                        $propertyName = "$property.$embeddedProperty";
230 View Code Duplication
                        if ($targetClassMetadata->hasField($propertyName) && (true === $propertyMetadata->getAttribute('fetchable') || $propertyMetadata->isReadable())) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
231
                            $select[] = $propertyName;
232
                        }
233
                    }
234
                }
235
            }
236
237
            $queryBuilder->addSelect(sprintf('partial %s.{%s}', $associationAlias, implode(',', $select)));
238
        }
239
    }
240
241
    /**
242
     * Gets serializer context.
243
     *
244
     * @param string $resourceClass
245
     * @param string $contextType   normalization_context or denormalization_context
246
     * @param array  $options       represents the operation name so that groups are the one of the specific operation
247
     *
248
     * @return array
249
     */
250
    private function getSerializerContext(string $resourceClass, string $contextType, array $options): array
251
    {
252
        $request = null;
253
254
        if (null !== $this->requestStack && null !== $this->serializerContextBuilder) {
255
            $request = $this->requestStack->getCurrentRequest();
256
        }
257
258
        if (null !== $this->serializerContextBuilder && null !== $request) {
259
            return $this->serializerContextBuilder->createFromRequest($request, 'normalization_context' === $contextType);
260
        }
261
262
        $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
263
264 View Code Duplication
        if (isset($options['collection_operation_name'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
265
            $context = $resourceMetadata->getCollectionOperationAttribute($options['collection_operation_name'], $contextType, null, true);
266
        } elseif (isset($options['item_operation_name'])) {
267
            $context = $resourceMetadata->getItemOperationAttribute($options['item_operation_name'], $contextType, null, true);
268
        } else {
269
            $context = $resourceMetadata->getAttribute($contextType);
270
        }
271
272
        return $context ? $context : [];
273
    }
274
275
    /**
276
     * Gets serializer groups if available, if not it returns the $options array.
277
     *
278
     * @param array $options represents the operation name so that groups are the one of the specific operation
279
     * @param array $context
280
     *
281
     * @return array
282
     */
283
    private function getSerializerGroups(array $options, array $context): array
284
    {
285
        if (empty($context['groups'])) {
286
            return $options;
287
        }
288
289
        return ['serializer_groups' => $context['groups']];
290
    }
291
}
292