AnalyticsAsset::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 17
rs 10
1
<?php
2
/**
3
 * @link      https://dukt.net/analytics/
4
 * @copyright Copyright (c) 2022, Dukt
5
 * @license   https://github.com/dukt/analytics/blob/master/LICENSE.md
6
 */
7
8
namespace dukt\analytics\web\assets\analytics;
9
10
use Craft;
11
use craft\web\AssetBundle;
12
use craft\web\assets\cp\CpAsset;
13
use craft\helpers\ChartHelper;
14
use craft\web\View;
15
use craft\helpers\Json;
16
use dukt\analytics\Plugin as Analytics;
17
18
class AnalyticsAsset extends AssetBundle
19
{
20
    /**
21
     * @inheritdoc
22
     */
23
    public function init()
24
    {
25
        // define the path that your publishable resources live
26
        $this->sourcePath = __DIR__.'/dist';
27
28
        // define the dependencies
29
        $this->depends = [
30
            CpAsset::class,
31
        ];
32
33
        // define the relative path to CSS/JS files that should be registered with the page
34
        // when this asset bundle is registered
35
        $this->js = [
36
            'Analytics.js',
37
        ];
38
39
        parent::init();
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function registerAssetFiles($view)
46
    {
47
        parent::registerAssetFiles($view);
48
49
        $mapsApiKey = Craft::parseEnv(Analytics::$plugin->getSettings()->mapsApiKey);
0 ignored issues
show
Deprecated Code introduced by
The function Craft::parseEnv() has been deprecated: in 3.7.29. [[App::parseEnv()]] should be used instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

49
        $mapsApiKey = /** @scrutinizer ignore-deprecated */ Craft::parseEnv(Analytics::$plugin->getSettings()->mapsApiKey);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
50
        $continents = Analytics::$plugin->geo->getContinents();
51
        $subContinents = Analytics::$plugin->geo->getSubContinents();
52
        $formats = ChartHelper::formats();
53
54
        $js = "if(typeof Analytics == 'undefined') {";
55
56
        $js .= 'var Analytics = {};';
57
        $js .= 'Analytics.mapsApiKey = "'.$mapsApiKey.'";';
58
        $js .= 'Analytics.GoogleVisualizationCalled = false;';
59
        $js .= 'Analytics.GoogleVisualizationReady = false;';
60
        $js .= 'Analytics.reports = {};';
61
        $js .= 'Analytics.continents = '.Json::encode($continents).';';
62
        $js .= 'Analytics.subContinents = '.Json::encode($subContinents).';';
63
        $js .= 'Analytics.formats = '.Json::encode($formats).';';
64
        $js .= 'Analytics.chartLanguage = "'.Analytics::$plugin->getAnalytics()->getChartLanguage().'";';
65
        $js .= 'Analytics.d3FormatLocaleDefinition = window.d3FormatLocaleDefinition;';
66
        $js .= '}';
67
68
        $view->registerJs($js, View::POS_BEGIN);
69
    }
70
}