Completed
Push — middleware-wip ( ffd957...48fd00 )
by Romain
02:53
created

FormObjectConfiguration   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 218
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 23
c 0
b 0
f 0
lcom 1
cbo 7
dl 0
loc 218
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getConfigurationObject() 0 11 3
A getConfigurationValidationResult() 0 11 3
A refreshConfigurationValidationResult() 0 14 1
A validateFormConfiguration() 0 12 2
A getConfigurationObjectFromCache() 0 17 3
A buildConfigurationObject() 0 4 1
A sanitizeConfiguration() 0 18 4
A getConfigurationObjectInstance() 0 4 1
A getCacheInstance() 0 4 1
A injectConfigurationFactory() 0 4 1
A __sleep() 0 4 1
A __wakeup() 0 7 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;
15
16
use Romm\ConfigurationObject\ConfigurationObjectFactory;
17
use Romm\ConfigurationObject\ConfigurationObjectInstance;
18
use Romm\Formz\Configuration\ConfigurationFactory;
19
use Romm\Formz\Configuration\Form\Form;
20
use Romm\Formz\Core\Core;
21
use Romm\Formz\Form\FormObject\FormObjectStatic;
22
use Romm\Formz\Service\CacheService;
23
use Romm\Formz\Validation\Validator\Internal\FormConfigurationValidator;
24
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
25
use TYPO3\CMS\Extbase\Error\Result;
26
27
class FormObjectConfiguration
28
{
29
    /**
30
     * @var FormObjectStatic
31
     */
32
    protected $formObject;
33
34
    /**
35
     * Contains the form configuration.
36
     *
37
     * @var array
38
     */
39
    protected $configurationArray = [];
40
41
    /**
42
     * Contains the form configuration object, which was created from the
43
     * configuration array.
44
     *
45
     * @var ConfigurationObjectInstance
46
     */
47
    protected $configurationObject;
48
49
    /**
50
     * @var Result
51
     */
52
    protected $configurationValidationResult;
53
54
    /**
55
     * @var string
56
     */
57
    protected $lastConfigurationHash;
58
59
    /**
60
     * @var ConfigurationFactory
61
     */
62
    protected $configurationFactory;
63
64
    /**
65
     * @param FormObjectStatic $formObject
66
     * @param array            $configurationArray
67
     */
68
    public function __construct(FormObjectStatic $formObject, array $configurationArray)
69
    {
70
        $this->formObject = $formObject;
71
        $this->configurationArray = $configurationArray;
72
    }
73
74
    /**
75
     * Returns an instance of configuration object. Checks if it was previously
76
     * stored in cache, otherwise it is created from scratch.
77
     *
78
     * @return ConfigurationObjectInstance
79
     */
80
    public function getConfigurationObject()
81
    {
82
        if (null === $this->configurationObject
83
            || $this->lastConfigurationHash !== $this->formObject->getObjectHash()
84
        ) {
85
            $this->lastConfigurationHash = $this->formObject->getObjectHash();
86
            $this->configurationObject = $this->getConfigurationObjectFromCache();
87
        }
88
89
        return $this->configurationObject;
90
    }
91
92
    /**
93
     * This function will merge and return the validation results of both the
94
     * global FormZ configuration object, and this form configuration object.
95
     *
96
     * @return Result
97
     */
98
    public function getConfigurationValidationResult()
99
    {
100
        if (null === $this->configurationValidationResult
101
            || $this->lastConfigurationHash !== $this->formObject->getObjectHash()
102
        ) {
103
            $configurationObject = $this->getConfigurationObject();
104
            $this->configurationValidationResult = $this->refreshConfigurationValidationResult($configurationObject);
105
        }
106
107
        return $this->configurationValidationResult;
108
    }
109
110
    /**
111
     * Resets the validation result and merges it with the global FormZ
112
     * configuration.
113
     *
114
     * @param ConfigurationObjectInstance $configurationObject
115
     * @return Result
116
     */
117
    protected function refreshConfigurationValidationResult(ConfigurationObjectInstance $configurationObject)
118
    {
119
        $result = new Result;
120
        $formzConfigurationValidationResult = $this->configurationFactory
121
            ->getFormzConfiguration()
122
            ->getValidationResult();
123
124
        $result->merge($formzConfigurationValidationResult);
125
126
        $result->forProperty('forms.' . $this->formObject->getClassName())
127
            ->merge($this->validateFormConfiguration($configurationObject));
128
129
        return $result;
130
    }
131
132
    /**
133
     * @param ConfigurationObjectInstance $configurationObject
134
     * @return Result
135
     */
136
    protected function validateFormConfiguration(ConfigurationObjectInstance $configurationObject)
137
    {
138
        $baseResult = $configurationObject->getValidationResult();
139
140
        if (false === $baseResult->hasErrors()) {
141
            $validator = new FormConfigurationValidator;
142
            $extendedResult = $validator->validate($configurationObject->getObject(true));
143
            $baseResult->merge($extendedResult);
144
        }
145
146
        return $baseResult;
147
    }
148
149
    /**
150
     * @return ConfigurationObjectInstance
151
     */
152
    protected function getConfigurationObjectFromCache()
153
    {
154
        $cacheInstance = $this->getCacheInstance();
155
        $cacheIdentifier = 'configuration-' . $this->formObject->getObjectHash();
156
157
        if ($cacheInstance->has($cacheIdentifier)) {
158
            $configurationObject = $cacheInstance->get($cacheIdentifier);
159
        } else {
160
            $configurationObject = $this->buildConfigurationObject();
161
162
            if (false === $configurationObject->getValidationResult()->hasErrors()) {
163
                $cacheInstance->set($cacheIdentifier, $configurationObject);
164
            }
165
        }
166
167
        return $configurationObject;
168
    }
169
170
    /**
171
     * @return ConfigurationObjectInstance
172
     */
173
    protected function buildConfigurationObject()
174
    {
175
        return $this->getConfigurationObjectInstance($this->sanitizeConfiguration($this->configurationArray));
176
    }
177
178
    /**
179
     * This function will clean the configuration array by removing useless data
180
     * and updating needed ones.
181
     *
182
     * @param array $configuration
183
     * @return array
184
     */
185
    protected function sanitizeConfiguration(array $configuration)
186
    {
187
        // Removing configuration of fields which do not exist for this form.
188
        $sanitizedFieldsConfiguration = [];
189
        $fieldsConfiguration = (isset($configuration['fields']))
190
            ? $configuration['fields']
191
            : [];
192
193
        foreach ($this->formObject->getProperties() as $property) {
194
            $sanitizedFieldsConfiguration[$property] = (isset($fieldsConfiguration[$property]))
195
                ? $fieldsConfiguration[$property]
196
                : [];
197
        }
198
199
        $configuration['fields'] = $sanitizedFieldsConfiguration;
200
201
        return $configuration;
202
    }
203
204
    /**
205
     * @param array $configuration
206
     * @return ConfigurationObjectInstance
207
     */
208
    protected function getConfigurationObjectInstance(array $configuration)
209
    {
210
        return ConfigurationObjectFactory::getInstance()->get(Form::class, $configuration);
211
    }
212
213
    /**
214
     * @return FrontendInterface
215
     */
216
    protected function getCacheInstance()
217
    {
218
        return CacheService::get()->getCacheInstance();
219
    }
220
221
    /**
222
     * @param ConfigurationFactory $configurationFactory
223
     */
224
    public function injectConfigurationFactory(ConfigurationFactory $configurationFactory)
225
    {
226
        $this->configurationFactory = $configurationFactory;
227
    }
228
229
    public function __sleep()
230
    {
231
        return ['formObject', 'configurationArray'];
232
    }
233
234
    /**
235
     * When this class is unserialized, the dependencies are injected.
236
     */
237
    public function __wakeup()
238
    {
239
        /** @var ConfigurationFactory $configurationFactory */
240
        $configurationFactory = Core::instantiate(ConfigurationFactory::class);
241
242
        $this->injectConfigurationFactory($configurationFactory);
243
    }
244
}
245