Failed Conditions
Push — master ( e245a2...503ac3 )
by Arnold
8s
created

Node   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 13
c 3
b 0
f 2
lcom 1
cbo 2
dl 0
loc 106
ccs 0
cts 37
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A getResult() 0 8 2
A setResult() 0 4 1
A hasInstruction() 0 5 1
A getInstruction() 0 16 3
A apply() 0 14 3
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