Passed
Push — master ( a21940...8fc5ed )
by Peter
02:25
created

AssetManager::renderRaw()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 1
dl 0
loc 14
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 League\Flysystem\FileNotFoundException;
9
use MatthiasMullie\Minify\CSS as CssMinifier;
10
use MatthiasMullie\Minify\JS as JsMinifier;
11
12
/**
13
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
14
 */
15
class AssetManager
16
{
17
    const FILE_EXTENSION_CSS = '.css';
18
    const FILE_EXTENSION_JS  = '.js';
19
20
    const ERROR_EMPTY_GROUP_NAME = 'Group name must not be empty.';
21
22
    /** @var MinifierFactory */
23
    protected $minifierFactory;
24
25
    /** @var FileFinder */
26
    protected $fileFinder;
27
28
    /** @var CacheManager */
29
    protected $cacheManager;
30
31
    /** @var JsMinifier[] */
32
    protected $jsMinifiers = [];
33
34
    /** @var CssMinifier[] */
35
    protected $cssMinifiers = [];
36
37
    /**
38
     * AssetManager constructor.
39
     *
40
     * @param MinifierFactory $minifierFactory
41
     * @param FileFinder      $fileFinder
42
     * @param CacheManager    $cacheManager
43
     */
44
    public function __construct(MinifierFactory $minifierFactory, FileFinder $fileFinder, CacheManager $cacheManager)
45
    {
46
        $this->minifierFactory = $minifierFactory;
47
        $this->fileFinder      = $fileFinder;
48
        $this->cacheManager    = $cacheManager;
49
    }
50
51
    /**
52
     * @param string $groupName
53
     * @param string $rawPath
54
     *
55
     * @throws \League\Flysystem\FileNotFoundException
56
     */
57
    public function addCss(string $groupName, string $rawPath)
58
    {
59
        $content = $this->fileFinder->read($rawPath . static::FILE_EXTENSION_CSS, $groupName);
60
61
        $this->getCssMinifier($groupName)->add($content);
62
    }
63
64
    /**
65
     * @param string $groupName
66
     * @param string $rawPath
67
     *
68
     * @throws \League\Flysystem\FileNotFoundException
69
     */
70
    public function addJs(string $groupName, string $rawPath)
71
    {
72
        $content = $this->fileFinder->read($rawPath . static::FILE_EXTENSION_JS, $groupName);
73
74
        $this->getJsMinifier($groupName)->add($content);
75
    }
76
77
    /**
78
     * @param string $groupName
79
     * @param string $content
80
     */
81
    public function addCssContent(string $groupName, string $content)
82
    {
83
        $this->getCssMinifier($groupName)->add($content);
84
    }
85
86
    /**
87
     * @param string $groupName
88
     * @param string $content
89
     */
90
    public function addJsContent(string $groupName, string $content)
91
    {
92
        $this->getJsMinifier($groupName)->add($content);
93
    }
94
95
    /**
96
     * @param string $groupName
97
     *
98
     * @return string
99
     * @throws \League\Flysystem\FileExistsException
100
     */
101
    public function renderCss(string $groupName): string
102
    {
103
        $content   = $this->getCssMinifier($groupName)->minify();
104
        $cachePath = $groupName . static::FILE_EXTENSION_CSS;
105
106
        $this->cacheManager->write($cachePath, $content);
107
108
        return $content;
109
    }
110
111
    /**
112
     * @param string $groupName
113
     *
114
     * @return string
115
     * @throws \League\Flysystem\FileExistsException
116
     */
117
    public function renderJs(string $groupName): string
118
    {
119
        $content   = $this->getJsMinifier($groupName)->minify();
120
        $cachePath = $groupName . static::FILE_EXTENSION_JS;
121
122
        $this->cacheManager->write($cachePath, $content);
123
124
        return $content;
125
    }
126
127
    /**
128
     * @param string $cachePath
129
     *
130
     * @return string|null
131
     * @throws \League\Flysystem\FileExistsException
132
     * @throws \League\Flysystem\FileNotFoundException
133
     */
134
    public function renderRaw(string $cachePath): ?string
135
    {
136
        $content = $this->fileFinder->read($cachePath);
137
        if (null === $content) {
138
            return null;
139
        }
140
141
        if ($content === '' && !$this->fileFinder->has($cachePath)) {
142
            return null;
143
        }
144
145
        $this->cacheManager->write($cachePath, $content);
146
147
        return $content;
148
    }
149
150
    /**
151
     * @param string $groupName
152
     *
153
     * @return string
154
     * @throws \League\Flysystem\FileExistsException
155
     * @throws \League\Flysystem\FileNotFoundException
156
     */
157
    public function ensureCssWebPath(string $groupName): string
158
    {
159
        $cachePath = $groupName . static::FILE_EXTENSION_CSS;
160
161
        if (!$this->cacheManager->has($cachePath)) {
162
            $this->renderCss($groupName);
163
        }
164
165
        return $this->cacheManager->getWebPath($cachePath);
166
    }
167
168
    /**
169
     * @param string $groupName
170
     *
171
     * @return string
172
     * @throws \League\Flysystem\FileExistsException
173
     * @throws \League\Flysystem\FileNotFoundException
174
     */
175
    public function ensureJsWebPath(string $groupName): string
176
    {
177
        $cachePath = $groupName . static::FILE_EXTENSION_JS;
178
179
        if (!$this->cacheManager->has($cachePath)) {
180
            $this->renderJs($groupName);
181
        }
182
183
        return $this->cacheManager->getWebPath($cachePath);
184
    }
185
186
    /**
187
     * @param string $groupName
188
     *
189
     * @return string
190
     * @throws \League\Flysystem\FileExistsException
191
     * @throws \League\Flysystem\FileNotFoundException
192
     */
193
    public function ensureImgWebPath(string $cachePath): string
194
    {
195
        if (!$this->cacheManager->has($cachePath)) {
196
            if ($this->renderRaw($cachePath) === null) {
197
                throw new FileNotFoundException($cachePath);
198
            }
199
        }
200
201
        return $this->cacheManager->getWebPath($cachePath);
202
    }
203
204
    /**
205
     * @param string $key
206
     *
207
     * @return CssMinifier
208
     */
209
    protected function getCssMinifier(string $key): CssMinifier
210
    {
211
        if (!array_key_exists($key, $this->cssMinifiers)) {
212
            $this->cssMinifiers[$key] = $this->minifierFactory->createCssMinifier();
213
        }
214
215
        return $this->cssMinifiers[$key];
216
    }
217
218
    /**
219
     * @param string $key
220
     *
221
     * @return JsMinifier
222
     */
223
    protected function getJsMinifier(string $key): JsMinifier
224
    {
225
        if (!array_key_exists($key, $this->jsMinifiers)) {
226
            $this->jsMinifiers[$key] = $this->minifierFactory->createJsMinifier();
227
        }
228
229
        return $this->jsMinifiers[$key];
230
    }
231
}
232