Completed
Push — feature/version-2 ( a353c5...4aa9b8 )
by Romain
19:54 queued 12:50
created

AbstractActivation   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 6
dl 0
loc 133
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getExpression() 0 4 1
A setExpression() 0 4 1
A getConditions() 0 14 2
A hasCondition() 0 6 1
A getCondition() 0 10 2
A addCondition() 0 4 1
A getRootObject() 0 4 1
A setRootObject() 0 4 1
A dataPreProcessor() 0 19 3
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\Definition\Field\Activation;
15
16
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessor;
17
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessorInterface;
18
use Romm\ConfigurationObject\Service\Items\Parents\ParentsTrait;
19
use Romm\Formz\Condition\Items\ConditionItemInterface;
20
use Romm\Formz\Configuration\AbstractFormzConfiguration;
21
use Romm\Formz\Exceptions\EntryNotFoundException;
22
use Romm\Formz\Form\Definition\FormDefinition;
23
use TYPO3\CMS\Core\Utility\ArrayUtility;
24
use TYPO3\CMS\Extbase\Error\Error;
25
26
abstract class AbstractActivation extends AbstractFormzConfiguration implements ActivationInterface, DataPreProcessorInterface
27
{
28
    use ParentsTrait;
29
30
    /**
31
     * @var string
32
     */
33
    protected $expression;
34
35
    /**
36
     * @var \Romm\Formz\Condition\Items\ConditionItemInterface[]
37
     * @mixedTypesResolver \Romm\Formz\Form\Definition\Condition\ConditionItemResolver
38
     */
39
    protected $conditions = [];
40
41
    /**
42
     * @var ActivationUsageInterface
43
     */
44
    private $rootObject;
45
46
    /**
47
     * @return string
48
     */
49
    public function getExpression()
50
    {
51
        return $this->expression;
52
    }
53
54
    /**
55
     * @param string $expression
56
     */
57
    public function setExpression($expression)
58
    {
59
        $this->expression = $expression;
60
    }
61
62
    /**
63
     * Will merge the conditions with the condition list of the parent form.
64
     *
65
     * @return ConditionItemInterface[]
66
     */
67
    public function getConditions()
68
    {
69
        $conditionList = $this->withFirstParent(
70
            FormDefinition::class,
71
            function (FormDefinition $formConfiguration) {
72
                return $formConfiguration->getConditionList();
73
            }
74
        );
75
76
        $conditionList = ($conditionList) ?: [];
77
        ArrayUtility::mergeRecursiveWithOverrule($conditionList, $this->conditions);
78
79
        return $conditionList;
80
    }
81
82
    /**
83
     * @param string $name Name of the condition.
84
     * @return bool
85
     */
86
    public function hasCondition($name)
87
    {
88
        $conditions = $this->getConditions();
89
90
        return true === isset($conditions[$name]);
91
    }
92
93
    /**
94
     * Return the condition with the given name.
95
     *
96
     * @param string $name Name of the item.
97
     * @return ConditionItemInterface
98
     * @throws EntryNotFoundException
99
     */
100
    public function getCondition($name)
101
    {
102
        if (false === $this->hasCondition($name)) {
103
            throw EntryNotFoundException::activationConditionNotFound($name);
104
        }
105
106
        $items = $this->getConditions();
107
108
        return $items[$name];
109
    }
110
111
    /**
112
     * @param string                 $name
113
     * @param ConditionItemInterface $condition
114
     */
115
    public function addCondition($name, ConditionItemInterface $condition)
116
    {
117
        $this->conditions[$name] = $condition;
118
    }
119
120
    /**
121
     * @return ActivationUsageInterface
122
     */
123
    public function getRootObject()
124
    {
125
        return $this->rootObject;
126
    }
127
128
    /**
129
     * @param ActivationUsageInterface $rootObject
130
     */
131
    public function setRootObject(ActivationUsageInterface $rootObject)
132
    {
133
        $this->rootObject = $rootObject;
134
    }
135
136
    /**
137
     * @param DataPreProcessor $processor
138
     */
139
    public static function dataPreProcessor(DataPreProcessor $processor)
140
    {
141
        $data = $processor->getData();
142
143
        if (isset($data['condition'])) {
144
            $error = new Error(
145
                'The property "condition" has been deprecated and renamed to "expression", please change your TypoScript configuration.',
146
                1488483802
147
            );
148
            $processor->addError($error);
149
        }
150
        if (isset($data['items'])) {
151
            $error = new Error(
152
                'The property "items" has been deprecated and renamed to "conditions", please change your TypoScript configuration.',
153
                1488531112
154
            );
155
            $processor->addError($error);
156
        }
157
    }
158
}
159