Completed
Push — feature/improve-form-object-ha... ( 8bb971 )
by Romain
10:18
created

getConfigurationObjectFromCache()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 3
eloc 12
nc 3
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->getGlobalConfigurationValidationResult();
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 getGlobalConfigurationValidationResult()
106
    {
107
        $result = new Result;
108
        $formzConfigurationValidationResult = ConfigurationFactory::get()
109
            ->getFormzConfiguration()
110
            ->getValidationResult();
111
112
        $result->merge($formzConfigurationValidationResult);
113
114
        if (false === $this->configurationIsValid) {
115
            $propertyName = 'forms.' . $this->static->getClassName();
116
            $formValidationResult = $this->getConfigurationObject()->getValidationResult();
117
118
            $result->forProperty($propertyName)->merge($formValidationResult);
119
        }
120
121
        return $result;
122
    }
123
124
    /**
125
     * @return ConfigurationObjectInstance
126
     */
127
    protected function getConfigurationObjectFromCache()
128
    {
129
        $cacheInstance = $this->getCacheInstance();
130
        $cacheIdentifier = 'configuration-' . $this->static->getObjectHash();
131
132
        if ($cacheInstance->has($cacheIdentifier)) {
133
            $configurationObject = $cacheInstance->get($cacheIdentifier);
134
135
            $this->configurationIsValid = true;
136
        } else {
137
            $configurationArray = $this->sanitizeConfiguration($this->configurationArray);
138
            $configurationObject = $this->getConfigurationObjectInstance($configurationArray);
139
140
            if (false === $configurationObject->getValidationResult()->hasErrors()) {
141
                $cacheInstance->set($cacheIdentifier, $configurationObject);
142
            }
143
        }
144
145
        return $configurationObject;
146
    }
147
148
    /**
149
     * This function will clean the configuration array by removing useless data
150
     * and updating needed ones.
151
     *
152
     * @param array $configuration
153
     * @return array
154
     */
155
    protected function sanitizeConfiguration(array $configuration)
156
    {
157
        // Removing configuration of fields which do not exist for this form.
158
        $sanitizedFieldsConfiguration = [];
159
        $fieldsConfiguration = (isset($configuration['fields']))
160
            ? $configuration['fields']
161
            : [];
162
163
        foreach ($this->static->getProperties() as $property) {
164
            $sanitizedFieldsConfiguration[$property] = (isset($fieldsConfiguration[$property]))
165
                ? $fieldsConfiguration[$property]
166
                : [];
167
        }
168
169
        $configuration['fields'] = $sanitizedFieldsConfiguration;
170
171
        return $configuration;
172
    }
173
174
    /**
175
     * @param array $configuration
176
     * @return ConfigurationObjectInstance
177
     */
178
    protected function getConfigurationObjectInstance(array $configuration)
179
    {
180
        return ConfigurationObjectFactory::getInstance()->get(Form::class, $configuration);
181
    }
182
183
    /**
184
     * @return FrontendInterface
185
     */
186
    protected function getCacheInstance()
187
    {
188
        return CacheService::get()->getCacheInstance();
189
    }
190
191
    /**
192
     * @return array
193
     */
194
    public function __sleep()
195
    {
196
        return ['static', 'configurationArray'];
197
    }
198
}
199