JSLoggingAsset   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 7 1
A publish() 0 14 2
1
<?php
2
/**
3
 * AssetBundle to register when you want to log javascript events as well.
4
 */
5
6
namespace bedezign\yii2\audit\web;
7
8
use bedezign\yii2\audit\Audit;
9
use yii\helpers\Url;
10
use yii\web\AssetBundle;
11
use yii\web\View;
12
13
/**
14
 * JSLoggingAsset
15
 * @package bedezign\yii2\audit\assets
16
 */
17
class JSLoggingAsset extends AssetBundle
18
{
19
    /**
20
     * @var string
21
     */
22
    public $sourcePath = '@bedezign/yii2/audit/web/assets';
23
24
    /**
25
     * @var array
26
     */
27
    public $js = [
28
        'javascript/logger.js',
29
    ];
30
31
    /**
32
     *
33
     */
34 42
    public function init()
35
    {
36
        // Activate the logging as soon as we can
37 42
        $this->jsOptions['position'] = View::POS_HEAD;
38 42
        $this->publishOptions['forceCopy'] = YII_DEBUG;
39 42
        parent::init();
40 42
    }
41
42
    /**
43
     * @param \yii\web\AssetManager $assetManager
44
     */
45 42
    public function publish($assetManager)
46
    {
47 42
        $module = Audit::getInstance();
48
        // We can't be sure that the actual logger was loaded already, so we fallback on the window object
49
        // to store the associated audit url and entry id
50 42
        $url = Url::to(["/{$module->id}/js-log/index"]);
51 42
        $script = "window.auditUrl = '$url';";
52 42
        if ($module->entry) {
53 18
            $id = $module->getEntry()->id;
54 18
            $script .= "window.auditEntry = $id;";
55 18
        }
56 42
        \Yii::$app->view->registerJs($script, View::POS_HEAD);
57 42
        parent::publish($assetManager);
58
    }
59
}