Completed
Pull Request — master (#31)
by
unknown
02:00
created

AssetHandlerConnectorManager   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 205
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
wmc 17
lcom 2
cbo 6
dl 0
loc 205
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A get() 0 13 2
A includeDefaultAssets() 0 11 2
A getFormzGeneratedFilePath() 0 9 2
A createFileInTemporaryDirectory() 0 13 2
A fileExists() 0 4 1
A writeTemporaryFile() 0 6 1
A getPageRenderer() 0 4 1
A getAssetHandlerFactory() 0 4 1
A getAssetHandlerConnectorStates() 0 4 1
A getJavaScriptAssetHandlerConnector() 0 4 1
A getCssAssetHandlerConnector() 0 4 1
A injectAssetHandlerConnectorStates() 0 4 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\Connector;
15
16
use Romm\Formz\AssetHandler\AssetHandlerFactory;
17
use Romm\Formz\Core\Core;
18
use TYPO3\CMS\Core\Page\PageRenderer;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21
/**
22
 * This manager is used to create instances of connectors, which will be used to
23
 * gather every asset used for a form, mainly JavaScript and CSS code.
24
 */
25
class AssetHandlerConnectorManager
26
{
27
    /**
28
     * @var AssetHandlerConnectorManager[]
29
     */
30
    private static $instances = [];
31
32
    /**
33
     * @var PageRenderer
34
     */
35
    private $pageRenderer;
36
37
    /**
38
     * @var AssetHandlerFactory
39
     */
40
    private $assetHandlerFactory;
41
42
    /**
43
     * @var JavaScriptAssetHandlerConnector
44
     */
45
    private $javaScriptAssetHandlerConnector;
46
47
    /**
48
     * @var CssAssetHandlerConnector
49
     */
50
    private $cssAssetHandlerConnector;
51
52
    /**
53
     * @var AssetHandlerConnectorStates
54
     */
55
    private $assetHandlerConnectorStates;
56
57
    /**
58
     * @param PageRenderer        $pageRenderer
59
     * @param AssetHandlerFactory $assetHandlerFactory
60
     */
61
    public function __construct(PageRenderer $pageRenderer, AssetHandlerFactory $assetHandlerFactory)
62
    {
63
        $this->pageRenderer = $pageRenderer;
64
        $this->assetHandlerFactory = $assetHandlerFactory;
65
66
        /** @noinspection PhpMethodParametersCountMismatchInspection */
67
        $this->cssAssetHandlerConnector = Core::get()
68
            ->getObjectManager()
69
            ->get(CssAssetHandlerConnector::class, $this);
70
71
        /** @noinspection PhpMethodParametersCountMismatchInspection */
72
        $this->javaScriptAssetHandlerConnector = Core::get()
73
            ->getObjectManager()
74
            ->get(JavaScriptAssetHandlerConnector::class, $this);
75
    }
76
77
    /**
78
     * @param PageRenderer        $pageRenderer
79
     * @param AssetHandlerFactory $assetHandlerFactory
80
     * @return AssetHandlerConnectorManager
81
     */
82
    public static function get(PageRenderer $pageRenderer, AssetHandlerFactory $assetHandlerFactory)
83
    {
84
        $hash = sha1(spl_object_hash($pageRenderer) . spl_object_hash($assetHandlerFactory));
85
86
        if (false === isset(self::$instances[$hash])) {
87
            /** @noinspection PhpMethodParametersCountMismatchInspection */
88
            self::$instances[$hash] = Core::get()
89
                ->getObjectManager()
90
                ->get(self::class, $pageRenderer, $assetHandlerFactory);
91
        }
92
93
        return self::$instances[$hash];
94
    }
95
96
    /**
97
     * Will take care of including internal Formz JavaScript and CSS files. They
98
     * will be included only once, even if the view helper is used several times
99
     * in the same page.
100
     *
101
     * @return $this
102
     */
103
    public function includeDefaultAssets()
104
    {
105
        if (false === $this->assetHandlerConnectorStates->defaultAssetsWereIncluded()) {
106
            $this->assetHandlerConnectorStates->markDefaultAssetsAsIncluded();
107
108
            $this->getJavaScriptAssetHandlerConnector()->includeDefaultJavaScriptFiles();
109
            $this->getCssAssetHandlerConnector()->includeDefaultCssFiles();
110
        }
111
112
        return $this;
113
    }
114
115
    /**
116
     * Returns a file name based on the form object class name.
117
     *
118
     * @param string $prefix
119
     * @return string
120
     */
121
    public function getFormzGeneratedFilePath($prefix = '')
122
    {
123
        $formObject = $this->assetHandlerFactory->getFormObject();
124
        $prefix = (false === empty($prefix))
125
            ? $prefix . '-'
126
            : '';
127
128
        return Core::GENERATED_FILES_PATH . Core::get()->getCacheIdentifier('formz-' . $prefix, $formObject->getClassName() . '-' . $formObject->getName());
129
    }
130
131
    /**
132
     * This function will check if the file at the given path exists. If it does
133
     * not, the callback is called to get the content of the file, which is put
134
     * in the created file.
135
     *
136
     * A boolean is returned: if the file did not exist, and it was created
137
     * without error, `true` is returned. Otherwise, `false` is returned.
138
     *
139
     * @param string   $relativePath
140
     * @param callable $callback
141
     * @return bool
142
     */
143
    public function createFileInTemporaryDirectory($relativePath, callable $callback)
144
    {
145
        $result = false;
146
        $absolutePath = GeneralUtility::getFileAbsFileName($relativePath);
147
148
        if (false === $this->fileExists($absolutePath)) {
149
            $content = call_user_func($callback);
150
151
            $result = $this->writeTemporaryFile($absolutePath, $content);
152
        }
153
154
        return $result;
155
    }
156
157
    /**
158
     * This function is mocked in unit tests.
159
     *
160
     * @param string $absolutePath
161
     * @return bool
162
     */
163
    protected function fileExists($absolutePath)
164
    {
165
        return file_exists($absolutePath);
166
    }
167
168
    /**
169
     * This function is mocked in unit tests.
170
     *
171
     * @param string $absolutePath
172
     * @param string $content
173
     * @return bool
174
     */
175
    protected function writeTemporaryFile($absolutePath, $content)
176
    {
177
        $result = GeneralUtility::writeFileToTypo3tempDir($absolutePath, $content);
178
179
        return (null === $result);
180
    }
181
182
    /**
183
     * @return PageRenderer
184
     */
185
    public function getPageRenderer()
186
    {
187
        return $this->pageRenderer;
188
    }
189
190
    /**
191
     * @return AssetHandlerFactory
192
     */
193
    public function getAssetHandlerFactory()
194
    {
195
        return $this->assetHandlerFactory;
196
    }
197
198
    /**
199
     * @return AssetHandlerConnectorStates
200
     */
201
    public function getAssetHandlerConnectorStates()
202
    {
203
        return $this->assetHandlerConnectorStates;
204
    }
205
206
    /**
207
     * @return JavaScriptAssetHandlerConnector
208
     */
209
    public function getJavaScriptAssetHandlerConnector()
210
    {
211
        return $this->javaScriptAssetHandlerConnector;
212
    }
213
214
    /**
215
     * @return CssAssetHandlerConnector
216
     */
217
    public function getCssAssetHandlerConnector()
218
    {
219
        return $this->cssAssetHandlerConnector;
220
    }
221
222
    /**
223
     * @param AssetHandlerConnectorStates $assetHandlerConnectorStates
224
     */
225
    public function injectAssetHandlerConnectorStates(AssetHandlerConnectorStates $assetHandlerConnectorStates)
226
    {
227
        $this->assetHandlerConnectorStates = $assetHandlerConnectorStates;
228
    }
229
}
230