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

PropertyHelperTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B addLookupsForNestedProperty() 0 41 8
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\Exception\InvalidArgumentException;
17
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
18
use Doctrine\ODM\MongoDB\Aggregation\Builder;
19
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata as MongoDbOdmClassMetadata;
20
21
/**
22
 * Helper trait regarding a property in a MongoDB document using the resource metadata.
23
 *
24
 * @experimental
25
 *
26
 * @author Alan Poulain <[email protected]>
27
 */
28
trait PropertyHelperTrait
29
{
30
    /**
31
     * Splits the given property into parts.
32
     */
33
    abstract protected function splitPropertyParts(string $property/*, string $resourceClass*/): array;
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
34
35
    /**
36
     * Gets class metadata for the given resource.
37
     */
38
    abstract protected function getClassMetadata(string $resourceClass): ClassMetadata;
39
40
    /**
41
     * Adds the necessary lookups for a nested property.
42
     *
43
     * @throws InvalidArgumentException If property is not nested
44
     *
45
     * @return array An array where the first element is the $alias of the lookup,
46
     *               the second element is the $field name
47
     *               the third element is the $associations array
48
     */
49
    protected function addLookupsForNestedProperty(string $property, Builder $aggregationBuilder, string $resourceClass): array
50
    {
51
        $propertyParts = $this->splitPropertyParts($property, $resourceClass);
0 ignored issues
show
Unused Code introduced by
The call to ApiPlatform\Core\Bridge\...t::splitPropertyParts() has too many arguments starting with $resourceClass. ( Ignorable by Annotation )

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

51
        /** @scrutinizer ignore-call */ 
52
        $propertyParts = $this->splitPropertyParts($property, $resourceClass);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
52
        $alias = '';
53
54
        foreach ($propertyParts['associations'] as $association) {
55
            $classMetadata = $this->getClassMetadata($resourceClass);
56
57
            if (!$classMetadata instanceof MongoDbOdmClassMetadata) {
58
                break;
59
            }
60
61
            if ($classMetadata->hasReference($association)) {
62
                $propertyAlias = "${association}_lkup";
63
                // previous_association_lkup.association
64
                $localField = "$alias$association";
65
                // previous_association_lkup.association_lkup
66
                $alias .= $propertyAlias;
67
                $referenceMapping = $classMetadata->getFieldMapping($association);
68
69
                $aggregationBuilder->lookup($classMetadata->getAssociationTargetClass($association))
70
                    ->localField($referenceMapping['isOwningSide'] ? $localField : '_id')
71
                    ->foreignField($referenceMapping['isOwningSide'] ? '_id' : $referenceMapping['mappedBy'])
72
                    ->alias($alias);
73
                $aggregationBuilder->unwind("\$$alias");
74
75
                // assocation.property => association_lkup.property
76
                $property = substr_replace($property, $propertyAlias, strpos($property, $association), \strlen($association));
77
                $resourceClass = $classMetadata->getAssociationTargetClass($association);
78
                $alias .= '.';
79
            } elseif ($classMetadata->hasEmbed($association)) {
80
                $alias = "$association.";
81
                $resourceClass = $classMetadata->getAssociationTargetClass($association);
82
            }
83
        }
84
85
        if ('' === $alias) {
86
            throw new InvalidArgumentException(sprintf('Cannot add lookups for property "%s" - property is not nested.', $property));
87
        }
88
89
        return [$property, $propertyParts['field'], $propertyParts['associations']];
90
    }
91
}
92