Passed
Push — master ( 6d13f3...89c0f3 )
by
unknown
03:48
created

FieldDatatypeTrait::getNestedFieldPath()   C

Complexity

Conditions 12
Paths 7

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 39
rs 6.9666
c 0
b 0
f 0
cc 12
nc 7
nop 2

How to fix   Complexity   

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:

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\Elasticsearch\Util;
15
16
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
17
use ApiPlatform\Core\Exception\PropertyNotFoundException;
18
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
19
use Symfony\Component\PropertyInfo\Type;
20
21
/**
22
 * Field datatypes helpers.
23
 *
24
 * @internal
25
 *
26
 * @experimental
27
 *
28
 * @author Baptiste Meyer <[email protected]>
29
 */
30
trait FieldDatatypeTrait
31
{
32
    /**
33
     * @var PropertyMetadataFactoryInterface
34
     */
35
    private $propertyMetadataFactory;
36
37
    /**
38
     * @var ResourceClassResolverInterface
39
     */
40
    private $resourceClassResolver;
41
42
    /**
43
     * Is the decomposed given property of the given resource class potentially mapped as a nested field in Elasticsearch?
44
     */
45
    private function isNestedField(string $resourceClass, string $property): bool
46
    {
47
        return null !== $this->getNestedFieldPath($resourceClass, $property);
48
    }
49
50
    /**
51
     * Get the nested path to the decomposed given property (e.g.: foo.bar.baz => foo.bar).
52
     */
53
    private function getNestedFieldPath(string $resourceClass, string $property): ?string
54
    {
55
        $properties = explode('.', $property);
56
        $currentProperty = array_shift($properties);
57
58
        if (!$properties) {
59
            return null;
60
        }
61
62
        try {
63
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $currentProperty);
64
        } catch (PropertyNotFoundException $e) {
65
            return null;
66
        }
67
68
        if (null === $type = $propertyMetadata->getType()) {
69
            return null;
70
        }
71
72
        if (
73
            Type::BUILTIN_TYPE_OBJECT === $type->getBuiltinType()
74
            && null !== ($nextResourceClass = $type->getClassName())
75
            && $this->resourceClassResolver->isResourceClass($nextResourceClass)
76
        ) {
77
            $nestedPath = $this->getNestedFieldPath($nextResourceClass, implode('.', $properties));
78
79
            return null === $nestedPath ? $nestedPath : "$currentProperty.$nestedPath";
80
        }
81
82
        if (
83
            null !== ($type = $type->getCollectionValueType())
84
            && Type::BUILTIN_TYPE_OBJECT === $type->getBuiltinType()
85
            && null !== ($className = $type->getClassName())
86
            && $this->resourceClassResolver->isResourceClass($className)
87
        ) {
88
            return $currentProperty;
89
        }
90
91
        return null;
92
    }
93
}
94