Completed
Push — master ( e1fc17...f005ec )
by Gaetano
09:31
created

LoopResolver::listReferences()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
    public function beginLoop()
14
    {
15
        $this->stack[] = 0;
16
    }
17
18
    public function endLoop()
19
    {
20
        array_pop($this->stack);
21
    }
22
23
    public function loopStep()
24
    {
25
        $idx = count($this->stack) - 1;
26
        $this->stack[$idx] = $this->stack[$idx] + 1;
27
    }
28
29
    /**
30
     * @param string $identifier format: 'loop:index', 'loop:depth'
31
     * @return int
32
     * @throws \Exception When trying to retrieve anything else but index and depth
33
     */
34
    public function getReferenceValue($identifier)
35
    {
36
        switch(substr($identifier, 5)) {
37
            case 'iteration':
38
                return end($this->stack);
39
            case 'depth':
40
                return count($this->stack);
41
            default:
42
                throw new \Exception("Can not resolve loop value '$identifier'");
43
        }
44
    }
45
46
    /**
47
     * We implement this method (interface) purely for convenience, as it allows this resolver to be added to the
48
     * 'custom-reference-resolver' chain and not break migration suspend/resume
49
     * @return array
50
     */
51
    public function listReferences()
52
    {
53
        return array();
54
    }
55
}