Passed
Push — master ( 3c999b...8ea9d3 )
by Fran
02:26
created

JsTrait   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 75.61%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 40
c 2
b 0
f 0
dl 0
loc 95
ccs 31
cts 41
cp 0.7561
rs 10
wmc 18

4 Methods

Rating   Name   Duplication   Size   Complexity  
B compileJs() 0 28 9
A dumpJs() 0 9 2
A putDebugJs() 0 10 3
A printJs() 0 9 4
1
<?php
2
namespace PSFS\base\extension\traits;
3
4
use MatthiasMullie\Minify\JS;
5
use PSFS\base\config\Config;
6
use PSFS\base\types\helpers\AssetsHelper;
7
use PSFS\base\types\helpers\GeneratorHelper;
8
9
/**
10
 * Trait JsTrait
11
 * @package PSFS\base\extension\traits
12
 */
13
trait JsTrait {
14
15
    /**
16
     * @param array $compiledFiles
17
     * @param string $baseUrl
18
     * @param string $hash
19
     * @param string $sri
20
     */
21 1
    protected function printJs(array $compiledFiles, $baseUrl, $hash, $sri = null)
22
    {
23 1
        if (Config::getParam('debug') && 0 < count($compiledFiles)) {
24 1
            foreach ($compiledFiles as $file) {
25 1
                echo "\t\t<script type='text/javascript' src='{$file}'></script>\n";
26
            }
27
        } else {
28 1
            echo "\t\t<script type='text/javascript' src='" . $baseUrl . "/js/" . $hash . ".js'" .
29 1
                " crossorigin='anonymous' integrity='sha384-" . $sri . "'></script>\n";
30
        }
31 1
    }
32
33
    /**
34
     * @param $pathParts
35
     * @param string $base
36
     * @param string $file
37
     * @param string $hash
38
     * @param array $compiledFiles
39
     * @return false|string
40
     * @throws \PSFS\base\exception\GeneratorException
41
     */
42 1
    protected function putDebugJs($pathParts, $base, $file, $hash, array &$compiledFiles)
43
    {
44 1
        $filePath = $hash . "_" . $pathParts[count($pathParts) - 1];
45 1
        $compiledFiles[] = "/js/" . $filePath;
46 1
        $data = "";
47 1
        if (!file_exists($base . $filePath) || filemtime($base . $filePath) < filemtime($file)) {
48 1
            $data = file_get_contents($file);
49 1
            AssetsHelper::storeContents($base . $filePath, $data);
50
        }
51 1
        return $data;
52
    }
53
54
    /**
55
     * Método que compila los ficheros javascript en función del modo de ejecución
56
     * @param array $files
57
     * @param string $basePath
58
     * @param string $hash
59
     * @param array $compiledFiles
60
     * @return $this
61
     * @throws \PSFS\base\exception\GeneratorException
62
     */
63 1
    protected function compileJs(array $files, $basePath, $hash, array &$compiledFiles)
64
    {
65 1
        $base = $basePath . "js" . DIRECTORY_SEPARATOR;
66 1
        $debug = Config::getParam('debug');
67 1
        if ($debug || !file_exists($base . $hash . ".js")) {
68 1
            $data = '';
69 1
            if (0 < count($files)) {
70 1
                $minifier = new JS();
71 1
                foreach ($files as $file) {
72 1
                    $pathParts = explode("/", $file);
73 1
                    if (file_exists($file)) {
74 1
                        if ($debug) {
75 1
                            $data = $this->putDebugJs($pathParts, $base, $file, $hash, $compiledFiles);
76
                        } elseif (!file_exists($base . $hash . ".js")) {
77
                            $minifier->add($file);
78
                            //$data = $this->putProductionJs($base, $file, $data);
79
                        }
80
                    }
81
                }
82 1
                if($debug) {
83 1
                    AssetsHelper::storeContents($base . $hash . ".js", $data);
84
                } else {
85
                    $this->dumpJs($hash, $base, $minifier);
86
                }
87 1
                unset($minifier);
88
            }
89
        }
90 1
        return $this;
91
    }
92
93
    /**
94
     * @param $hash
95
     * @param string $base
96
     * @param JS $minifier
97
     * @throws \PSFS\base\exception\GeneratorException
98
     */
99
    protected function dumpJs($hash, string $base, JS $minifier)
100
    {
101
        ini_set('max_execution_time', -1);
102
        ini_set('memory_limit', -1);
103
        GeneratorHelper::createDir($base);
104
        if (Config::getParam('assets.obfuscate', false)) {
105
            $minifier->gzip($base . $hash . ".js");
106
        } else {
107
            $minifier->minify($base . $hash . ".js");
108
        }
109
    }
110
}
111