Completed
Branch conditions-refactoring (7efd7c)
by Romain
01:49
created

ConditionProcessorFactory::get()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 27
rs 8.8571
c 1
b 0
f 0
cc 3
eloc 15
nc 3
nop 1
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
            $cacheInstance = Core::get()->getCacheInstance();
56
57
            /** @var ConditionProcessor $instance */
58
            if ($cacheInstance->has($cacheIdentifier)) {
59
                $instance = $cacheInstance->get($cacheIdentifier);
60
            } else {
61
                /** @noinspection PhpMethodParametersCountMismatchInspection */
62
                $instance = Core::get()
63
                    ->getObjectManager()
64
                    ->get(ConditionProcessor::class, $formObject);
65
66
                $instance->calculateAllTrees();
67
                $instance->attachFormObject($formObject);
68
69
                $cacheInstance->set($cacheIdentifier, $instance);
70
            }
71
72
            $this->processorInstances[$cacheIdentifier] = $instance;
73
        }
74
75
        return $this->processorInstances[$cacheIdentifier];
76
    }
77
}
78