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