Completed
Push — unit-tests-conditions ( 8c637d...408609 )
by Romain
02:38
created

getNewConditionTreeFromActivation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 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\Configuration\Form\Condition\Activation\ActivationInterface;
21
use Romm\Formz\Configuration\Form\Field\Field;
22
use Romm\Formz\Configuration\Form\Field\Validation\Validation;
23
use Romm\Formz\Form\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 $validationsTrees = [];
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->getFieldName();
65
66
        if (false === array_key_exists($key, $this->fieldsTrees)) {
67
            $this->fieldsTrees[$key] = $this->getConditionTree($field->getActivation());
68
        }
69
70
        return $this->fieldsTrees[$key];
71
    }
72
73
    /**
74
     * Returns the condition tree for a given validation instance, giving access
75
     * to CSS, JavaScript and PHP transpiled results.
76
     *
77
     * @param Validation $validation
78
     * @return ConditionTree
79
     */
80
    public function getActivationConditionTreeForValidation(Validation $validation)
81
    {
82
        $key = $validation->getParentField()->getFieldName() . '->' . $validation->getValidationName();
83
84
        if (false === array_key_exists($key, $this->validationsTrees)) {
85
            $this->validationsTrees[$key] = $this->getConditionTree($validation->getActivation());
86
        }
87
88
        return $this->validationsTrees[$key];
89
    }
90
91
    /**
92
     * Function that will calculate all trees from fields and their validation
93
     * rules.
94
     *
95
     * This is useful to be able to store this instance in cache.
96
     */
97
    public function calculateAllTrees()
98
    {
99
        $fields = $this->formObject->getConfiguration()->getFields();
100
101
        foreach ($fields as $field) {
102
            $this->getActivationConditionTreeForField($field);
103
104
            foreach ($field->getValidation() as $validation) {
0 ignored issues
show
Bug introduced by
The expression $field->getValidation() of type object<Romm\Formz\Config...Validation\Validation>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
105
                $this->getActivationConditionTreeForValidation($validation);
106
            }
107
        }
108
    }
109
110
    /**
111
     * @param ActivationInterface $activation
112
     * @return ConditionTree
113
     */
114
    protected function getConditionTree(ActivationInterface $activation)
115
    {
116
        $tree = $this->getNewConditionTreeFromActivation($activation);
117
        $tree->alongNodes(function (NodeInterface $node) {
118
            $this->attachNodeJavaScriptFiles($node);
119
        });
120
121
        return $tree;
122
    }
123
124
    /**
125
     * @param ActivationInterface $activation
126
     * @return ConditionTree
127
     */
128
    protected function getNewConditionTreeFromActivation(ActivationInterface $activation)
129
    {
130
        return ConditionParserFactory::get()
131
            ->parse($activation)
132
            ->attachConditionProcessor($this);
133
    }
134
135
    /**
136
     * @param NodeInterface $node
137
     */
138
    protected function attachNodeJavaScriptFiles(NodeInterface $node)
139
    {
140
        if ($node instanceof ConditionNode) {
141
            $files = $node->getCondition()->getJavaScriptFiles();
142
143
            foreach ($files as $file) {
144
                if (false === in_array($file, $this->javaScriptFiles)) {
145
                    $this->javaScriptFiles[] = $file;
146
                }
147
            }
148
        }
149
    }
150
151
    /**
152
     * @param FormObject $formObject
153
     */
154
    public function attachFormObject(FormObject $formObject)
155
    {
156
        $this->formObject = $formObject;
157
    }
158
159
    /**
160
     * @return array
161
     */
162
    public function getJavaScriptFiles()
163
    {
164
        return $this->javaScriptFiles;
165
    }
166
167
    /**
168
     * @return array
169
     */
170
    public function __sleep()
171
    {
172
        return ['fieldsTrees', 'validationsTrees', 'javaScriptFiles'];
173
    }
174
}
175