AbstractVisitor   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 9
eloc 26
dl 0
loc 82
ccs 24
cts 28
cp 0.8571
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setDataNavigator() 0 3 1
A propertyConfig() 0 15 3
A navigateObject() 0 33 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CCT\Component\ODMElasticsearch\Transformer\Visitor;
6
7
use CCT\Component\ODMElasticsearch\Metadata\PropertyMetadata;
8
use CCT\Component\ODMElasticsearch\Metadata\PropertyMetadataInterface;
9
use CCT\Component\ODMElasticsearch\Metadata\VirtualPropertyMetadata;
10
use CCT\Component\ODMElasticsearch\Transformer\DataNavigatorInterface;
11
use CCT\Component\ODMElasticsearch\Transformer\Exception\RuntimeException;
12
13
abstract class AbstractVisitor implements VisitorInterface
14
{
15
    /**
16
     * @var DataNavigatorInterface
17
     */
18
    protected $dataNavigator;
19
20
    /**
21
     * Set data navigator for visitor
22
     *
23
     * @param DataNavigatorInterface $dataNavigator
24
     */
25 3
    public function setDataNavigator(DataNavigatorInterface $dataNavigator): void
26
    {
27 3
        $this->dataNavigator = $dataNavigator;
28 3
    }
29
30
    /**
31
     * Extracts data from an object into an array
32
     *
33
     * @param $object
34
     *
35
     * @return array|null
36
     */
37 3
    protected function navigateObject($object): ?array
38
    {
39 3
        if (null === $object) {
40 2
            return null;
41
        }
42
43 3
        if (null === $this->dataNavigator) {
44
            throw new RuntimeException(
45
                'Data Navigator has not been set. Make sure to call setDataNavigator on visitor before transforming'
46
            );
47
        }
48
49 3
        $className = \get_class($object);
50 3
        $metadata = $this->dataNavigator->getMetadataForClass($className);
51
52 3
        $dataArray = [];
53
54
        /** @var PropertyMetadata|VirtualPropertyMetadata $propertyMetadata */
55 3
        foreach ($metadata->getRootClassMetadata()->propertyMetadata as $propertyMetadata) {
0 ignored issues
show
Bug introduced by
The method getRootClassMetadata() does not exist on Metadata\MergeableClassMetadata. ( Ignorable by Annotation )

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

55
        foreach ($metadata->/** @scrutinizer ignore-call */ getRootClassMetadata()->propertyMetadata as $propertyMetadata) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56 3
            if (!($propertyMetadata instanceof PropertyMetadataInterface)) {
57
                continue;
58
            }
59
60 3
            $index = $propertyMetadata->getFieldName() ?? $propertyMetadata->name;
0 ignored issues
show
Bug introduced by
Accessing name on the interface CCT\Component\ODMElastic...opertyMetadataInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
61
62 3
            $value = $propertyMetadata->getValue($object);
63
64 3
            $config = $this->propertyConfig($propertyMetadata, $value);
65
66 3
            $dataArray[$index] = $this->dataNavigator->navigate($value, $this, $config);
67
        }
68
69 3
        return $dataArray;
70
    }
71
72
    /**
73
     * Get property config for object
74
     *
75
     * @param PropertyMetadataInterface|PropertyMetadata|VirtualPropertyMetadata $propertyMetadata
76
     * @param $value
77
     *
78
     * @return array|null
79
     */
80 3
    protected function propertyConfig(PropertyMetadataInterface $propertyMetadata, $value): ?array
81
    {
82 3
        $type = $propertyMetadata->getType();
83
84 3
        if (null === $type) {
85
            return null;
86
        }
87
88 3
        if ('object' === $type) {
89 3
            $className = $propertyMetadata->getTypeClass() ?? \get_class($value);
90
91 3
            return array('type' => $type, 'class' => $className, 'params' => []);
92
        }
93
94 3
        return array('type' => $type, 'params' => array());
95
    }
96
}
97