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

PropertyHelperTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 29
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A addLookupsForNestedProperty() 0 18 4
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\ODM\MongoDB\Aggregation\Builder;
18
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
19
20
/**
21
 * Helper trait regarding a property in a MongoDB document using the resource metadata.
22
 *
23
 * @author Alan Poulain <[email protected]>
24
 */
25
trait PropertyHelperTrait
26
{
27
    /**
28
     * Adds the necessary lookups for a nested property.
29
     *
30
     * @throws InvalidArgumentException If property is not nested
31
     *
32
     * @return array An array where the first element is the $alias of the lookup,
33
     *               the second element is the $field name
34
     *               the third element is the $associations array
35
     */
36
    protected function addLookupsForNestedProperty(string $property, Builder $aggregationBuilder, string $resourceClass): array
37
    {
38
        $propertyParts = $this->splitPropertyParts($property, $resourceClass);
0 ignored issues
show
Bug introduced by
It seems like splitPropertyParts() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

38
        /** @scrutinizer ignore-call */ 
39
        $propertyParts = $this->splitPropertyParts($property, $resourceClass);
Loading history...
39
        $association = $propertyParts['associations'][0] ?? null;
40
41
        if (null === $association) {
42
            throw new InvalidArgumentException(sprintf('Cannot add lookups for property "%s" - property is not nested.', $property));
43
        }
44
45
        $alias = $association;
46
        $classMetadata = $this->getClassMetadata($resourceClass);
0 ignored issues
show
Bug introduced by
It seems like getClassMetadata() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

46
        /** @scrutinizer ignore-call */ 
47
        $classMetadata = $this->getClassMetadata($resourceClass);
Loading history...
47
        if ($classMetadata instanceof ClassMetadata && $classMetadata->hasReference($association)) {
48
            $alias = "${association}_lkup";
49
            $aggregationBuilder->lookup($association)->alias($alias);
50
        }
51
52
        // assocation.property => association_lkup.property
53
        return [substr_replace($property, $alias, 0, \strlen($association)), $propertyParts['field'], $propertyParts['associations']];
54
    }
55
}
56