Passed
Push — feature/cleanup ( f97f45 )
by Fike
05:53
created

Accessor::exists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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