Completed
Push — master ( 071064...9995fc )
by Sven
10s
created

DataEnricher::applyTo()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 16.6682

Importance

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