Failed Conditions
Push — master ( 04820f...7dec56 )
by Moesjarraf
03:10
created

GetByReference   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 72.21%

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 56
ccs 13
cts 18
cp 0.7221
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A withSourceAndTarget() 0 9 1
B getByReference() 0 19 5
1
<?php
2
3
namespace LegalThings\DataEnricher\Processor\Helper;
4
5
use Jasny\DotKey;
6
7
/**
8
 * Get property from source or target by reference
9
 */
10
trait GetByReference
11
{
12
    /**
13
     * @var object
14
     */
15
    protected $source;
16
17
    /**
18
     * @var object|array
19
     */
20
    protected $target;
21
22
    
23
    /**
24
     * Get copy of processor that uses the given source and target
25
     * 
26
     * @param object       $source  Data source
27
     * @param array|object $target  Target or dot key path
28
     */
29 1
    public function withSourceAndTarget($source, $target)
30
    {
31 1
        $clone = clone $this;
32
        
33 1
        $clone->source = $source;
34 1
        $clone->target = $target;
35
        
36 1
        return $clone;
37
    }
38
    
39
    /**
40
     * Get item by reference
41
     * 
42
     * @param string       $ref
43
     * @param object       $source
44
     * @param object|array $target
45
     */
46 1
    protected function getByReference($ref, $source, $target)
47
    {
48 1
        $subject = $source;
49
        
50 1
        if ($ref === '$') {
51
            return $source;
52 1
        } elseif ($ref === '@') {
53
            return $target;
54
        }
55
        
56 1
        if (substr($ref, 0, 2) === '$.') {
57 1
            $ref = substr($ref, 2);
58 1
        } elseif (substr($ref, 0, 2) === '@.') {
59
            $ref = substr($ref, 2);
60
            $subject = $target;
61
        }
62
        
63 1
        return DotKey::on($subject)->get($ref);
64
    }
65
}
66