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

LoopResolver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 40
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A beginLoop() 0 4 1
A endLoop() 0 4 1
A loopStep() 0 5 1
A getReferenceValue() 0 11 3
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
}