Completed
Push — middleware-wip ( dac6b8...b0158b )
by Romain
05:23
created

FormObjectConfiguration::getConfigurationObjectFromCache()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 13
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\Formz\Configuration\ConfigurationFactory;
17
use Romm\Formz\Core\Container;
18
use Romm\Formz\Form\Definition\FormDefinition;
19
use Romm\Formz\Form\FormObject\Definition\FormDefinitionObject;
20
use Romm\Formz\Form\FormObject\FormObjectStatic;
21
use TYPO3\CMS\Extbase\Error\Result;
22
23
class FormObjectConfiguration
24
{
25
    /**
26
     * @var FormObjectStatic
27
     */
28
    protected $static;
29
30
    /**
31
     * @var FormDefinitionObject
32
     */
33
    protected $definition;
34
35
    /**
36
     * @var Result
37
     */
38
    protected $configurationValidationResult;
39
40
    /**
41
     * @param FormObjectStatic     $static
42
     * @param FormDefinitionObject $definition
43
     */
44
    public function __construct(FormObjectStatic $static, FormDefinitionObject $definition)
45
    {
46
        $this->static = $static;
47
        $this->definition = $definition;
48
    }
49
50
    /**
51
     * This function will merge and return the validation results of both the
52
     * global FormZ configuration object, and this form configuration object.
53
     *
54
     * @return Result
55
     */
56
    public function getConfigurationValidationResult()
57
    {
58
        if (null === $this->configurationValidationResult) {
59
            $this->configurationValidationResult = $this->getMergedValidationResult();
60
        }
61
62
        return $this->configurationValidationResult;
63
    }
64
65
    /**
66
     * Resets the validation result and merges it with the global FormZ
67
     * configuration.
68
     *
69
     * @return Result
70
     */
71
    protected function getMergedValidationResult()
72
    {
73
        $globalConfigurationValidationResult = $this->getGlobalConfigurationValidationResult();
74
75
        $result = new Result;
76
        $result->merge($globalConfigurationValidationResult);
77
78
        $propertyName = 'forms.' . $this->static->getClassName();
79
        $formValidationResult = $this->definition->getValidationResult();
80
81
        $result->forProperty($propertyName)->merge($formValidationResult);
82
83
        return $result;
84
    }
85
86
    /**
87
     * Middlewares of the form are stored in cache, so their dependencies must
88
     * be injected again when they are fetched from cache.
89
     */
90
    protected function injectDependenciesInConfiguration()
91
    {
92
        /** @var FormDefinition $formDefinition */
93
        $formDefinition = $this->definition->getObject(true);
94
95
        foreach ($formDefinition->getAllMiddlewares() as $middleware) {
96
            Container::get()->injectDependenciesInInstance($middleware);
97
        }
98
    }
99
100
    /**
101
     * @return Result
102
     */
103
    protected function getGlobalConfigurationValidationResult()
104
    {
105
        return ConfigurationFactory::get()->getFormzConfiguration()->getValidationResult();
106
    }
107
108
    /**
109
     * Some operations must be done when this object is unserialized.
110
     */
111
    public function __wakeup()
112
    {
113
        $this->injectDependenciesInConfiguration();
114
    }
115
}
116