Minify::renderer()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 5
nc 3
nop 2
crap 3
1
<?php declare(strict_types=1);
2
namespace samsonphp\minify;
3
4
use samson\core\ExternalModule;
5
use samsonphp\compressor\Compressor;
6
use samsonphp\event\Event;
7
8
/**
9
 * SamsonPHP minification module.
10
 *
11
 * @package SamsonPHP
12
 * @author  Vitaly Iegorov <[email protected]>
13
 * @author  Alexander Nazarenko <[email protected]>
14
 * @author  Nikita Kotenko <[email protected]>
15
 */
16
class Minify extends ExternalModule
17
{
18
    /**
19
     * Module preparation stage.
20
     *
21
     * @return bool Preparation stage result
22
     */
23 2
    public function prepare()
24
    {
25
        // Subscribe to compressor resource management
26 2
        if (class_exists(Compressor::class, false)) {
27
            Event::subscribe(Compressor::E_RESOURCE_COMPRESS, [$this, 'renderer']);
28
        }
29 2
    }
30
31
    /**
32
     * New resource file update handler.
33
     *
34
     * @param string $extension Resource extension
35
     * @param string $content   Compiled output resource content
36
     */
37 2
    public function renderer($extension, &$content)
38
    {
39
        // If CSS resource has been updated
40 2
        if ($extension === 'css') {
41
            // Read updated CSS resource file and compile it
42 1
            $content = \CssMin::minify($content);
43 1
        } elseif ($extension === 'js') {
44 1
            $content = \JShrink\Minifier::minify($content);
45
        }
46 2
    }
47
}
48