Completed
Push — cleanup-service ( 87abfc...b0282b )
by Romain
05:55
created

ConditionParserFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

2 Methods

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