Completed
Pull Request — master (#5)
by Moesjarraf
08:07
created

DataEnricher::__construct()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 9.2
cc 4
eloc 9
nc 4
nop 1
1
<?php
2
3
namespace LegalThings;
4
5
use LegalThings\DataEnricher\Node;
6
use LegalThings\DataEnricher\Processor;
7
8
/**
9
 * Enrich objects by processing special properties.
10
 */
11
class DataEnricher
12
{
13
    /**
14
     * Default processors
15
     * @var array
16
     */
17
    public static $defaultProcessors = [
18
        '_ref' => Processor\Reference::class,
19
        '_switch' => Processor\SwitchChoose::class,
20
        '_src' => Processor\Http::class,
21
        '_merge' => Processor\Merge::class,
22
        '_jmespath' => Processor\JmesPath::class,
23
        '_tpl' => Processor\Mustache::class
24
    ];
25
    
26
    
27
    /**
28
     * @var object
29
     */
30
    protected $source;
31
    
32
    /**
33
     * Processors, applied in specified order.
34
     * 
35
     * @var DataEnricher\Processor[]
36
     */
37
    public $processors;
38
    
39
    
40
    /**
41
     * Class constructor
42
     * 
43
     * @param object $source  Data source
44
     */
45
    public function __construct($source)
46
    {
47
        if (!is_object($source)) {
48
            throw new \Exception("Data enricher on works on an object, not on a " . gettype($source));
49
        }
50
        
51
        $this->source = $source;
52
        
53
        foreach (static::$defaultProcessors as $property => $processor) {
54
            if (is_string($processor)) {
55
                $class = $processor;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
56
                $processor = new $class($this, $property);
57
            }
58
            
59
            $this->processors[] = $processor;
60
        }
61
    }
62
    
63
    /**
64
     * Get the source object
65
     * 
66
     * @return object
67
     */
68
    public function getSource()
69
    {
70
        return $this->source;
71
    }
72
    
73
    
74
    /**
75
     * Invoke enricher 
76
     * 
77
     * @param array|object|string $target  Target or dot key path
78
     */
79
    public function applyTo($target)
80
    {
81
        if (is_string($target)) {
82
            $target = \DotKey::on($this->source)->get($target);
83
        }
84
        
85
        $nodes = $this->findNodes($target);
86
        
87
        foreach ($this->processors as $processor) {
88
            $processor->applyTo($nodes);
89
        }
90
    }
91
92
    /**
93
     * Find nodes that have processing instructions
94
     * 
95
     * @param array|object $target
96
     * @return array|object
97
     */
98
    public function findNodes(&$target)
99
    {
100
        $nodes = [];
101
        
102
        foreach ($target as &$value) {
103 View Code Duplication
            if (is_array($value) || (is_object($value) && !$value instanceof Node)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
104
                $this->findNodes($value);
105
            }
106
            
107
            if ($value instanceof stdClass && array_intersect_key((array)$value, $this->processors)) {
0 ignored issues
show
Bug introduced by
The class LegalThings\stdClass does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
108
                $value = new Node($value);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
109
                $nodes[] = $value;
110
            }
111
        }
112
        
113
        return $nodes;
114
    }
115
    
116
    /**
117
     * Replace nodes with their results
118
     * 
119
     * @param array|object $target
120
     */
121
    public function applyNodeResults(&$target)
122
    {
123
        foreach ($target as &$value) {
124 View Code Duplication
            if (is_array($value) || (is_object($value) && !$value instanceof Node)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
125
                $this->findNodes($value);
126
            }
127
            
128
            if ($value instanceof Node) {
129
                $value = $value->getResult();
130
            }
131
        }
132
    }
133
    
134
    /**
135
     * Enrich object
136
     * 
137
     * @param object $subject
138
     */
139
    public static function process($subject)
140
    {
141
        $enrich = new static($subject);
142
        $enrich->applyTo($subject);
143
    }
144
}
145