Completed
Branch conditions-refactoring (4b1dba)
by Romain
03:17
created

ConditionItemResolver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 35
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A getInstanceClassName() 0 19 4
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\Configuration\Form\Condition;
15
16
use Romm\ConfigurationObject\Service\Items\MixedTypes\MixedTypesResolver;
17
use Romm\Formz\Condition\ConditionFactory;
18
use Romm\Formz\Configuration\AbstractFormzConfiguration;
19
use Romm\ConfigurationObject\Service\Items\MixedTypes\MixedTypesInterface;
20
use TYPO3\CMS\Extbase\Error\Error;
21
22
abstract class ConditionItemResolver extends AbstractFormzConfiguration implements MixedTypesInterface
23
{
24
25
    /**
26
     * Because this class implements `MixedTypeInterface`, this function needs
27
     * to be implemented.
28
     *
29
     * It will allow to fill properly the properties of type
30
     * `AbstractConditionItem` by returning the real type of the condition.
31
     *
32
     * The property `type` must be present in the resolver data, and contain one
33
     * of the values of `AbstractConditionItem::$types`.
34
     *
35
     * @param MixedTypesResolver $resolver
36
     */
37
    final public static function getInstanceClassName(MixedTypesResolver $resolver)
38
    {
39
        $conditionFactory = ConditionFactory::get();
40
        $data = $resolver->getData();
41
        $conditionName = (true === is_array($data) && true === isset($data['type']))
42
            ? $data['type']
43
            : null;
44
45
        if (false === $conditionFactory->hasCondition($conditionName)) {
46
            $error = new Error(
47
                'No condition was found (type: "' . $data['type'] . '").',
48
                1471809847
49
            );
50
51
            $resolver->addError($error);
52
        } else {
53
            $resolver->setObjectType($conditionFactory->getCondition($conditionName));
54
        }
55
    }
56
}
57