Completed
Branch unit-test-asset-handler-connec... (029d08)
by Romain
01:55
created

ConditionProcessor::attachNodeJavaScriptFiles()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 4
nop 1
1
<?php
2
/*
3
 * 2016 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\ConditionTree;
17
use Romm\Formz\Condition\Parser\ConditionParserFactory;
18
use Romm\Formz\Condition\Parser\Node\ConditionNode;
19
use Romm\Formz\Condition\Parser\Node\NodeInterface;
20
use Romm\Formz\Configuration\Form\Field\Field;
21
use Romm\Formz\Configuration\Form\Field\Validation\Validation;
22
use Romm\Formz\Form\FormObject;
23
24
class ConditionProcessor
25
{
26
    /**
27
     * @var FormObject
28
     */
29
    private $formObject;
30
31
    /**
32
     * @var ConditionTree[]
33
     */
34
    private $fieldsTrees = [];
35
36
    /**
37
     * @var ConditionTree[]
38
     */
39
    private $validationsTrees = [];
40
41
    /**
42
     * @var array
43
     */
44
    private $javaScriptFiles = [];
45
46
    /**
47
     * @param FormObject $formObject
48
     */
49
    public function __construct(FormObject $formObject)
50
    {
51
        $this->attachFormObject($formObject);
52
    }
53
54
    /**
55
     * Returns the condition tree for a given field instance, giving access to
56
     * CSS, JavaScript and PHP transpiled results.
57
     *
58
     * @param Field $field
59
     * @return ConditionTree
60
     */
61
    public function getActivationConditionTreeForField(Field $field)
62
    {
63
        if (false === array_key_exists($field->getFieldName(), $this->fieldsTrees)) {
64
            $this->fieldsTrees[$field->getFieldName()] = ConditionParserFactory::get()
65
                ->parse($field->getActivation())
66
                ->attachConditionProcessor($this);
67
        }
68
69
        return $this->fieldsTrees[$field->getFieldName()];
70
    }
71
72
    /**
73
     * Returns the condition tree for a given validation instance, giving access
74
     * to CSS, JavaScript and PHP transpiled results.
75
     *
76
     * @param Validation $validation
77
     * @return ConditionTree
78
     */
79
    public function getActivationConditionTreeForValidation(Validation $validation)
80
    {
81
        $key = $validation->getParentField()->getFieldName() . '->' . $validation->getValidationName();
82
83
        if (false === array_key_exists($key, $this->validationsTrees)) {
84
            $this->validationsTrees[$key] = ConditionParserFactory::get()
85
                ->parse($validation->getActivation())
86
                ->attachConditionProcessor($this);
87
        }
88
89
        return $this->validationsTrees[$key];
90
    }
91
92
    /**
93
     * Function that will calculate all trees from fields and their validation
94
     * rules.
95
     *
96
     * This is useful to be able to store this instance in cache.
97
     */
98
    public function calculateAllTrees()
99
    {
100
        $fields = $this->formObject->getConfiguration()->getFields();
101
        foreach ($fields as $field) {
102
            $this->getActivationConditionTreeForField($field)
103
                ->alongNodes(function (NodeInterface $node) {
104
                    $this->attachNodeJavaScriptFiles($node);
105
                });
106
107
            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...
108
                $this->getActivationConditionTreeForValidation($validation)
109
                    ->alongNodes(function (NodeInterface $node) {
110
                        $this->attachNodeJavaScriptFiles($node);
111
                    });
112
            }
113
        }
114
    }
115
116
    /**
117
     * @param NodeInterface $node
118
     */
119
    protected function attachNodeJavaScriptFiles(NodeInterface $node)
120
    {
121
        if ($node instanceof ConditionNode) {
122
            $files = $node->getCondition()->getJavaScriptFiles();
123
124
            foreach ($files as $file) {
125
                if (false === in_array($file, $this->javaScriptFiles)) {
126
                    $this->javaScriptFiles[] = $file;
127
                }
128
            }
129
        }
130
    }
131
132
    /**
133
     * @param FormObject $formObject
134
     */
135
    public function attachFormObject(FormObject $formObject)
136
    {
137
        $this->formObject = $formObject;
138
    }
139
140
    /**
141
     * @return FormObject
142
     */
143
    public function getFormObject()
144
    {
145
        return $this->formObject;
146
    }
147
148
    /**
149
     * @return array
150
     */
151
    public function getJavaScriptFiles()
152
    {
153
        return $this->javaScriptFiles;
154
    }
155
156
    /**
157
     * @return array
158
     */
159
    public function __sleep()
160
    {
161
        return ['fieldsTrees', 'validationsTrees', 'javaScriptFiles'];
162
    }
163
}
164