Passed
Push — master ( 70f9f1...079f74 )
by Peter
02:21
created

AssetManager::renderImg()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Assets;
6
7
use AbterPhp\Framework\Assets\Factory\Minifier as MinifierFactory;
8
use MatthiasMullie\Minify\CSS as CssMinifier;
9
use MatthiasMullie\Minify\JS as JsMinifier;
10
11
/**
12
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
13
 */
14
class AssetManager
15
{
16
    const FILE_EXTENSION_CSS = '.css';
17
    const FILE_EXTENSION_JS  = '.js';
18
19
    const ERROR_EMPTY_GROUP_NAME = 'Group name must not be empty.';
20
21
    /** @var MinifierFactory */
22
    protected $minifierFactory;
23
24
    /** @var FileFinder */
25
    protected $fileFinder;
26
27
    /** @var CacheManager */
28
    protected $cacheManager;
29
30
    /** @var JsMinifier[] */
31
    protected $jsMinifiers = [];
32
33
    /** @var CssMinifier[] */
34
    protected $cssMinifiers = [];
35
36
    /**
37
     * AssetManager constructor.
38
     *
39
     * @param MinifierFactory $minifierFactory
40
     * @param FileFinder      $fileFinder
41
     * @param CacheManager    $cacheManager
42
     */
43
    public function __construct(MinifierFactory $minifierFactory, FileFinder $fileFinder, CacheManager $cacheManager)
44
    {
45
        $this->minifierFactory = $minifierFactory;
46
        $this->fileFinder      = $fileFinder;
47
        $this->cacheManager    = $cacheManager;
48
    }
49
50
    /**
51
     * @param string $groupName
52
     * @param string $rawPath
53
     *
54
     * @throws \League\Flysystem\FileNotFoundException
55
     */
56
    public function addCss(string $groupName, string $rawPath)
57
    {
58
        $content = $this->fileFinder->read($rawPath . static::FILE_EXTENSION_CSS, $groupName);
59
60
        $this->getCssMinifier($groupName)->add($content);
61
    }
62
63
    /**
64
     * @param string $groupName
65
     * @param string $rawPath
66
     *
67
     * @throws \League\Flysystem\FileNotFoundException
68
     */
69
    public function addJs(string $groupName, string $rawPath)
70
    {
71
        $content = $this->fileFinder->read($rawPath . static::FILE_EXTENSION_JS, $groupName);
72
73
        $this->getJsMinifier($groupName)->add($content);
74
    }
75
76
    /**
77
     * @param string $groupName
78
     * @param string $content
79
     */
80
    public function addCssContent(string $groupName, string $content)
81
    {
82
        $this->getCssMinifier($groupName)->add($content);
83
    }
84
85
    /**
86
     * @param string $groupName
87
     * @param string $content
88
     */
89
    public function addJsContent(string $groupName, string $content)
90
    {
91
        $this->getJsMinifier($groupName)->add($content);
92
    }
93
94
    /**
95
     * @param string $groupName
96
     *
97
     * @return string
98
     * @throws \League\Flysystem\FileExistsException
99
     */
100
    public function renderCss(string $groupName): string
101
    {
102
        $content   = $this->getCssMinifier($groupName)->minify();
103
        $cachePath = $groupName . static::FILE_EXTENSION_CSS;
104
105
        $this->cacheManager->write($cachePath, $content);
106
107
        return $content;
108
    }
109
110
    /**
111
     * @param string $groupName
112
     *
113
     * @return string
114
     * @throws \League\Flysystem\FileExistsException
115
     */
116
    public function renderJs(string $groupName): string
117
    {
118
        $content   = $this->getJsMinifier($groupName)->minify();
119
        $cachePath = $groupName . static::FILE_EXTENSION_JS;
120
121
        $this->cacheManager->write($cachePath, $content);
122
123
        return $content;
124
    }
125
126
    /**
127
     * @param string $cachePath
128
     *
129
     * @return string
130
     * @throws \League\Flysystem\FileExistsException
131
     */
132
    public function renderImg(string $cachePath): string
133
    {
134
        $content = $this->fileFinder->read($cachePath);
135
136
        $this->cacheManager->write($cachePath, $content);
0 ignored issues
show
Bug introduced by
It seems like $content can also be of type null; however, parameter $content of AbterPhp\Framework\Assets\CacheManager::write() 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

136
        $this->cacheManager->write($cachePath, /** @scrutinizer ignore-type */ $content);
Loading history...
137
138
        return $content;
139
    }
140
141
    /**
142
     * @param string $groupName
143
     *
144
     * @return string
145
     * @throws \League\Flysystem\FileExistsException
146
     * @throws \League\Flysystem\FileNotFoundException
147
     */
148
    public function ensureCssWebPath(string $groupName): string
149
    {
150
        $cachePath = $groupName . static::FILE_EXTENSION_CSS;
151
152
        if (!$this->cacheManager->has($cachePath)) {
153
            $this->renderCss($groupName);
154
        }
155
156
        return $this->cacheManager->getWebPath($cachePath);
157
    }
158
159
    /**
160
     * @param string $groupName
161
     *
162
     * @return string
163
     * @throws \League\Flysystem\FileExistsException
164
     * @throws \League\Flysystem\FileNotFoundException
165
     */
166
    public function ensureJsWebPath(string $groupName): string
167
    {
168
        $cachePath = $groupName . static::FILE_EXTENSION_JS;
169
170
        if (!$this->cacheManager->has($cachePath)) {
171
            $this->renderJs($groupName);
172
        }
173
174
        return $this->cacheManager->getWebPath($cachePath);
175
    }
176
177
    /**
178
     * @param string $groupName
179
     *
180
     * @return string
181
     * @throws \League\Flysystem\FileExistsException
182
     * @throws \League\Flysystem\FileNotFoundException
183
     */
184
    public function ensureImgWebPath(string $cachePath): string
185
    {
186
        if (!$this->cacheManager->has($cachePath)) {
187
            $this->renderImg($cachePath);
188
        }
189
190
        return $this->cacheManager->getWebPath($cachePath);
191
    }
192
193
    /**
194
     * @param string $key
195
     *
196
     * @return CssMinifier
197
     */
198
    protected function getCssMinifier(string $key): CssMinifier
199
    {
200
        if (!array_key_exists($key, $this->cssMinifiers)) {
201
            $this->cssMinifiers[$key] = $this->minifierFactory->createCssMinifier();
202
        }
203
204
        return $this->cssMinifiers[$key];
205
    }
206
207
    /**
208
     * @param string $key
209
     *
210
     * @return JsMinifier
211
     */
212
    protected function getJsMinifier(string $key): JsMinifier
213
    {
214
        if (!array_key_exists($key, $this->jsMinifiers)) {
215
            $this->jsMinifiers[$key] = $this->minifierFactory->createJsMinifier();
216
        }
217
218
        return $this->jsMinifiers[$key];
219
    }
220
}
221