Completed
Push — master ( bc9a97...2c9e04 )
by Gaetano
12:29 queued 07:39
created

LoopResolver   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 89.29%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 58
ccs 25
cts 28
cp 0.8929
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A beginLoop() 0 4 1
A endLoop() 0 4 1
A loopStep() 0 7 1
A getReferenceValue() 0 18 5
A listReferences() 0 4 1
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\ReferenceResolver;
4
5
use Kaliop\eZMigrationBundle\API\EnumerableReferenceResolverInterface;
6
7
class LoopResolver extends AbstractResolver implements EnumerableReferenceResolverInterface
8
{
9
    protected $referencePrefixes = array('loop:');
10
11
    protected $stack = array();
12
13 1
    public function beginLoop()
14
    {
15 1
        $this->stack[] = array('step' => 0, 'key' => null, 'value' => null);
16 1
    }
17
18 1
    public function endLoop()
19
    {
20 1
        array_pop($this->stack);
21 1
    }
22
23 1
    public function loopStep($key = null, $value = null)
24
    {
25 1
        $idx = count($this->stack) - 1;
26 1
        $this->stack[$idx]['step'] = $this->stack[$idx]['step'] + 1;
27 1
        $this->stack[$idx]['key'] = $key;
28 1
        $this->stack[$idx]['value'] = $value;
29 1
    }
30
31
    /**
32
     * @param string $identifier format: 'loop:iteration', 'loop:depth', 'loop:key', 'loop:value'
33
     * @return int
34
     * @throws \Exception When trying to retrieve anything else but index and depth
35
     */
36 1
    public function getReferenceValue($identifier)
37
    {
38 1
        switch(substr($identifier, 5)) {
39 1
            case 'iteration':
40 1
                $current = end($this->stack);
41 1
                return $current['step'];
42 1
            case 'key':
43 1
                $current = end($this->stack);
44 1
                return $current['key'];
45 1
            case 'value':
46 1
                $current = end($this->stack);
47 1
                return $current['value'];
48
            case 'depth':
49
                return count($this->stack);
50
            default:
51
                throw new \Exception("Can not resolve loop value '$identifier'");
52
        }
53
    }
54
55
    /**
56
     * We implement this method (interface) purely for convenience, as it allows this resolver to be added to the
57
     * 'custom-reference-resolver' chain and not break migration suspend/resume
58
     * @return array
59
     */
60 1
    public function listReferences()
61
    {
62 1
        return array();
63
    }
64
}
65