Completed
Push — master ( bed826...e1fc17 )
by Gaetano
08:02
created

LoopResolver::loopStep()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\ReferenceResolver;
4
5
class LoopResolver extends AbstractResolver
6
{
7
    protected $referencePrefixes = array('loop:');
8
9
    protected $stack = array();
10
11
    public function beginLoop()
12
    {
13
        $this->stack[] = 0;
14
    }
15
16
    public function endLoop()
17
    {
18
        array_pop($this->stack);
19
    }
20
21
    public function loopStep()
22
    {
23
        $idx = count($this->stack) - 1;
24
        $this->stack[$idx] = $this->stack[$idx] + 1;
25
    }
26
27
    /**
28
     * @param string $identifier format: 'loop:index', 'loop:depth'
29
     * @return int
30
     * @throws \Exception When trying to retrieve anything else but index and depth
31
     */
32
    public function getReferenceValue($identifier)
33
    {
34
        switch(substr($identifier, 5)) {
35
            case 'iteration':
36
                return end($this->stack);
37
            case 'depth':
38
                return count($this->stack);
39
            default:
40
                throw new \Exception("Can not resolve loop value '$identifier'");
41
        }
42
    }
43
44
}