Passed
Push — master ( f38dde...147718 )
by Fran
02:30
created

JsTrait::compileJs()   B

Complexity

Conditions 10
Paths 5

Size

Total Lines 34
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 12.8177

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 24
c 1
b 0
f 0
nc 5
nop 4
dl 0
loc 34
ccs 16
cts 23
cp 0.6957
crap 12.8177
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace PSFS\base\extension\traits;
3
4
use MatthiasMullie\Minify\JS;
5
use PSFS\base\config\Config;
6
use PSFS\base\exception\ConfigException;
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
    /**
17
     * @param array $compiledFiles
18
     * @param string $baseUrl
19
     * @param string $hash
20
     * @param bool $debug
21
     */
22 1
    protected function printJs(array $compiledFiles, $baseUrl, $hash, $debug = false)
23
    {
24 1
        if ($debug && 0 < count($compiledFiles)) {
25 1
            foreach ($compiledFiles as $file) {
26 1
                echo "\t\t<script type='text/javascript' src='{$file}'></script>\n";
27
            }
28
        } else {
29
            echo "\t\t<script type='text/javascript' src='" . $baseUrl . "/js/" . $hash . ".js'></script>\n";
30
        }
31 1
    }
32
33
    /**
34
     * @param $pathParts
35
     * @param string $base
36
     * @param $file
37
     * @return false|string
38
     * @throws \PSFS\base\exception\GeneratorException
39
     */
40 1
    protected function putDebugJs($pathParts, $base, $file)
41
    {
42 1
        $filePath = $this->hash . "_" . $pathParts[count($pathParts) - 1];
43 1
        $this->compiled_files[] = "/js/" . $filePath;
0 ignored issues
show
Bug Best Practice introduced by
The property compiled_files does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
44 1
        $data = "";
45 1
        if (!file_exists($base . $filePath) || filemtime($base . $filePath) < filemtime($file)) {
46 1
            $data = file_get_contents($file);
47 1
            $this->storeContents($base . $filePath, $data);
48
        }
49 1
        return $data;
50
    }
51
52
    /**
53
     * Método para guardar cualquier contenido y controlar que existe el directorio y se guarda correctamente
54
     * @param string $path
55
     * @param string $content
56
     * @throws \PSFS\base\exception\GeneratorException
57
     */
58 1
    private function storeContents($path, $content = "")
59
    {
60 1
        GeneratorHelper::createDir(dirname($path));
61 1
        if ("" !== $content && false === file_put_contents($path, $content)) {
62
            throw new ConfigException(t('No se tienen permisos para escribir en ' . $path));
63
        }
64 1
    }
65
66
    /**
67
     * Método que compila los ficheros javascript en función del modo de ejecución
68
     * @return $this
69
     * @throws \PSFS\base\exception\GeneratorException
70
     */
71 1
    protected function compileJs(array $files, $basePath, $hash, $debug = false)
72
    {
73 1
        $base = $basePath . "js" . DIRECTORY_SEPARATOR;
74 1
        if ($debug || !file_exists($base . $hash . ".js")) {
75 1
            $data = '';
76 1
            if (0 < count($files)) {
77 1
                $minifier = new JS();
78 1
                foreach ($files as $file) {
79 1
                    $pathParts = explode("/", $file);
80 1
                    if (file_exists($file)) {
81 1
                        if ($debug) {
82 1
                            $data = $this->putDebugJs($pathParts, $base, $file);
83
                        } elseif (!file_exists($base . $hash . ".js")) {
84 1
                            $minifier->add($file);
85
                            //$data = $this->putProductionJs($base, $file, $data);
86
                        }
87
                    }
88
                }
89 1
                if($debug) {
90 1
                    $this->storeContents($base . $hash . ".js", $data);
91
                } else {
92
                    ini_set('max_execution_time', -1);
93
                    ini_set('memory_limit', -1);
94
                    GeneratorHelper::createDir($base);
95
                    if(Config::getParam('assets.obfuscate', false)) {
96
                        $minifier->gzip($base . $hash . ".js");
97
                    } else {
98
                        $minifier->minify($base . $hash . ".js");
99
                    }
100
                }
101 1
                unset($minifier);
102
            }
103
        }
104 1
        return $this;
105
    }
106
}
107