Failed Conditions
Push — master ( 0bbe18...211e20 )
by Moesjarraf
03:53
created

DataEnricher::applyTo()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6.0493

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
ccs 16
cts 18
cp 0.8889
rs 8.439
cc 6
eloc 13
nc 14
nop 2
crap 6.0493
1
<?php
2
3
namespace LegalThings;
4
5
use LegalThings\DataEnricher\Node;
6
use LegalThings\DataEnricher\Processor;
7
use Jasny\DotKey;
8
9
/**
10
 * Enrich objects by processing special properties.
11
 */
12
class DataEnricher
13
{
14
    /**
15
     * Default processors
16
     * @var array
17
     */
18
    public static $defaultProcessors = [
19
        '<ifset>' => Processor\IfSet::class,
20
        '<ref>' => Processor\Reference::class,
21
        '<switch>' => Processor\SwitchChoose::class,
22
        '<merge>' => Processor\Merge::class,
23
        '<tpl>' => Processor\Mustache::class,
24
        '<src>' => Processor\Http::class,
25
        '<jmespath>' => Processor\JmesPath::class,
26
        '<apply>' => Processor\JmesPath::class,
27
        '<transformation>' => Processor\Transform::class,
28
        '<math>' => Processor\Math::class,
29
        '<enrich>' => Processor\Enrich::class,
30
        '<dateformat>' => Processor\DateFormat::class,
31
        '<numberformat>' => Processor\NumberFormat::class,
32
        '<equal>' => Processor\Equal::class,
33
        '<match>' => Processor\Match::class,
34
        '<replace>' => Processor\Replace::class,
35
        '<if>' => Processor\IfElse::class,
36
        '<join>' => Processor\Join::class,
37
        '<encode>' => Processor\Encode::class,
38
        '<decode>' => Processor\Decode::class,
39
        '<serialize>' => Processor\Serialize::class,
40
        '<unserialize>' => Processor\Unserialize::class,
41
        '<hash>' => Processor\Hash::class,
42
        
43
        // Deprecated
44
        '_ref' => Processor\Reference::class,
45
        '_switch' => Processor\SwitchChoose::class,
46
        '_src' => Processor\Http::class,
47
        '_merge' => Processor\Merge::class,
48
        '_jmespath' => Processor\JmesPath::class,
49
        '_tpl' => Processor\Mustache::class,
50
        '_transformation' => Processor\Transform::class
51
    ];
52
    
53
    
54
    /**
55
     * Processors, applied in specified order.
56
     * 
57
     * @var DataEnricher\Processor[]
58
     */
59
    public $processors;
60
    
61
    
62
    /**
63
     * Class constructor
64
     */
65 4
    public function __construct()
66
    {
67 4
        foreach (static::$defaultProcessors as $property => $processor) {
68 4
            if (is_string($processor)) {
69 4
                $processor = new $processor($property);
70 4
            }
71
            
72 4
            $this->processors[] = $processor;
73 4
        }
74 4
    }
75
    
76
    /**
77
     * Create processors
78
     * 
79
     * @param object       $source  Data source
80
     * @param array|object $target  Target or dot key path
81
     * @return Processor[]
82
     */
83 1
    protected function getProcessorsFor($source, $target)
84
    {
85 1
        $processors = [];
86
        
87 1
        foreach ($this->processors as $processor) {
88 1
            $processors[] = $processor->withSourceAndTarget($source, $target);
89 1
        }
90
        
91 1
        return $processors;
92
    }
93
    
94
    
95
    /**
96
     * Apply processing instructions
97
     * 
98
     * @param array|object|string $target  Target or dot key path
99
     * @param object              $source  Data source
100
     */
101 3
    public function applyTo($target, $source = null)
102
    {
103 3
        if (!isset($source)) {
104 3
            $source = $target;
105 3
        }
106
        
107 3
        if (!is_object($source)) {
108 2
            throw new \Exception("Data enricher on works on an object, not on a " . gettype($source));
109
        }
110
        
111 1
        if (is_string($target)) {
112
            $target = DotKey::on($source)->get($target);
113
        }
114
        
115 1
        $nodes = $this->findNodes($target);
116 1
        $processors = $this->getProcessorsFor($source, $target);
117
        
118 1
        foreach ($nodes as $node) {
119 1
            foreach ($processors as $processor) {
120 1
                $node->apply($processor);
121 1
            }
122 1
        }
123
        
124 1
        $this->applyNodeResults($target);
125 1
    }
126
127
    /**
128
     * Find nodes that have processing instructions
129
     * 
130
     * @param array|object $target
131
     * @return array
132
     */
133 1
    public function findNodes(&$target)
134
    {
135 1
        $nodes = [];
136
        
137 1
        foreach ($target as $key => &$value) {
138 1
            if (is_array($value) || (is_object($value) && !$value instanceof Node)) {
139 1
                $nodes = array_merge($nodes, $this->findNodes($value));
140 1
            }
141
            
142 1
            if ($value instanceof \stdClass && $this->hasProcessorProperty($value)) {
143 1
                $value = new Node($value);
144 1
                $nodes[] = $value;
145 1
            }
146 1
        }
147
        
148 1
        return $nodes;
149
    }
150
    
151
    /**
152
     * Check if object has at leas one process property
153
     * 
154
     * @param \stdClass    $value
155
     * @param Processor[]  $processors
0 ignored issues
show
Bug introduced by
There is no parameter named $processors. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
156
     * @return boolean
157
     */
158
    protected function hasProcessorProperty($value)
159
    {
160 1
        $processorProps = array_map(function ($processor) {
161 1
            return $processor->getProperty();
162 1
        }, $this->processors);
163
        
164 1
        $valueProps = array_keys(get_object_vars($value));
165 1
        return count(array_intersect($valueProps, $processorProps)) > 0;
166
    }
167
    
168
    /**
169
     * Replace nodes with their results
170
     * 
171
     * @param array|object $target
172
     */
173 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...
174
    {
175 1
        foreach ($target as &$value) {
176 1
            if ($value instanceof Node) {
177 1
                $value = $value->getResult();
178 1
            } elseif (is_array($value) || is_object($value)) {
179 1
                $this->applyNodeResults($value);
180 1
            }
181 1
        }
182 1
    }
183
}
184