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

FormStepPersistence   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 20
lcom 2
cbo 2
dl 0
loc 186
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A markStepAsValidated() 0 7 1
A removeStep() 0 8 1
A stepWasValidated() 0 4 1
A setStepLevel() 0 4 1
A hasStepIdentifierAtLevel() 0 4 1
A getStepIdentifierAtLevel() 0 8 2
A addStepFormValues() 0 4 1
A hasStepFormValues() 0 4 1
A getStepFormValues() 0 8 2
A getMergedFormValues() 0 11 2
A addValidatedFields() 0 4 1
A hasData() 0 5 2
A getObjectHash() 0 4 1
A refreshObjectHash() 0 5 1
A resetValidationData() 0 5 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\FormObject\Service\Step;
15
16
use Romm\Formz\Form\Definition\Step\Step\Step;
17
use Romm\Formz\Form\Definition\Step\Step\StepDefinition;
18
19
/**
20
 * This object is stored in a form metadata, and contains important information
21
 * about the form steps:
22
 * - Which steps were already validated;
23
 * - Form data that were submitted by the user at every step.
24
 *
25
 * Data consistency of this object is assured by the form object hash (which is
26
 * mainly calculated from the form configuration): if the hash changes (if the
27
 * form configuration changes), the steps that were validated are no longer
28
 * considered as valid, and will need to be validated again.
29
 */
30
class FormStepPersistence
31
{
32
    /**
33
     * @var string
34
     */
35
    protected $objectHash;
36
37
    /**
38
     * @var array
39
     */
40
    protected $validatedSteps = [];
41
42
    /**
43
     * @var array
44
     */
45
    protected $stepLevels = [];
46
47
    /**
48
     * @var array
49
     */
50
    protected $stepsFormValues = [];
51
52
    /**
53
     * @var array
54
     */
55
    protected $validatedFields = [];
56
57
    /**
58
     * @param string $configurationHash
59
     */
60
    public function __construct($configurationHash)
61
    {
62
        $this->objectHash = $configurationHash;
63
    }
64
65
    /**
66
     * @param StepDefinition $stepDefinition
67
     */
68
    public function markStepAsValidated(StepDefinition $stepDefinition)
69
    {
70
        $identifier = $stepDefinition->getStep()->getIdentifier();
71
72
        $this->validatedSteps[$identifier] = $identifier;
73
        $this->stepLevels[$stepDefinition->getStepLevel()] = $identifier;
74
    }
75
76
    /**
77
     * @param StepDefinition $stepDefinition
78
     */
79
    public function removeStep(StepDefinition $stepDefinition)
80
    {
81
        $identifier = $stepDefinition->getStep()->getIdentifier();
82
83
        unset($this->validatedSteps[$identifier]);
84
        unset($this->stepLevels[$stepDefinition->getStepLevel()]);
85
        unset($this->stepsFormValues[$identifier]);
86
    }
87
88
    /**
89
     * @param Step $step
90
     * @return bool
91
     */
92
    public function stepWasValidated(Step $step)
93
    {
94
        return in_array($step->getIdentifier(), $this->validatedSteps);
95
    }
96
97
    /**
98
     * @param StepDefinition $stepDefinition
99
     */
100
    public function setStepLevel(StepDefinition $stepDefinition)
101
    {
102
        $this->stepLevels[$stepDefinition->getStepLevel()] = $stepDefinition->getStep()->getIdentifier();
103
    }
104
105
    /**
106
     * @param int $level
107
     * @return bool
108
     */
109
    public function hasStepIdentifierAtLevel($level)
110
    {
111
        return isset($this->stepLevels[$level]);
112
    }
113
114
    /**
115
     * @param int $level
116
     * @return string
117
     */
118
    public function getStepIdentifierAtLevel($level)
119
    {
120
        if (false === $this->hasStepIdentifierAtLevel($level)) {
121
            throw new \Exception('todo'); // @todo
122
        }
123
124
        return $this->stepLevels[$level];
125
    }
126
127
    /**
128
     * @param StepDefinition $stepDefinition
129
     * @param array          $formValues
130
     */
131
    public function addStepFormValues(StepDefinition $stepDefinition, array $formValues)
132
    {
133
        $this->stepsFormValues[$stepDefinition->getStep()->getIdentifier()] = $formValues;
134
    }
135
136
    /**
137
     * @param StepDefinition $stepDefinition
138
     * @return bool
139
     */
140
    public function hasStepFormValues(StepDefinition $stepDefinition)
141
    {
142
        return true === array_key_exists($stepDefinition->getStep()->getIdentifier(), $this->stepsFormValues);
143
    }
144
145
    /**
146
     * @param StepDefinition $stepDefinition
147
     * @return array
148
     */
149
    public function getStepFormValues(StepDefinition $stepDefinition)
150
    {
151
        if (false === $this->hasStepFormValues($stepDefinition)) {
152
            throw new \Exception('todo'); // @todo
153
        }
154
155
        return $this->stepsFormValues[$stepDefinition->getStep()->getIdentifier()];
156
    }
157
158
    /**
159
     * @return array
160
     */
161
    public function getMergedFormValues()
162
    {
163
        $formValues = [];
164
165
        foreach ($this->stepsFormValues as $stepFormValues) {
166
            unset($stepFormValues['__identity']);
167
            $formValues = array_merge($formValues, $stepFormValues);
168
        }
169
170
        return $formValues;
171
    }
172
173
    /**
174
     * @param array $validatedFields
175
     */
176
    public function addValidatedFields(array $validatedFields)
177
    {
178
        $this->validatedFields = array_merge($this->validatedFields, $validatedFields);
179
    }
180
181
    /**
182
     * @return bool
183
     */
184
    public function hasData()
185
    {
186
        return false === empty($this->validatedSteps)
187
            && false === empty($this->stepsFormValues);
188
    }
189
190
    /**
191
     * @return string
192
     */
193
    public function getObjectHash()
194
    {
195
        return $this->objectHash;
196
    }
197
198
    /**
199
     * @param string $hash
200
     */
201
    public function refreshObjectHash($hash)
202
    {
203
        $this->objectHash = $hash;
204
        $this->resetValidationData();
205
    }
206
207
    /**
208
     * @todo
209
     */
210
    public function resetValidationData()
211
    {
212
        $this->validatedSteps = [];
213
        $this->stepLevels = [];
214
    }
215
}
216