Completed
Push — tmp-wip ( 51e3a7...57b785 )
by Romain
03:04
created

ConditionProcessor   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 8
dl 0
loc 160
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getActivationConditionTreeForField() 0 12 2
A getActivationConditionTreeForValidator() 0 12 2
A calculateAllTrees() 0 12 3
A getConditionTree() 0 9 1
A getNewConditionTreeFromActivation() 0 5 1
A attachNodeJavaScriptFiles() 0 12 4
A attachFormObject() 0 4 1
A getFormObject() 0 4 1
A getJavaScriptFiles() 0 4 1
A __sleep() 0 4 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\Condition\Processor;
15
16
use Romm\Formz\Condition\Parser\ConditionParserFactory;
17
use Romm\Formz\Condition\Parser\ConditionTree;
18
use Romm\Formz\Condition\Parser\Node\ConditionNode;
19
use Romm\Formz\Condition\Parser\Node\NodeInterface;
20
use Romm\Formz\Form\Definition\Field\Activation\ActivationInterface;
21
use Romm\Formz\Form\Definition\Field\Field;
22
use Romm\Formz\Form\Definition\Field\Validation\Validator;
23
use Romm\Formz\Form\FormObject\FormObject;
24
25
class ConditionProcessor
26
{
27
    /**
28
     * @var FormObject
29
     */
30
    private $formObject;
31
32
    /**
33
     * @var ConditionTree[]
34
     */
35
    private $fieldsTrees = [];
36
37
    /**
38
     * @var ConditionTree[]
39
     */
40
    private $conditionTrees = [];
41
42
    /**
43
     * @var array
44
     */
45
    private $javaScriptFiles = [];
46
47
    /**
48
     * @param FormObject $formObject
49
     */
50
    public function __construct(FormObject $formObject)
51
    {
52
        $this->attachFormObject($formObject);
53
    }
54
55
    /**
56
     * Returns the condition tree for a given field instance, giving access to
57
     * CSS, JavaScript and PHP transpiled results.
58
     *
59
     * @param Field $field
60
     * @return ConditionTree
61
     */
62
    public function getActivationConditionTreeForField(Field $field)
63
    {
64
        $key = $field->getName();
65
66
        if (false === array_key_exists($key, $this->fieldsTrees)) {
67
            $this->fieldsTrees[$key] = $this->getConditionTree($field->getActivation());
68
        }
69
70
        $this->fieldsTrees[$key]->injectDependencies($this, $field->getActivation());
71
72
        return $this->fieldsTrees[$key];
73
    }
74
75
    /**
76
     * Returns the condition tree for a given validator instance, giving access
77
     * to CSS, JavaScript and PHP transpiled results.
78
     *
79
     * @param Validator $validator
80
     * @return ConditionTree
81
     */
82
    public function getActivationConditionTreeForValidator(Validator $validator)
83
    {
84
        $key = $validator->getParentField()->getName() . '->' . $validator->getName();
85
86
        if (false === array_key_exists($key, $this->conditionTrees)) {
87
            $this->conditionTrees[$key] = $this->getConditionTree($validator->getActivation());
88
        }
89
90
        $this->conditionTrees[$key]->injectDependencies($this, $validator->getActivation());
91
92
        return $this->conditionTrees[$key];
93
    }
94
95
    /**
96
     * Function that will calculate all trees from fields and their validators.
97
     *
98
     * This is useful to be able to store this instance in cache.
99
     */
100
    public function calculateAllTrees()
101
    {
102
        $fields = $this->formObject->getDefinition()->getFields();
103
104
        foreach ($fields as $field) {
105
            $this->getActivationConditionTreeForField($field);
106
107
            foreach ($field->getValidators() as $validator) {
108
                $this->getActivationConditionTreeForValidator($validator);
109
            }
110
        }
111
    }
112
113
    /**
114
     * @param ActivationInterface $activation
115
     * @return ConditionTree
116
     */
117
    protected function getConditionTree(ActivationInterface $activation)
118
    {
119
        $tree = $this->getNewConditionTreeFromActivation($activation);
120
        $tree->alongNodes(function (NodeInterface $node) {
121
            $this->attachNodeJavaScriptFiles($node);
122
        });
123
124
        return $tree;
125
    }
126
127
    /**
128
     * @param ActivationInterface $activation
129
     * @return ConditionTree
130
     */
131
    protected function getNewConditionTreeFromActivation(ActivationInterface $activation)
132
    {
133
        return ConditionParserFactory::get()
134
            ->parse($activation);
135
    }
136
137
    /**
138
     * @param NodeInterface $node
139
     */
140
    protected function attachNodeJavaScriptFiles(NodeInterface $node)
141
    {
142
        if ($node instanceof ConditionNode) {
143
            $files = $node->getCondition()->getJavaScriptFiles();
144
145
            foreach ($files as $file) {
146
                if (false === in_array($file, $this->javaScriptFiles)) {
147
                    $this->javaScriptFiles[] = $file;
148
                }
149
            }
150
        }
151
    }
152
153
    /**
154
     * @param FormObject $formObject
155
     */
156
    public function attachFormObject(FormObject $formObject)
157
    {
158
        $this->formObject = $formObject;
159
    }
160
161
    /**
162
     * @return FormObject
163
     */
164
    public function getFormObject()
165
    {
166
        return $this->formObject;
167
    }
168
169
    /**
170
     * @return array
171
     */
172
    public function getJavaScriptFiles()
173
    {
174
        return $this->javaScriptFiles;
175
    }
176
177
    /**
178
     * @return array
179
     */
180
    public function __sleep()
181
    {
182
        return ['fieldsTrees', 'conditionTrees', 'javaScriptFiles'];
183
    }
184
}
185