Completed
Push — middleware-wip ( a29b16...4a8d7b )
by Romain
11:57
created

StepDefinition::getStepWeight()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 0
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;
15
16
use Romm\ConfigurationObject\Service\Items\Parents\ParentsTrait;
17
use Romm\Formz\Exceptions\EntryNotFoundException;
18
use Romm\Formz\Form\Definition\AbstractFormDefinitionComponent;
19
use Romm\Formz\Form\Definition\Step\Steps;
20
21
class StepDefinition extends AbstractFormDefinitionComponent
22
{
23
    use ParentsTrait;
24
25
    /**
26
     * @var string
27
     * @validate NotEmpty
28
     */
29
    protected $step;
30
31
    /**
32
     * @var \Romm\Formz\Form\Definition\Step\Step\StepDefinition
33
     */
34
    protected $next;
35
36
    /**
37
     * @var \Romm\Formz\Form\Definition\Step\Step\ConditionalStepDefinition[]
38
     */
39
    protected $detour;
40
41
    /**
42
     * @var \Romm\Formz\Form\Definition\Step\Step\ConditionalStepDefinition[]
43
     */
44
    protected $divergence;
45
46
    /**
47
     * @return Step
48
     */
49
    public function getStep()
50
    {
51
        return $this->withFirstParent(
52
            Steps::class,
53
            function (Steps $steps) {
54
                return $steps->getEntry($this->step);
55
            }
56
        );
57
    }
58
59
    public function getStepLevel()
60
    {
61
        $level = 1;
62
63
        if ($this->hasPreviousDefinition()) {
64
            $this->getPreviousDefinition()->alongParents(
65
                function ($parent) use (&$level) {
66
                    if ($parent instanceof self) {
67
                        $level += $parent->getStepWeight();
68
                    } elseif ($parent instanceof Steps) {
69
                        return false;
70
                    }
71
72
                    return true;
73
                }
74
            );
75
        }
76
77
        return $level;
78
    }
79
80
    public function getStepWeight()
81
    {
82
        $weight = 1;
83
        $childWeight = $this->hasNextStep() ? 1 : 0;
84
85
        if ($this->hasDetour()) {
86
            foreach ($this->getDetourSteps() as $detourStep) {
87
                $childWeight = max($childWeight, $detourStep->getStepWeight());
88
            }
89
        }
90
91
        return $weight + $childWeight;
92
    }
93
94
    /**
95
     * @return bool
96
     */
97
    public function hasNextStep()
98
    {
99
        return null !== $this->next
100
            || false === empty($this->detour)
101
            || false === empty($this->divergence);
102
    }
103
104
    /**
105
     * @return StepDefinition
106
     * @throws EntryNotFoundException
107
     */
108
    public function getNextStep()
109
    {
110
        if (false === $this->hasNextStep()) {
111
            throw EntryNotFoundException::nextStepsNotFound($this);
112
        }
113
114
        return $this->next;
115
    }
116
117
    /**
118
     * @return bool
119
     */
120
    public function hasPreviousDefinition()
121
    {
122
        return $this->hasParent(self::class);
123
    }
124
125
    /**
126
     * @return StepDefinition
127
     * @throws EntryNotFoundException
128
     */
129
    public function getPreviousDefinition()
130
    {
131
        if (false === $this->hasPreviousDefinition()) {
132
            throw EntryNotFoundException::previousDefinitionNotFound($this);
133
        }
134
135
        /** @var StepDefinition $previousStepDefinition */
136
        $previousStepDefinition = $this->getFirstParent(self::class);
137
138
        return $previousStepDefinition;
139
    }
140
141
    /**
142
     * @return ConditionalStepDefinition[]
143
     */
144
    public function getDetourSteps()
145
    {
146
        return $this->detour;
147
    }
148
149
    /**
150
     * @return bool
151
     */
152
    public function hasDetour()
153
    {
154
        return false === empty($this->detour);
155
    }
156
157
    /**
158
     * @return bool
159
     */
160
    public function isInDetour()
161
    {
162
        return $this instanceof ConditionalStepDefinition
163
            || $this->hasParent(ConditionalStepDefinition::class);
164
    }
165
166
    public function getDetourRootStep()
167
    {
168
        if (false === $this->isInDetour()) {
169
            throw new \Exception('todo'); // @todo
170
        }
171
172
        $stepDefinition = $this;
173
174
        while ($stepDefinition->hasParent(ConditionalStepDefinition::class)) {
175
            $stepDefinition = $stepDefinition->getFirstParent(ConditionalStepDefinition::class);
176
        }
177
178
        /** @var StepDefinition $stepDefinition */
179
        $stepDefinition = $stepDefinition->getFirstParent(StepDefinition::class);
180
181
        return $stepDefinition->getNextStep();
182
    }
183
184
    /**
185
     * @return ConditionalStepDefinition[]
186
     */
187
    public function getDivergenceSteps()
188
    {
189
        return $this->divergence;
190
    }
191
192
    /**
193
     * @return bool
194
     */
195
    public function hasDivergence()
196
    {
197
        return false === empty($this->divergence);
198
    }
199
}
200