ConditionParserFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 61
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 14 2
A getConditionTree() 0 14 2
A buildConditionTree() 0 4 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\Configuration\Form\Field\Activation\ActivationInterface;
17
use Romm\Formz\Service\CacheService;
18
use Romm\Formz\Service\HashService;
19
use Romm\Formz\Service\Traits\SelfInstantiateTrait;
20
use TYPO3\CMS\Core\SingletonInterface;
21
22
/**
23
 * Factory class allowing to parse a condition to get an instance of
24
 * `ConditionTree`.
25
 */
26
class ConditionParserFactory implements SingletonInterface
27
{
28
    use SelfInstantiateTrait;
29
30
    /**
31
     * @var ConditionTree[]
32
     */
33
    private $trees = [];
34
35
    /**
36
     * Will parse a condition expression, to build a tree containing one or
37
     * several nodes which represent the condition. See the class
38
     * `ConditionTree` for more information.
39
     *
40
     * @param ActivationInterface $activation
41
     * @return ConditionTree
42
     */
43
    public function parse(ActivationInterface $activation)
44
    {
45
        $hash = 'condition-tree-' .
46
            HashService::get()->getHash(serialize([
47
                $activation->getExpression(),
48
                $activation->getConditions()
49
            ]));
50
51
        if (false === array_key_exists($hash, $this->trees)) {
52
            $this->trees[$hash] = $this->getConditionTree($hash, $activation);
53
        }
54
55
        return $this->trees[$hash];
56
    }
57
58
    /**
59
     * @param string              $cacheIdentifier
60
     * @param ActivationInterface $activation
61
     * @return ConditionTree
62
     */
63
    protected function getConditionTree($cacheIdentifier, ActivationInterface $activation)
64
    {
65
        $cacheInstance = CacheService::get()->getCacheInstance();
66
67
        /** @var ConditionTree $instance */
68
        if ($cacheInstance->has($cacheIdentifier)) {
69
            $instance = $cacheInstance->get($cacheIdentifier);
70
        } else {
71
            $instance = $this->buildConditionTree($activation);
72
            $cacheInstance->set($cacheIdentifier, $instance);
73
        }
74
75
        return $instance;
76
    }
77
78
    /**
79
     * @param ActivationInterface $activation
80
     * @return ConditionTree
81
     */
82
    protected function buildConditionTree(ActivationInterface $activation)
83
    {
84
        return ConditionParser::get()->parse($activation);
85
    }
86
}
87