DbAsset   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 7
dl 0
loc 43
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B init() 0 25 4
1
<?php
2
namespace app\modules\prototype\assets;
3
4
/**
5
 * @link http://www.diemeisterei.de/
6
 *
7
 * @copyright Copyright (c) 2015 diemeisterei GmbH, Stuttgart
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
use app\modules\prototype\models\Less;
14
use yii\caching\FileDependency;
15
use yii\helpers\FileHelper;
16
use yii\helpers\Json;
17
use yii\web\AssetBundle;
18
19
class DbAsset extends AssetBundle
20
{
21
    const CACHE_ID = 'app\assets\SettingsAsset';
22
    const SETTINGS_KEY = 'app.less';
23
    const MAIN_LESS_FILE = 'main.less';
24
25
    public $sourcePath = '@runtime/settings-asset';
26
27
    public $css = [
28
        self::MAIN_LESS_FILE,
29
    ];
30
31
    public $depends = [
32
        // if a full Bootstrap CSS is compiled, it's recommended to disable the asset in assetManager configuration
33
        'yii\bootstrap\BootstrapAsset',
34
    ];
35
36
    public function init()
37
    {
38
        parent::init();
39
40
        $sourcePath = \Yii::getAlias($this->sourcePath);
41
        @mkdir($sourcePath);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
42
43
        $models = Less::find()->all();
44
        $hash = sha1(Json::encode($models));
45
46
        if ($hash !== \Yii::$app->cache->get(self::CACHE_ID)) {
47
            $lessFiles = FileHelper::findFiles($sourcePath, ['only' => ['*.less']]);
0 ignored issues
show
Bug introduced by
It seems like $sourcePath defined by \Yii::getAlias($this->sourcePath) on line 40 can also be of type boolean; however, yii\helpers\BaseFileHelper::findFiles() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
48
            foreach ($lessFiles as $file) {
49
                unlink($file);
50
            }
51
            foreach ($models as $model) {
52
                file_put_contents("$sourcePath/{$model->key}.less", $model->value);
53
            }
54
55
            $dependency = new FileDependency();
56
            $dependency->fileName = __FILE__;
57
            \Yii::$app->cache->set(self::CACHE_ID, $hash, 0, $dependency);
58
            @touch($sourcePath);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
59
        }
60
    }
61
}
62