Completed
Push — unit-test-form-view-helper ( 21ce8b...04fc41 )
by Romain
04:37
created

ConditionTree::attachConditionProcessor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

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 3
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\Parser;
15
16
use Romm\Formz\Condition\Parser\Node\NodeInterface;
17
use Romm\Formz\Condition\Parser\Node\ProcessorDependencyAwareInterface;
18
use Romm\Formz\Condition\Processor\ConditionProcessor;
19
use Romm\Formz\Condition\Processor\DataObject\PhpConditionDataObject;
20
use Romm\Formz\Configuration\Form\Condition\Activation\ActivationInterface;
21
use TYPO3\CMS\Extbase\Error\Result;
22
23
/**
24
 * Tree built with instances of `NodeInterface` that represents a condition
25
 * instance.
26
 *
27
 * It can be used to get CSS, JavaScript or PHP results of the condition.
28
 */
29
class ConditionTree
30
{
31
    /**
32
     * @var NodeInterface
33
     */
34
    private $rootNode;
35
36
    /**
37
     * @var Result
38
     */
39
    private $validationResult;
40
41
    /**
42
     * @var ConditionProcessor
43
     */
44
    private $conditionProcessor;
45
46
    /**
47
     * @var bool
48
     */
49
    private $dependenciesWereInjected = false;
50
51
    /**
52
     * @param NodeInterface $rootNode
53
     * @param Result        $validationResult
54
     */
55
    public function __construct(NodeInterface $rootNode, Result $validationResult)
56
    {
57
        $this->rootNode = $rootNode;
58
        $this->validationResult = $validationResult;
59
60
        $this->rootNode->setTree($this);
61
    }
62
63
    /**
64
     * @param ConditionProcessor  $conditionProcessor
65
     * @param ActivationInterface $activation
66
     * @return $this
67
     */
68
    public function injectDependencies(ConditionProcessor $conditionProcessor, ActivationInterface $activation)
69
    {
70
        if (false === $this->dependenciesWereInjected) {
71
            $this->dependenciesWereInjected = true;
72
73
            $this->conditionProcessor = $conditionProcessor;
74
75
            // Looping on node to detect which ones have a dependency to the processor.
76
            $this->alongNodes(function (NodeInterface $node) use ($activation) {
77
                if ($node instanceof ProcessorDependencyAwareInterface) {
78
                    $node->injectProcessorDependencies($this->conditionProcessor, $activation);
79
                }
80
            });
81
        }
82
83
        return $this;
84
    }
85
86
    /**
87
     * Allows to go through all the nodes and sub-nodes of the tree. The
88
     * callback is called for every node, with a unique argument: the node
89
     * instance.
90
     *
91
     * @param callable $callback
92
     */
93
    public function alongNodes(callable $callback)
94
    {
95
        $this->rootNode->along($callback);
96
    }
97
98
    /**
99
     * @return array
100
     */
101
    public function getCssConditions()
102
    {
103
        return $this->rootNode->getCssResult();
104
    }
105
106
    /**
107
     * @return array
108
     */
109
    public function getJavaScriptConditions()
110
    {
111
        return $this->rootNode->getJavaScriptResult();
112
    }
113
114
    /**
115
     * @param PhpConditionDataObject $dataObject
116
     * @return bool
117
     */
118
    public function getPhpResult(PhpConditionDataObject $dataObject)
119
    {
120
        return $this->rootNode->getPhpResult($dataObject);
121
    }
122
123
    /**
124
     * @return Result
125
     */
126
    public function getValidationResult()
127
    {
128
        return $this->validationResult;
129
    }
130
131
    /**
132
     * @return ConditionProcessor
133
     */
134
    public function getConditionProcessor()
135
    {
136
        return $this->conditionProcessor;
137
    }
138
139
    /**
140
     * Resetting the dependencies injection.
141
     */
142
    public function __wakeup()
143
    {
144
        $this->dependenciesWereInjected = false;
145
    }
146
}
147