Passed
Push — feature/active-node ( 6701c3...87efcf )
by Fike
02:21
created

Accessor::getNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 7
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
crap 1
1
<?php
2
3
namespace AmaTeam\TreeAccess;
4
5
use AmaTeam\TreeAccess\API\AccessorInterface;
6
use AmaTeam\TreeAccess\API\ActiveNodeFactoryInterface;
7
use AmaTeam\TreeAccess\API\Exception\IllegalTargetException;
8
use AmaTeam\TreeAccess\API\TypeAccessorInterface;
9
use AmaTeam\TreeAccess\Locator\Context;
10
use AmaTeam\TreeAccess\Type\Registry;
11
12
class Accessor implements AccessorInterface, ActiveNodeFactoryInterface
13
{
14
    /**
15
     * @var Locator
16
     */
17
    private $locator;
18
    /**
19
     * @var Registry
20
     */
21
    private $registry;
22
23
    /**
24
     * @param Registry $registry
25
     */
26 18
    public function __construct(Registry $registry)
27
    {
28 18
        $this->registry = $registry;
29 18
        $this->locator = new Locator($registry);
30 18
    }
31
32
    /**
33
     * @inheritDoc
34
     */
35 12 View Code Duplication
    public function getNode(&$root, $path)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
    {
37 12
        $context = (new Context())
38 12
            ->setIgnoreIllegal(false)
39 12
            ->setIgnoreMissing(false);
40 12
        return $this->traverse($root, $path, $context);
41
    }
42
43
    /**
44
     * @inheritDoc
45
     */
46 5 View Code Duplication
    public function findNode(&$root, $path)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48 5
        $context = (new Context())
49 5
            ->setIgnoreIllegal(true)
50 5
            ->setIgnoreMissing(true);
51 5
        return $this->traverse($root, $path, $context);
52
    }
53
54 17
    private function traverse(&$root, $path, Context $context)
55
    {
56 17
        $node = new Node([], $root);
57 17
        return $this->locator->locate($node, Paths::normalize($path), $context);
58
    }
59
60
    /**
61
     * @inheritDoc
62
     */
63 7
    public function read(&$root, $path)
64
    {
65 7
        $path = Paths::normalize($path);
66 7
        $node = $this->getNode($root, $path);
67 5
        return $node->getValue();
68
    }
69
70
    /**
71
     * @inheritDoc
72
     */
73 1
    public function enumerate(&$root, $path)
74
    {
75 1
        $normalizedPath = Paths::normalize($path);
76 1
        $parent = $this->getNode($root, $normalizedPath);
77 1
        $accessor = $this->getAccessor($parent->getValue(), $normalizedPath);
78 1
        $target = [];
79 1
        foreach ($accessor->enumerate($parent->getValue()) as $name => $node) {
80 1
            $nodePath = array_merge($normalizedPath, $node->getPath());
81 1
            $target[$name] = Node::withPath($node, $nodePath);
82
        }
83 1
        return $target;
84
    }
85
86
    /**
87
     * @inheritDoc
88
     */
89 5
    public function write(&$root, $path, $value)
90
    {
91 5
        $normalizedPath = Paths::normalize($path);
92 5
        if (empty($normalizedPath)) {
93 1
            $root = $value;
94 1
            return;
95
        }
96 4
        $key = $normalizedPath[sizeof($normalizedPath) - 1];
97 4
        $parentPath = array_slice($normalizedPath, 0, -1);
98 4
        $parent = $this->getNode($root, $parentPath);
99 4
        $accessor = $this->getAccessor($parent->getValue(), $parentPath);
100 3
        $parentValue = &$parent->getValue();
101 3
        $accessor->write($parentValue, $key, $value);
102 3
    }
103
104
    /**
105
     * @inheritDoc
106
     */
107 5
    public function exists($root, $path)
108
    {
109 5
        $normalizedPath = Paths::normalize($path);
110 5
        return $this->findNode($root, $normalizedPath) !== null;
111
    }
112
113
    /**
114
     * @param $value
115
     * @param array $path
116
     *
117
     * @return TypeAccessorInterface
118
     *
119
     * @throws IllegalTargetException
120
     */
121 5
    private function getAccessor($value, array $path)
122
    {
123 5
        $accessor = $this->registry->getAccessor(gettype($value));
124 5
        if (!$accessor) {
125 1
            throw new IllegalTargetException($path);
126
        }
127 4
        return $accessor;
128
    }
129
130
    /**
131
     * @inheritDoc
132
     */
133 1
    public function wrap(&$structure)
134
    {
135 1
        return new ActiveNode($this, [], $structure);
136
    }
137
}
138