JS   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 4
dl 0
loc 109
ccs 48
cts 54
cp 0.8889
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B export() 0 47 10
B process() 0 52 8
1
<?php
2
/**
3
 * JS.php
4
 * @author Revin Roman
5
 * @link https://rmrevin.com
6
 */
7
8
namespace rmrevin\yii\minify\components;
9
10
use JSMin\JSMin;
11
use yii\helpers\Html;
12
13
/**
14
 * Class JS
15
 * @package rmrevin\yii\minify\components
16
 */
17
class JS extends MinifyComponent
18
{
19
20 9
    public function export()
21
    {
22 9
        $jsFiles = $this->view->jsFiles;
23
24 9
        $jsPosition = $this->view->jsPosition;
25 9
        $jsOptions = $this->view->jsOptions;
26
27 9
        if (!empty($jsFiles)) {
28 7
            foreach ($jsFiles as $position => $files) {
29 7
                if (false === in_array($position, $jsPosition, true)) {
30 6
                    $this->view->jsFiles[$position] = [];
31
32 6
                    foreach ($files as $file => $html) {
33 6
                        $this->view->jsFiles[$position][$file] = $html;
34
                    }
35
                } else {
36 7
                    $this->view->jsFiles[$position] = [];
37
38 7
                    $toMinify = [];
39
40 7
                    foreach ($files as $file => $html) {
41 7
                        if ($this->thisFileNeedMinify($file, $html)) {
42 7
                            if ($this->view->concatJs) {
43 7
                                $toMinify[$file] = $html;
44
                            } else {
45 7
                                $this->process($position, $jsOptions, [$file => $html]);
46
                            }
47
                        } else {
48 6
                            if (!empty($toMinify)) {
49
                                $this->process($position, $jsOptions, $toMinify);
50
51
                                $toMinify = [];
52
                            }
53
54 6
                            $this->view->jsFiles[$position][$file] = $html;
55
                        }
56
                    }
57
58 7
                    if (!empty($toMinify)) {
59 7
                        $this->process($position, $jsOptions, $toMinify);
60
                    }
61
62 7
                    unset($toMinify);
63
                }
64
            }
65
        }
66 9
    }
67
68
    /**
69
     * @param integer $position
70
     * @param array $options
71
     * @param array $files
72
     */
73 7
    protected function process($position, $options, $files)
74
    {
75 7
        $minifyPath = $this->view->minifyPath;
76 7
        $hash = $this->_getSummaryFilesHash($files);
77
78 7
        $resultFile = $minifyPath . DIRECTORY_SEPARATOR . $hash . '.js';
79
80 7
        if (!file_exists($resultFile)) {
81 7
            $js = '';
82
83 7
            foreach ($files as $file => $html) {
84 7
                $cacheKey = $this->buildCacheKey($file);
85
86 7
                $content = $this->getFromCache($cacheKey);
87
88 7
                if (false !== $content) {
89
                    $js .= $content;
90
                    continue;
91
                }
92
93 7
                $file = $this->getAbsoluteFilePath($file);
94
95 7
                $content = '';
96
97 7
                if (!file_exists($file)) {
98
                    \Yii::warning(sprintf('Asset file not found `%s`', $file), __METHOD__);
99 7
                } elseif (!is_readable($file)) {
100
                    \Yii::warning(sprintf('Asset file not readable `%s`', $file), __METHOD__);
101
                } else {
102 7
                    $content .= file_get_contents($file) . ';' . "\n";
103
                }
104
105 7
                if ($this->view->minifyJs) {
106 7
                    $content = JSMin::minify($content);
107
                }
108
109 7
                $this->saveToCache($cacheKey, $content);
110
111 7
                $js .= $content;
112
            }
113
114 7
            file_put_contents($resultFile, $js);
115
116 7
            if (false !== $this->view->fileMode) {
117 7
                @chmod($resultFile, $this->view->fileMode);
118
            }
119
        }
120
121 7
        $file = $this->prepareResultFile($resultFile);
122
123 7
        $this->view->jsFiles[$position][$file] = Html::jsFile($file, $options);
124 7
    }
125
}
126