Passed
Push — master ( 8bd912...d93388 )
by Alan
06:58 queued 02:20
created

Bridge/Doctrine/MongoDbOdm/PropertyHelperTrait.php (2 issues)

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
use Doctrine\ODM\MongoDB\Mapping\MappingException;
21
22
/**
23
 * Helper trait regarding a property in a MongoDB document using the resource metadata.
24
 *
25
 * @experimental
26
 *
27
 * @author Alan Poulain <[email protected]>
28
 */
29
trait PropertyHelperTrait
30
{
31
    /**
32
     * Splits the given property into parts.
33
     */
34
    abstract protected function splitPropertyParts(string $property/*, string $resourceClass*/): array;
35
36
    /**
37
     * Gets class metadata for the given resource.
38
     */
39
    abstract protected function getClassMetadata(string $resourceClass): ClassMetadata;
40
41
    /**
42
     * Adds the necessary lookups for a nested property.
43
     *
44
     * @throws InvalidArgumentException If property is not nested
45
     * @throws MappingException
46
     *
47
     * @return array An array where the first element is the $alias of the lookup,
48
     *               the second element is the $field name
49
     *               the third element is the $associations array
50
     */
51
    protected function addLookupsForNestedProperty(string $property, Builder $aggregationBuilder, string $resourceClass): array
52
    {
53
        $propertyParts = $this->splitPropertyParts($property, $resourceClass);
0 ignored issues
show
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

53
        /** @scrutinizer ignore-call */ 
54
        $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...
54
        $alias = '';
55
56
        foreach ($propertyParts['associations'] as $association) {
57
            $classMetadata = $this->getClassMetadata($resourceClass);
58
59
            if (!$classMetadata instanceof MongoDbOdmClassMetadata) {
60
                break;
61
            }
62
63
            if ($classMetadata->hasReference($association)) {
64
                $propertyAlias = "${association}_lkup";
65
                // previous_association_lkup.association
66
                $localField = "$alias$association";
67
                // previous_association_lkup.association_lkup
68
                $alias .= $propertyAlias;
69
                $referenceMapping = $classMetadata->getFieldMapping($association);
70
71
                if (($isOwningSide = $referenceMapping['isOwningSide']) && MongoDbOdmClassMetadata::REFERENCE_STORE_AS_ID !== $referenceMapping['storeAs']) {
72
                    throw MappingException::cannotLookupDbRefReference($classMetadata->getReflectionClass()->getShortName(), $association);
73
                }
74
                if (!$isOwningSide) {
75
                    if (isset($referenceMapping['repositoryMethod']) || !isset($referenceMapping['mappedBy'])) {
76
                        throw MappingException::repositoryMethodLookupNotAllowed($classMetadata->getReflectionClass()->getShortName(), $association);
77
                    }
78
79
                    $targetClassMetadata = $this->getClassMetadata($referenceMapping['targetDocument']);
80
                    if ($targetClassMetadata instanceof MongoDbOdmClassMetadata && MongoDbOdmClassMetadata::REFERENCE_STORE_AS_ID !== $targetClassMetadata->getFieldMapping($referenceMapping['mappedBy'])['storeAs']) {
81
                        throw MappingException::cannotLookupDbRefReference($classMetadata->getReflectionClass()->getShortName(), $association);
82
                    }
83
                }
84
85
                $aggregationBuilder->lookup($classMetadata->getAssociationTargetClass($association))
0 ignored issues
show
It seems like $classMetadata->getAssoc...rgetClass($association) can also be of type null; however, parameter $from of Doctrine\ODM\MongoDB\Aggregation\Builder::lookup() does only seem to accept string, 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

85
                $aggregationBuilder->lookup(/** @scrutinizer ignore-type */ $classMetadata->getAssociationTargetClass($association))
Loading history...
86
                    ->localField($isOwningSide ? $localField : '_id')
87
                    ->foreignField($isOwningSide ? '_id' : $referenceMapping['mappedBy'])
88
                    ->alias($alias);
89
                $aggregationBuilder->unwind("\$$alias");
90
91
                // association.property => association_lkup.property
92
                $property = substr_replace($property, $propertyAlias, strpos($property, $association), \strlen($association));
93
                $resourceClass = $classMetadata->getAssociationTargetClass($association);
94
                $alias .= '.';
95
            } elseif ($classMetadata->hasEmbed($association)) {
96
                $alias = "$association.";
97
                $resourceClass = $classMetadata->getAssociationTargetClass($association);
98
            }
99
        }
100
101
        if ('' === $alias) {
102
            throw new InvalidArgumentException(sprintf('Cannot add lookups for property "%s" - property is not nested.', $property));
103
        }
104
105
        return [$property, $propertyParts['field'], $propertyParts['associations']];
106
    }
107
}
108