Completed
Push — wip/steps ( f858ae...f1ed33 )
by Romain
10:54
created

Substeps::getMaxLevelRecursive()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 1
1
<?php
2
/*
3
 * 2017 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 FormZ project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\Formz\Form\Definition\Step\Step\Substep;
15
16
use Romm\Formz\Exceptions\EntryNotFoundException;
17
use Romm\Formz\Form\Definition\AbstractFormDefinitionComponent;
18
19
class Substeps extends AbstractFormDefinitionComponent
20
{
21
    /**
22
     * @var \Romm\Formz\Form\Definition\Step\Step\Substep\Substep[]
23
     */
24
    protected $entries;
25
26
    /**
27
     * @var \Romm\Formz\Form\Definition\Step\Step\Substep\SubstepDefinition
28
     * @validate NotEmpty
29
     */
30
    protected $firstSubstep;
31
32
    /**
33
     * @return SubstepDefinition
34
     */
35
    public function getFirstSubstepDefinition()
36
    {
37
        return $this->firstSubstep;
38
    }
39
40
    /**
41
     * @return Substep[]
42
     */
43
    public function getEntries()
44
    {
45
        return $this->entries;
46
    }
47
48
    /**
49
     * @param string $name
50
     * @return Substep
51
     * @throws EntryNotFoundException
52
     */
53
    public function getEntry($name)
54
    {
55
        if (false === $this->hasEntry($name)) {
56
            throw new \Exception('todo'); // @todo
57
        }
58
59
        return $this->entries[$name];
60
    }
61
62
    /**
63
     * @param string $name
64
     * @return bool
65
     */
66
    public function hasEntry($name)
67
    {
68
        return isset($this->entries[$name]);
69
    }
70
71
    /**
72
     * @todo
73
     */
74
    public function getMaxLevel()
75
    {
76
        return $this->getMaxLevelRecursive($this->firstSubstep);
77
    }
78
79
    /**
80
     * @param SubstepDefinition $substepDefinition
81
     * @return int
82
     */
83
    protected function getMaxLevelRecursive(SubstepDefinition $substepDefinition)
84
    {
85
        $maxLevel = $substepDefinition->getLevel();
86
87
        if ($substepDefinition->hasNextSubstep()) {
88
            $maxLevel = $this->getMaxLevelRecursive($substepDefinition->getNextSubstep());
89
        }
90
91
        if ($substepDefinition->hasDivergence()) {
92
            foreach ($substepDefinition->getDivergenceSubsteps() as $divergenceSubstep) {
93
                $maxLevel = max($maxLevel, $this->getMaxLevelRecursive($divergenceSubstep));
94
            }
95
        }
96
97
        return $maxLevel;
98
    }
99
}
100