Passed
Push — master ( 097691...c99bd6 )
by Peter
02:34
created

AssetManager::fixCss()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Assets;
6
7
use AbterPhp\Framework\Assets\CacheManager\ICacheManager;
8
use AbterPhp\Framework\Assets\Factory\Minifier as MinifierFactory;
9
use AbterPhp\Framework\Config\Routes;
10
use AbterPhp\Framework\Filesystem\IFileFinder;
11
use League\Flysystem\FileNotFoundException;
12
use MatthiasMullie\Minify\CSS as CssMinifier;
13
use MatthiasMullie\Minify\JS as JsMinifier;
14
15
/**
16
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
17
 */
18
class AssetManager
19
{
20
    protected const EXT_CSS = '.css';
21
    protected const EXT_JS  = '.js';
22
23
    /** @var MinifierFactory */
24
    protected $minifierFactory;
25
26
    /** @var IFileFinder */
27
    protected $fileFinder;
28
29
    /** @var ICacheManager */
30
    protected $cacheManager;
31
32
    /** @var JsMinifier[] */
33
    protected $jsMinifiers = [];
34
35
    /** @var CssMinifier[] */
36
    protected $cssMinifiers = [];
37
38
    /** @var UrlFixer */
39
    protected $urlFixer;
40
41
    /**
42
     * AssetManager constructor.
43
     *
44
     * @param MinifierFactory $minifierFactory
45
     * @param IFileFinder     $fileFinder
46
     * @param ICacheManager   $cacheManager
47
     * @param UrlFixer        $urlFixer
48
     */
49
    public function __construct(
50
        MinifierFactory $minifierFactory,
51
        IFileFinder $fileFinder,
52
        ICacheManager $cacheManager,
53
        UrlFixer $urlFixer
54
    ) {
55
        $this->minifierFactory = $minifierFactory;
56
        $this->fileFinder      = $fileFinder;
57
        $this->cacheManager    = $cacheManager;
58
        $this->urlFixer        = $urlFixer;
59
    }
60
61
    /**
62
     * @param string $groupName
63
     * @param string $rawPath
64
     *
65
     * @throws \League\Flysystem\FileNotFoundException
66
     */
67
    public function addCss(string $groupName, string $rawPath)
68
    {
69
        $fixedPath = $rawPath;
70
        if ($this->getExtension($rawPath) !== static:: EXT_CSS) {
71
            $fixedPath = $rawPath . static::EXT_CSS;
72
        }
73
74
        $content = $this->fileFinder->read($fixedPath, $groupName);
75
76
        $fixedContent = $this->urlFixer->fixCss($content, $fixedPath);
0 ignored issues
show
Bug introduced by
It seems like $content can also be of type null; however, parameter $content of AbterPhp\Framework\Assets\UrlFixer::fixCss() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
        $fixedContent = $this->urlFixer->fixCss(/** @scrutinizer ignore-type */ $content, $fixedPath);
Loading history...
77
78
        $this->getCssMinifier($groupName)->add($fixedContent);
79
    }
80
81
    /**
82
     * @param string $fixedPath
83
     * @param string $content
84
     *
85
     * @return string
86
     */
87
    protected function fixCss(string $fixedPath, string $content): string
0 ignored issues
show
Unused Code introduced by
The parameter $fixedPath is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

87
    protected function fixCss(/** @scrutinizer ignore-unused */ string $fixedPath, string $content): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
88
    {
89
        return $content;
90
    }
91
92
    /**
93
     * @param string $groupName
94
     * @param string $rawPath
95
     *
96
     * @throws \League\Flysystem\FileNotFoundException
97
     */
98
    public function addJs(string $groupName, string $rawPath)
99
    {
100
        $fixedPath = $rawPath;
101
        if ($this->getExtension($rawPath) !== static:: EXT_JS) {
102
            $fixedPath = $rawPath . static::EXT_JS;
103
        }
104
105
        $content = $this->fileFinder->read($fixedPath, $groupName);
106
107
        $fixedContent = $this->urlFixer->fixJs($content, $fixedPath);
0 ignored issues
show
Bug introduced by
It seems like $content can also be of type null; however, parameter $url of AbterPhp\Framework\Assets\UrlFixer::fixJs() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
        $fixedContent = $this->urlFixer->fixJs(/** @scrutinizer ignore-type */ $content, $fixedPath);
Loading history...
108
109
        $this->getJsMinifier($groupName)->add($fixedContent);
110
    }
111
112
    /**
113
     * @param string $path
114
     *
115
     * @return string
116
     */
117
    protected function getExtension(string $path): string
118
    {
119
        $dotrpos = strrpos($path, '.');
120
121
        if ($dotrpos === false) {
122
            return '';
123
        }
124
125
        return substr($path, $dotrpos);
126
    }
127
128
    /**
129
     * @param string $groupName
130
     * @param string $content
131
     */
132
    public function addCssContent(string $groupName, string $content)
133
    {
134
        $this->getCssMinifier($groupName)->add($content);
135
    }
136
137
    /**
138
     * @param string $groupName
139
     * @param string $content
140
     */
141
    public function addJsContent(string $groupName, string $content)
142
    {
143
        $this->getJsMinifier($groupName)->add($content);
144
    }
145
146
    /**
147
     * @param string $groupName
148
     *
149
     * @return string
150
     * @throws \League\Flysystem\FileExistsException
151
     */
152
    public function renderCss(string $groupName): string
153
    {
154
        $content   = $this->getCssMinifier($groupName)->minify();
155
        $cachePath = $groupName . static::EXT_CSS;
156
157
        $this->cacheManager->write($cachePath, $content);
158
159
        return $content;
160
    }
161
162
    /**
163
     * @param string $groupName
164
     *
165
     * @return string
166
     * @throws \League\Flysystem\FileExistsException
167
     */
168
    public function renderJs(string $groupName): string
169
    {
170
        $content   = $this->getJsMinifier($groupName)->minify();
171
        $cachePath = $groupName . static::EXT_JS;
172
173
        $this->cacheManager->write($cachePath, $content);
174
175
        return $content;
176
    }
177
178
    /**
179
     * @param string $cachePath
180
     *
181
     * @return string|null
182
     * @throws \League\Flysystem\FileExistsException
183
     * @throws \League\Flysystem\FileNotFoundException
184
     */
185
    public function renderRaw(string $cachePath): ?string
186
    {
187
        $content = $this->fileFinder->read($cachePath);
188
        if (null === $content) {
189
            return null;
190
        }
191
192
        $this->cacheManager->write($cachePath, $content);
193
194
        return $content;
195
    }
196
197
    /**
198
     * @param string $groupName
199
     *
200
     * @return string
201
     * @throws \League\Flysystem\FileExistsException
202
     * @throws \League\Flysystem\FileNotFoundException
203
     */
204
    public function ensureCssWebPath(string $groupName): string
205
    {
206
        $cachePath = $groupName . static::EXT_CSS;
207
208
        if (!$this->cacheManager->has($cachePath)) {
209
            $this->renderCss($groupName);
210
        }
211
212
        return $this->getWebPath($cachePath);
213
    }
214
215
    /**
216
     * @param string $groupName
217
     *
218
     * @return string
219
     * @throws \League\Flysystem\FileExistsException
220
     * @throws \League\Flysystem\FileNotFoundException
221
     */
222
    public function ensureJsWebPath(string $groupName): string
223
    {
224
        $cachePath = $groupName . static::EXT_JS;
225
226
        if (!$this->cacheManager->has($cachePath)) {
227
            $this->renderJs($groupName);
228
        }
229
230
        return $this->getWebPath($cachePath);
231
    }
232
233
    /**
234
     * @param string $cachePath
235
     *
236
     * @return string
237
     * @throws \League\Flysystem\FileExistsException
238
     * @throws \League\Flysystem\FileNotFoundException
239
     */
240
    public function ensureImgWebPath(string $cachePath): string
241
    {
242
        if (!$this->cacheManager->has($cachePath)) {
243
            if ($this->renderRaw($cachePath) === null) {
244
                throw new FileNotFoundException($cachePath);
245
            }
246
        }
247
248
        return $this->getWebPath($cachePath);
249
    }
250
251
    /**
252
     * @param string $cachePath
253
     *
254
     * @return string
255
     */
256
    protected function getWebPath(string $cachePath): string
257
    {
258
        $path = $this->cacheManager->getWebPath($cachePath);
259
        if (!$path) {
260
            return $path;
261
        }
262
263
        $cachePath = Routes::getCacheUrl();
264
        if (!$cachePath) {
265
            return $path;
266
        }
267
268
        return sprintf(
269
            '%s%s%s',
270
            $cachePath,
271
            DIRECTORY_SEPARATOR,
272
            ltrim($path, DIRECTORY_SEPARATOR)
273
        );
274
    }
275
276
    /**
277
     * @param string $key
278
     *
279
     * @return CssMinifier
280
     */
281
    protected function getCssMinifier(string $key): CssMinifier
282
    {
283
        if (!array_key_exists($key, $this->cssMinifiers)) {
284
            $this->cssMinifiers[$key] = $this->minifierFactory->createCssMinifier();
285
        }
286
287
        return $this->cssMinifiers[$key];
288
    }
289
290
    /**
291
     * @param string $key
292
     *
293
     * @return JsMinifier
294
     */
295
    protected function getJsMinifier(string $key): JsMinifier
296
    {
297
        if (!array_key_exists($key, $this->jsMinifiers)) {
298
            $this->jsMinifiers[$key] = $this->minifierFactory->createJsMinifier();
299
        }
300
301
        return $this->jsMinifiers[$key];
302
    }
303
}
304