Passed
Push — master ( b23467...14e04f )
by Pierrick
11:03
created

MacroNode::setParent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of NACL.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright 2019 Nuglif (2018) Inc.
9
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
10
 * @author    Pierrick Charron <[email protected]>
11
 * @author    Charle Demers <[email protected]>
12
 */
13
14
declare(strict_types=1);
15
16
namespace Nuglif\Nacl;
17
18
class MacroNode extends Node
19
{
20
    private $callback;
21
    private $param;
22
    private $options;
23
    private $value;
24
    private $isResolved = false;
25
26 10
    public function __construct(callable $callback, $param, ObjectNode $options)
27
    {
28 10
        $this->callback = $callback;
29 10
        $this->param    = $param;
30 10
        $this->options  = $options;
31 10
    }
32
33 6
    public function execute()
34
    {
35 6
        $result = $this->getNativeValue();
36
37 6
        if (is_array($result)) {
38 3
            $result = is_int(key($result)) ? new ArrayNode(array_values($result)) : new ObjectNode($result);
39
        }
40
41 6
        return $result;
42
    }
43
44 9
    public function getNativeValue()
45
    {
46 9
        if (!$this->isResolved) {
47 9
            $this->resolve();
48
        }
49
50 9
        return $this->value;
51
    }
52
53 9
    private function resolve()
54
    {
55 9
        $callback    = $this->callback;
56 9
        $this->value = $callback(
57 9
            $this->param instanceof Node ? $this->param->getNativeValue() : $this->param,
58 9
            $this->options->getNativeValue()
59
        );
60 9
        $this->isResolved = true;
61 9
    }
62
63 6
    public function setParent(Node $parent)
64
    {
65 6
        if ($this->param instanceof Node) {
66 3
            $this->param->setParent($parent);
67
        }
68 6
    }
69
}
70