Completed
Push — feature/improve-form-object-ha... ( b02d23...a46062 )
by Romain
15:30
created

getMergedValidationResult()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
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\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\Form\FormObject\FormObjectStatic;
21
use Romm\Formz\Service\CacheService;
22
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
23
use TYPO3\CMS\Extbase\Error\Result;
24
25
class FormObjectConfiguration
26
{
27
    /**
28
     * @var FormObjectStatic
29
     */
30
    protected $static;
31
32
    /**
33
     * @var array
34
     */
35
    protected $configurationArray = [];
36
37
    /**
38
     * Contains the form configuration object, which was created from the
39
     * configuration array.
40
     *
41
     * @var ConfigurationObjectInstance
42
     */
43
    protected $configurationObject;
44
45
    /**
46
     * @var Result
47
     */
48
    protected $configurationValidationResult;
49
50
    /**
51
     * Flag to know if the form configuration is valid or not: if the
52
     * configuration is fetched from cache, then we know the configuration is
53
     * valid because it is saved in cache only when no error is found.
54
     *
55
     * @var bool
56
     */
57
    protected $configurationIsValid = false;
58
59
    /**
60
     * @param FormObjectStatic $static
61
     * @param array            $configurationArray
62
     */
63
    public function __construct(FormObjectStatic $static, array $configurationArray)
64
    {
65
        $this->static = $static;
66
        $this->configurationArray = $configurationArray;
67
    }
68
69
    /**
70
     * Returns an instance of configuration object. Checks if it was previously
71
     * stored in cache, otherwise it is created from scratch.
72
     *
73
     * @return ConfigurationObjectInstance
74
     */
75
    public function getConfigurationObject()
76
    {
77
        if (null === $this->configurationObject) {
78
            $this->configurationObject = $this->getConfigurationObjectFromCache();
79
        }
80
81
        return $this->configurationObject;
82
    }
83
84
    /**
85
     * This function will merge and return the validation results of both the
86
     * global FormZ configuration object, and this form configuration object.
87
     *
88
     * @return Result
89
     */
90
    public function getConfigurationValidationResult()
91
    {
92
        if (null === $this->configurationValidationResult) {
93
            $this->configurationValidationResult = $this->getMergedValidationResult();
94
        }
95
96
        return $this->configurationValidationResult;
97
    }
98
99
    /**
100
     * Resets the validation result and merges it with the global FormZ
101
     * configuration.
102
     *
103
     * @return Result
104
     */
105
    protected function getMergedValidationResult()
106
    {
107
        $globalConfigurationValidationResult = $this->getGlobalConfigurationValidationResult();
108
109
        $result = new Result;
110
        $result->merge($globalConfigurationValidationResult);
111
112
        /*
113
         * If the form configuration is not valid, we merge the form validation
114
         * result with the global validation result.
115
         */
116
        if (false === $this->configurationIsValid) {
117
            $propertyName = 'forms.' . $this->static->getClassName();
118
            $formValidationResult = $this->getConfigurationObject()->getValidationResult();
119
120
            $result->forProperty($propertyName)->merge($formValidationResult);
121
        }
122
123
        return $result;
124
    }
125
126
    /**
127
     * @return ConfigurationObjectInstance
128
     */
129
    protected function getConfigurationObjectFromCache()
130
    {
131
        $cacheInstance = $this->getCacheInstance();
132
        $cacheIdentifier = 'configuration-' . $this->static->getObjectHash();
133
134
        if ($cacheInstance->has($cacheIdentifier)) {
135
            $configurationObject = $cacheInstance->get($cacheIdentifier);
136
137
            $this->configurationIsValid = true;
138
        } else {
139
            $configurationArray = $this->sanitizeConfiguration($this->configurationArray);
140
            $configurationObject = $this->getConfigurationObjectInstance($configurationArray);
141
142
            if (false === $configurationObject->getValidationResult()->hasErrors()) {
143
                $cacheInstance->set($cacheIdentifier, $configurationObject);
144
            }
145
        }
146
147
        return $configurationObject;
148
    }
149
150
    /**
151
     * This function will clean the configuration array by removing useless data
152
     * and updating needed ones.
153
     *
154
     * @param array $configuration
155
     * @return array
156
     */
157
    protected function sanitizeConfiguration(array $configuration)
158
    {
159
        // Removing configuration of fields which do not exist for this form.
160
        $sanitizedFieldsConfiguration = [];
161
        $fieldsConfiguration = (isset($configuration['fields']))
162
            ? $configuration['fields']
163
            : [];
164
165
        foreach ($this->static->getProperties() as $property) {
166
            $sanitizedFieldsConfiguration[$property] = (isset($fieldsConfiguration[$property]))
167
                ? $fieldsConfiguration[$property]
168
                : [];
169
        }
170
171
        $configuration['fields'] = $sanitizedFieldsConfiguration;
172
173
        return $configuration;
174
    }
175
176
    /**
177
     * @param array $configuration
178
     * @return ConfigurationObjectInstance
179
     */
180
    protected function getConfigurationObjectInstance(array $configuration)
181
    {
182
        return ConfigurationObjectFactory::getInstance()->get(Form::class, $configuration);
183
    }
184
185
    /**
186
     * @return FrontendInterface
187
     */
188
    protected function getCacheInstance()
189
    {
190
        return CacheService::get()->getCacheInstance();
191
    }
192
193
    /**
194
     * @return Result
195
     */
196
    protected function getGlobalConfigurationValidationResult()
197
    {
198
        return ConfigurationFactory::get()->getFormzConfiguration()->getValidationResult();
199
    }
200
201
    /**
202
     * @return array
203
     */
204
    public function __sleep()
205
    {
206
        return ['static', 'configurationArray', 'properties'];
207
    }
208
}
209