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

ConditionProcessorFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 10 2
A getProcessorInstance() 0 21 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\Processor;
15
16
use Romm\Formz\Core\Core;
17
use Romm\Formz\Form\FormObject;
18
use Romm\Formz\Service\Traits\ExtendedFacadeInstanceTrait;
19
use TYPO3\CMS\Core\SingletonInterface;
20
21
class ConditionProcessorFactory implements SingletonInterface
22
{
23
    use ExtendedFacadeInstanceTrait {
24
        get as getInstance;
25
    }
26
27
    /**
28
     * @var ConditionProcessor[]
29
     */
30
    private $processorInstances = [];
31
32
    /**
33
     * @param FormObject $formObject
34
     * @return ConditionProcessor
35
     */
36
    public function get(FormObject $formObject)
37
    {
38
        $cacheIdentifier = 'condition-processor-' . $formObject->getHash();
39
40
        if (false === array_key_exists($cacheIdentifier, $this->processorInstances)) {
41
            $this->processorInstances[$cacheIdentifier] = $this->getProcessorInstance($cacheIdentifier, $formObject);
42
        }
43
44
        return $this->processorInstances[$cacheIdentifier];
45
    }
46
47
    /**
48
     * Will either fetch the processor instance from cache, using the given
49
     * identifier, or calculate it and store it in cache.
50
     *
51
     * @param string     $cacheIdentifier
52
     * @param FormObject $formObject
53
     * @return ConditionProcessor
54
     */
55
    protected function getProcessorInstance($cacheIdentifier, FormObject $formObject)
56
    {
57
        $cacheInstance = Core::get()->getCacheInstance();
58
59
        /** @var ConditionProcessor $instance */
60
        if ($cacheInstance->has($cacheIdentifier)) {
61
            $instance = $cacheInstance->get($cacheIdentifier);
62
        } else {
63
            /** @noinspection PhpMethodParametersCountMismatchInspection */
64
            $instance = Core::get()
65
                ->getObjectManager()
66
                ->get(ConditionProcessor::class, $formObject);
67
68
            $instance->calculateAllTrees();
69
            $instance->attachFormObject($formObject);
70
71
            $cacheInstance->set($cacheIdentifier, $instance);
72
        }
73
74
        return $instance;
75
    }
76
}
77