Failed Conditions
Push — master ( da580a...04820f )
by Moesjarraf
03:10
created

Node::apply()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
crap 12
1
<?php
2
3
namespace LegalThings\DataEnricher;
4
5
use LegalThings\DataEnricher\Processor;
6
use GuzzleHttp\Promise\PromiseInterface;
7
8
/**
9
 * An object with processing instructions
10
 */
11
class Node extends \stdClass
12
{
13
    /**
14
     * The processed data
15
     * @var mixed 
16
     */
17
    protected $i_result;
18
19
    /**
20
     * Class constructor
21
     * 
22
     * @param \stdClass $data
23
     */
24 5
    public function __construct(\stdClass $data)
25
    {
26 5
        $this->i_result = $data;
27
        
28 5
        foreach ($data as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $data of type object<stdClass> is not traversable.
Loading history...
29 5
            if ($key === 'i_result') {
30
                continue;
31
            }
32
            
33 5
            $this->$key = $value;
34 5
        }
35 5
    }
36
37
    
38
    /**
39
     * Replace nodes with their results
40
     * 
41
     * @param array|object $target
42
     */
43 1 View Code Duplication
    protected function applyNodeResults(&$target)
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...
44
    {
45
        if ($target instanceof self) {
46
            $target = $target->getResult();
47
            $this->applyNodeResults($target);
48 1
        } elseif (is_array($target) || is_object($target)) {
49
            foreach ($target as &$value) {         
50
                $this->applyNodeResults($value);
51
            }
52
        }
53
    }
54
    
55
    /**
56
     * Get the processed result
57
     * 
58
     * @return mixed
59
     */
60
    public function getResult()
61
    {
62
        if ($this->i_result instanceof PromiseInterface) {
63
            $this->i_result->wait();
64
            
65
            if ($this->i_result instanceof PromiseInterface) {
66
                throw new \LogicException("Promise result not replaced with data");
67
            }
68
        }
69
70
        $result = $this->i_result;
71
        
72
        $this->applyNodeResults($result);
73
        return $result;
74
    }
75
    
76
    /**
77
     * Set the result after processing
78
     * 
79
     * @param mixed $result
80
     */
81
    public function setResult($result)
82
    {
83
        $this->i_result = $result;
84
    }
85
86
    
87
    /**
88
     * Test if the node has an instruction for a processor
89
     * 
90
     * @param Processor $processor
91
     * @return boolean
92
     */
93
    public function hasInstruction(Processor $processor)
94
    {
95
        $prop = $processor->getProperty();
96
        return isset($this->$prop);
97
    }
98
    
99
    /**
100
     * Get an instruction for a processor
101
     * 
102
     * @param Processor $processor
103
     * @return mixed
104
     */
105 5
    public function getInstruction(Processor $processor)
106
    {
107 5
        $prop = $processor->getProperty();
108
        
109 5
        if (!isset($this->$prop)) {
110
            throw new \LogicException("Node doesn't have instruction property '$prop'");
111
        }
112
        
113 5
        return $this->resolve($this->$prop);
114
    }
115
116
    /**
117
     * Resolve nodes within the value
118
     * 
119
     * @param mixed $value
120
     * @return mixed
121
     */
122 5 View Code Duplication
    protected function resolve($value)
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...
123
    {
124 5
        while ($value instanceof self) {
125 4
            $value = $value->getResult();
126 4
        }
127
        
128 5
        if (is_array($value) || is_object($value)) {
129 2
            foreach ($value as &$item) {
130 2
                $item = $this->resolve($item);
131 2
            }
132 2
        }
133
        
134 5
        return $value;
135
    }
136
    
137
    /**
138
     * Apply processing to this node
139
     * 
140
     * @param Processor $processor
141
     */
142
    public function apply(Processor $processor)
143
    {
144
        if (!$this->hasInstruction($processor)) {
145
            return;
146
        }
147
        
148
        if ($this->i_result instanceof PromiseInterface) {
149
            $this->i_result->then(function() use ($processor) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
150
                $processor->applyToNode($this);
151
            });
152
        } else {
153
            $processor->applyToNode($this);
154
        }
155
    }
156
}
157