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

SwitchChoose::applyToNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace LegalThings\DataEnricher\Processor;
4
5
use LegalThings\DataEnricher;
6
use LegalThings\DataEnricher\Node;
7
use LegalThings\DataEnricher\Processor;
8
use Jasny\DotKey;
9
10
/**
11
 * Choose one of the child properties based on a property in the document
12
 */
13
class SwitchChoose implements Processor
14
{
15
    use Processor\Implementation;
16
    
17
    /**
18
     * @var DotKey
19
     */
20
    protected $source;
21
    
22
    /**
23
     * Class constructor
24
     * 
25
     * @param DataEnricher $invoker
26
     * @param string       $property  Property key which should trigger the processor
27
     */
28
    public function __construct(DataEnricher $invoker, $property)
29
    {
30
        $this->source = DotKey::on($invoker->getSource());
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...
31
        $this->property = $property;
32
    }
33
    
34
    /**
35
     * Apply processing to a single node
36
     * 
37
     * @param Node $node
38
     */
39
    public function applyToNode($node)
40
    {
41
        $ref = $node->getInstruction($this);
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...
42
        $cases = $node->getResult();
43
        
44
        $result = $this->choose($ref, $cases);
45
        $node->setResult($result);
46
    }
47
    
48
    /**
49
     * Choose on of the cases
50
     * 
51
     * @param string $ref    Property name of source
52
     * @param object $cases
53
     * @return type
54
     */
55
    protected function choose($ref, $cases)
56
    {
57
        $test = $this->source->get($ref);
58
        return isset($cases->$test) ? $cases->$test : null;
59
    }
60
}
61