Completed
Pull Request — master (#11)
by Maksim (Ellrion)
15:23
created

LoopFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 73.32%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 5
c 3
b 1
f 0
lcom 1
cbo 1
dl 0
loc 60
ccs 11
cts 15
cp 0.7332
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A newLoop() 0 13 2
A endLoop() 0 12 2
A loop() 0 7 1
1
<?php
2
3
namespace Advmaker\BladeLoop;
4
5
class LoopFactory
6
{
7
    /**
8
     * The stack of Loop instances
9
     *
10
     * @var Loop[] $stack
11
     */
12
    protected $stack = [];
13
14
    /**
15
     * Creates a new loop with the given array and adds it to the stack
16
     *
17
     * @param array $items The array that will be iterated
18
     * @return Loop
19 15
     */
20
    public function newLoop($items)
21 15
    {
22 15
        $loop = new Loop($items);
23
24
        // Check stack for parent loop to register it with this loop
25
        if (count($this->stack) > 0) {
26
            $loop->setParentLoop(last($this->stack));
27
        }
28
29 15
        array_push($this->stack, $loop);
30
31
        return $loop;
32 15
    }
33 3
34 3
    /**
35
     * Should be called after the loop has finished
36 15
     *
37 15
     * @param $loop
38
     */
39
    public function endLoop(&$loop)
40
    {
41
        array_pop($this->stack);
42
43
        if (count($this->stack) > 0) {
44
            // This loop was inside another loop. We persist the loop variable and assign back the parent loop
45
            $loop = end($this->stack);
46
        } else {
47
            // This loop was not inside another loop. We remove the var
48
            $loop = null;
49
        }
50
    }
51
52
    /**
53
     * To be called first inside the foreach loop. Returns the current loop
54 15
     *
55
     * @return Loop $current The current loop data
56 15
     */
57
    public function loop()
58
    {
59
        $current = end($this->stack);
60
        $current->loop();
61
62
        return $current;
63
    }
64
}
65