Completed
Push — master ( 3e70d8...0fa5e9 )
by
unknown
03:54
created

AssetHandlerFactory::getAssetHandler()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 13
nc 4
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\AssetHandler;
15
16
use Romm\Formz\Exceptions\ClassNotFoundException;
17
use Romm\Formz\Exceptions\InvalidArgumentTypeException;
18
use Romm\Formz\Form\FormObject;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
21
22
/**
23
 * A factory used to get instances of asset handlers. This is useful because it
24
 * will automatically manage cached instances of asset handlers for a given
25
 * form.
26
 *
27
 * The factory is here to store all information which can be useful to the asset
28
 * handlers.
29
 */
30
class AssetHandlerFactory
31
{
32
33
    /**
34
     * Contains the instances of factory for every form/controller context.
35
     *
36
     * @var array
37
     */
38
    protected static $factoryInstances = [];
39
40
    /**
41
     * @var AbstractAssetHandler[]
42
     */
43
    protected $instances = [];
44
45
    /**
46
     * @var FormObject
47
     */
48
    protected $formObject = [];
49
50
    /**
51
     * @var ControllerContext
52
     */
53
    protected $controllerContext;
54
55
    /**
56
     * @param FormObject        $formObject Name of the form class.
57
     * @param ControllerContext $controllerContext
58
     * @throws \Exception
59
     */
60
    protected function __construct(FormObject $formObject, ControllerContext $controllerContext)
61
    {
62
        $this->formObject = $formObject;
63
        $this->controllerContext = $controllerContext;
64
    }
65
66
    /**
67
     * @param FormObject        $formObject Configuration of the form.
68
     * @param ControllerContext $controllerContext
69
     * @return AssetHandlerFactory
70
     */
71
    public static function get(FormObject $formObject, ControllerContext $controllerContext)
72
    {
73
        $hash = md5(spl_object_hash($formObject) . spl_object_hash($controllerContext));
74
75
        if (false === array_key_exists($hash, self::$factoryInstances)) {
76
            self::$factoryInstances[$hash] = new AssetHandlerFactory($formObject, $controllerContext);
77
        }
78
79
        return self::$factoryInstances[$hash];
80
    }
81
82
    /**
83
     * Return an instance of the wanted asset handler. Local storage is handled.
84
     *
85
     * @param string $className
86
     * @return AbstractAssetHandler
87
     * @throws ClassNotFoundException
88
     * @throws InvalidArgumentTypeException
89
     */
90
    public function getAssetHandler($className)
91
    {
92
        if (false === array_key_exists($className, $this->instances)) {
93
            if (false === class_exists($className)) {
94
                throw new ClassNotFoundException(
95
                    'Trying to get an asset handler with a wrong class name: "' . $className . '".',
96
                    1477468381
97
                );
98
            }
99
100
            $instance = GeneralUtility::makeInstance($className, $this);
101
102
            if (false === $instance instanceof AbstractAssetHandler) {
103
                throw new InvalidArgumentTypeException(
104
                    'The asset handler object must be an instance of "' . AbstractAssetHandler::class . '", current type: "' . get_class($instance) . '".',
105
                    1477468571
106
                );
107
            }
108
109
            $this->instances[$className] = $instance;
110
        }
111
112
        return $this->instances[$className];
113
    }
114
115
    /**
116
     * @return FormObject
117
     */
118
    public function getFormObject()
119
    {
120
        return $this->formObject;
121
    }
122
123
    /**
124
     * @return ControllerContext
125
     */
126
    public function getControllerContext()
127
    {
128
        return $this->controllerContext;
129
    }
130
}
131