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

PropertyHelperTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 28
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A addLookupsForNestedProperty() 0 17 3
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
19
/**
20
 * Helper trait regarding a property in a MongoDB document using the resource metadata.
21
 *
22
 * @author Alan Poulain <[email protected]>
23
 */
24
trait PropertyHelperTrait
25
{
26
    /**
27
     * Adds the necessary lookups for a nested property.
28
     *
29
     * @throws InvalidArgumentException If property is not nested
30
     *
31
     * @return array An array where the first element is the $alias of the lookup,
32
     *               the second element is the $field name
33
     *               the third element is the $associations array
34
     */
35
    protected function addLookupsForNestedProperty(string $property, Builder $aggregationBuilder, string $resourceClass): array
36
    {
37
        $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

37
        /** @scrutinizer ignore-call */ 
38
        $propertyParts = $this->splitPropertyParts($property, $resourceClass);
Loading history...
38
        $association = $propertyParts['associations'][0] ?? null;
39
40
        if (null === $association) {
41
            throw new InvalidArgumentException(sprintf('Cannot add lookups for property "%s" - property is not nested.', $property));
42
        }
43
44
        $alias = $association;
45
        if ($this->getClassMetadata($resourceClass)->hasReference($association)) {
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

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