Completed
Branch conditions-refactoring (5a0a7e)
by Romain
02:00
created

ConditionProcessorFactory::getProcessorInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 2
eloc 12
nc 2
nop 2
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\Condition\Processor;
15
16
use Romm\Formz\Core\Core;
17
use Romm\Formz\Form\FormObject;
18
use TYPO3\CMS\Core\SingletonInterface;
19
20
class ConditionProcessorFactory implements SingletonInterface
21
{
22
    /**
23
     * @var ConditionProcessorFactory
24
     */
25
    private static $instance;
26
27
    /**
28
     * @var ConditionProcessor[]
29
     */
30
    private $processorInstances = [];
31
32
    /**
33
     * @return ConditionProcessorFactory
34
     */
35
    public static function getInstance()
36
    {
37
        if (null === self::$instance) {
38
            self::$instance = Core::get()
39
                ->getObjectManager()
40
                ->get(self::class);
41
        }
42
43
        return self::$instance;
44
    }
45
46
    /**
47
     * @param FormObject $formObject
48
     * @return ConditionProcessor
49
     */
50
    public function get(FormObject $formObject)
51
    {
52
        $cacheIdentifier = 'condition-processor-' . $formObject->getHash();
53
54
        if (false === array_key_exists($cacheIdentifier, $this->processorInstances)) {
55
            $this->processorInstances[$cacheIdentifier] = $this->getProcessorInstance($cacheIdentifier, $formObject);
56
        }
57
58
        return $this->processorInstances[$cacheIdentifier];
59
    }
60
61
    /**
62
     * Will either fetch the processor instance from cache, using the given
63
     * identifier, or calculate it and store it in cache.
64
     *
65
     * @param string     $cacheIdentifier
66
     * @param FormObject $formObject
67
     * @return ConditionProcessor
68
     */
69
    protected function getProcessorInstance($cacheIdentifier, FormObject $formObject)
70
    {
71
        $cacheInstance = Core::get()->getCacheInstance();
72
73
        /** @var ConditionProcessor $instance */
74
        if ($cacheInstance->has($cacheIdentifier)) {
75
            $instance = $cacheInstance->get($cacheIdentifier);
76
        } else {
77
            /** @noinspection PhpMethodParametersCountMismatchInspection */
78
            $instance = Core::get()
79
                ->getObjectManager()
80
                ->get(ConditionProcessor::class, $formObject);
81
82
            $instance->calculateAllTrees();
83
            $instance->attachFormObject($formObject);
84
85
            $cacheInstance->set($cacheIdentifier, $instance);
86
        }
87
88
        return $instance;
89
    }
90
}
91