Failed Conditions
Push — master ( e245a2...503ac3 )
by Arnold
8s
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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 0
cts 10
cp 0
rs 9.4285
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
    public function __construct(\stdClass $data)
25
    {
26
        $this->i_result = $data;
27
        
28
        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
            if ($key === 'i_result') {
30
                continue;
31
            }
32
            
33
            $this->$key = $value;
34
        }
35
    }
36
    
37
    /**
38
     * Get the processed result
39
     * 
40
     * @return mixed
41
     */
42
    public function getResult()
43
    {
44
        if ($this->i_result instanceof PromiseInterface) {
45
            return $this->i_result->wait();
46
        }
47
        
48
        return $this->i_result;
49
    }
50
    
51
    /**
52
     * Set the result after processing
53
     * 
54
     * @param mixed $result
55
     */
56
    public function setResult($result)
57
    {
58
        $this->i_result = $result;
59
    }
60
61
    
62
    /**
63
     * Test if the node has an instruction for a processor
64
     * 
65
     * @param Processor $processor
66
     * @return boolean
67
     */
68
    public function hasInstruction(Processor $processor)
69
    {
70
        $prop = $processor->getProperty();
71
        return isset($this->$prop);
72
    }
73
    
74
    /**
75
     * Get an instruction for a processor
76
     * 
77
     * @param Processor $processor
78
     * @return mixed
79
     */
80
    public function getInstruction(Processor $processor)
81
    {
82
        $prop = $processor->getProperty();
83
        
84
        if (!isset($this->$prop)) {
85
            throw new \LogicException("Node doesn't have instruction property '$prop'");
86
        }
87
        
88
        $value = $this->$prop;
89
        
90
        if ($value instanceof self) {
91
            $value = $value->getResult();
92
        }
93
        
94
        return $value;
95
    }
96
    
97
    /**
98
     * Apply processing to this node
99
     * 
100
     * @param Processor $processor
101
     */
102
    public function apply(Processor $processor)
103
    {
104
        if (!$this->hasInstruction($processor)) {
105
            return;
106
        }
107
        
108
        if ($this->i_result instanceof PromiseInterface) {
109
            $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...
110
                $processor->applyToNode($this);
111
            });
112
        } else {
113
            $processor->applyToNode($this);
114
        }
115
    }
116
}
117