Completed
Push — master ( 7c6001...071064 )
by Arnold
05:03
created

DataEnricher::applyTo()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 15.566

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
ccs 5
cts 14
cp 0.357
rs 8.439
cc 6
eloc 13
nc 14
nop 2
crap 15.566
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
        '<transformation>' => Processor\Transform::class,
27
        '<math>' => Processor\Math::class,
28
        
29
        // Deprecated
30
        '_ref' => Processor\Reference::class,
31
        '_switch' => Processor\SwitchChoose::class,
32
        '_src' => Processor\Http::class,
33
        '_merge' => Processor\Merge::class,
34
        '_jmespath' => Processor\JmesPath::class,
35
        '_tpl' => Processor\Mustache::class,
36
        '_transformation' => Processor\Transform::class
37
    ];
38
    
39
    
40
    /**
41
     * Processors, applied in specified order.
42
     * 
43
     * @var DataEnricher\Processor[]
44
     */
45
    public $processors;
46
    
47
    
48
    /**
49
     * Class constructor
50
     */
51 2
    public function __construct()
52
    {
53 2
        foreach (static::$defaultProcessors as $property => $processor) {
54
            if (is_string($processor)) {
55
                $processor = new $processor($property);
56
            }
57
            
58
            $this->processors[] = $processor;
59
        }
60 2
    }
61
    
62
    /**
63
     * Create processors
64
     * 
65
     * @param object       $source  Data source
66
     * @param array|object $target  Target or dot key path
67
     * @return Processor[]
68
     */
69
    protected function getProcessorsFor($source, $target)
70
    {
71
        $processors = [];
72
        
73
        foreach ($this->processors as $processor) {
74
            $processors[] = $processor->withSourceAndTarget($source, $target);
75
        }
76
        
77
        return $processors;
78
    }
79
    
80
    
81
    /**
82
     * Apply processing instructions
83
     * 
84
     * @param array|object|string $target  Target or dot key path
85
     * @param object              $source  Data source
86
     */
87 2
    public function applyTo($target, $source = null)
88
    {
89 2
        if (!isset($source)) {
90 2
            $source = $target;
91
        }
92
        
93 2
        if (!is_object($source)) {
94 2
            throw new \Exception("Data enricher on works on an object, not on a " . gettype($source));
95
        }
96
        
97
        if (is_string($target)) {
98
            $target = DotKey::on($source)->get($target);
99
        }
100
        
101
        $nodes = $this->findNodes($target);
102
        $processors = $this->getProcessorsFor($source, $target);
103
        
104
        foreach ($nodes as $node) {
105
            foreach ($processors as $processor) {
106
                $node->apply($processor);
107
            }
108
        }
109
        
110
        $this->applyNodeResults($target);
111
    }
112
113
    /**
114
     * Find nodes that have processing instructions
115
     * 
116
     * @param array|object $target
117
     * @return array
118
     */
119
    public function findNodes(&$target)
120
    {
121
        $nodes = [];
122
        
123
        foreach ($target as $key => &$value) {
124
            if (is_array($value) || (is_object($value) && !$value instanceof Node)) {
125
                $nodes = array_merge($nodes, $this->findNodes($value));
126
            }
127
            
128
            if ($value instanceof \stdClass && $this->hasProcessorProperty($value)) {
129
                $value = new Node($value);
130
                $nodes[] = $value;
131
            }
132
        }
133
        
134
        return $nodes;
135
    }
136
    
137
    /**
138
     * Check if object has at leas one process property
139
     * 
140
     * @param \stdClass    $value
141
     * @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...
142
     * @return boolean
143
     */
144
    protected function hasProcessorProperty($value)
145
    {
146
        $processorProps = array_map(function ($processor) {
147
            return $processor->getProperty();
148
        }, $this->processors);
149
        
150
        $valueProps = array_keys(get_object_vars($value));
151
        return count(array_intersect($valueProps, $processorProps)) > 0;
152
    }
153
    
154
    /**
155
     * Replace nodes with their results
156
     * 
157
     * @param array|object $target
158
     */
159 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...
160
    {
161
        foreach ($target as &$value) {
162
            if ($value instanceof Node) {
163
                $value = $value->getResult();
164
            } elseif (is_array($value) || is_object($value)) {
165
                $this->applyNodeResults($value);
166
            }
167
        }
168
    }
169
}
170