Passed
Pull Request — master (#22)
by Théo
02:53
created

Javascript::compactContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box\Compactor;
16
17
use Exception;
18
use JShrink\Minifier;
19
20
/**
21
 * Compacts Javascript files using JShrink.
22
 *
23
 * @author Robert Hafner <[email protected]>
24
 */
25
final class Javascript extends FileExtensionCompactor
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function __construct(array $extensions = ['js'])
31
    {
32
        parent::__construct($extensions);
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    protected function compactContent(string $contents): string
39
    {
40
        try {
41
            return Minifier::minify($contents);
42
        } catch (Exception $e) {
43
            // Returns unchanged content
44
45
            return $contents;
46
        }
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    protected function supports(string $file): bool
53
    {
54
        if (!parent::supports($file)) {
55
            return false;
56
        }
57
58
        return '.min.js' !== substr($file, -7);
59
    }
60
}
61