Completed
Push — wip/steps ( 00e2d0...2b61f8 )
by Romain
14:33
created

StepDefinition::getPreviousDefinition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
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\Formz\Exceptions\EntryNotFoundException;
17
use Romm\Formz\Form\Definition\AbstractFormDefinitionComponent;
18
use Romm\Formz\Form\Definition\Condition\Activation;
19
use Romm\Formz\Form\Definition\Condition\ActivationInterface;
20
use Romm\Formz\Form\Definition\Step\Steps;
21
22
class StepDefinition extends AbstractFormDefinitionComponent
23
{
24
    /**
25
     * @var string
26
     * @validate NotEmpty
27
     */
28
    protected $step;
29
30
    /**
31
     * @var \Romm\Formz\Form\Definition\Condition\Activation
32
     * @validate Romm.Formz:Internal\ConditionIsValid
33
     */
34
    protected $activation;
35
36
    /**
37
     * @var \Romm\Formz\Form\Definition\Step\Step\StepDefinition
38
     */
39
    protected $next;
40
41
    /**
42
     * @var \Romm\Formz\Form\Definition\Step\Step\DivergenceStepDefinition[]
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
    /**
60
     * @return Activation
61
     */
62
    public function getActivation()
63
    {
64
        return $this->activation;
65
    }
66
67
    /**
68
     * @return bool
69
     */
70
    public function hasActivation()
71
    {
72
        return $this->activation instanceof ActivationInterface;
73
    }
74
75
    public function getStepLevel()
76
    {
77
        $level = 1;
78
79
        if ($this->hasPreviousDefinition()) {
80
            $this->getPreviousDefinition()->alongParents(
81
                function ($parent) use (&$level) {
82
                    if ($parent instanceof self) {
83
                        $level += $parent->getStepWeight();
84
                    } elseif ($parent instanceof Steps) {
85
                        return false;
86
                    }
87
88
                    return true;
89
                }
90
            );
91
        }
92
93
        return $level;
94
    }
95
96
    public function getStepWeight()
97
    {
98
        $weight = 1;
99
        $childWeight = $this->hasNextStep() ? 1 : 0;
100
101
        if ($this->hasDivergence()) {
102
            foreach ($this->getDivergenceSteps() as $divergenceStep) {
103
                $childWeight = max($childWeight, $divergenceStep->getStepWeight());
104
            }
105
        }
106
107
        return $weight + $childWeight;
108
    }
109
110
    /**
111
     * @return bool
112
     */
113
    public function hasNextStep()
114
    {
115
        return null !== $this->next;
116
    }
117
118
    /**
119
     * @return StepDefinition
120
     * @throws EntryNotFoundException
121
     */
122
    public function getNextStep()
123
    {
124
        if (false === $this->hasNextStep()) {
125
            throw EntryNotFoundException::nextStepsNotFound($this);
126
        }
127
128
        return $this->next;
129
    }
130
131
    /**
132
     * @return bool
133
     */
134
    public function hasPreviousDefinition()
135
    {
136
        return $this->hasParent(self::class);
137
    }
138
139
    /**
140
     * @return StepDefinition
141
     * @throws EntryNotFoundException
142
     */
143
    public function getPreviousDefinition()
144
    {
145
        if (false === $this->hasPreviousDefinition()) {
146
            throw EntryNotFoundException::previousDefinitionNotFound($this);
147
        }
148
149
        /** @var StepDefinition $previousStepDefinition */
150
        $previousStepDefinition = $this->getFirstParent(self::class);
151
152
        return $previousStepDefinition;
153
    }
154
155
    /**
156
     * @return DivergenceStepDefinition[]
157
     */
158
    public function getDivergenceSteps()
159
    {
160
        return $this->divergence;
161
    }
162
163
    /**
164
     * @return bool
165
     */
166
    public function hasDivergence()
167
    {
168
        return false === empty($this->divergence);
169
    }
170
}
171