Passed
Push — master ( 758032...380927 )
by Benjamin
28:34 queued 21:24
created

UtilsController::addColumnFromItem()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 3
nop 2
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link      https://dukt.net/craft/analytics/
4
 * @copyright Copyright (c) 2018, Dukt
5
 * @license   https://dukt.net/craft/analytics/docs/license
6
 */
7
8
namespace dukt\analytics\controllers;
9
10
use Craft;
11
use craft\helpers\FileHelper;
12
use craft\web\Controller;
13
use dukt\analytics\Plugin as Analytics;
14
use yii\web\Response;
15
16
/**
17
 * Class UtilsController
18
 *
19
 * @package dukt\analytics\controllers
20
 */
21
class UtilsController extends Controller
22
{
23
    // Properties
24
    // =========================================================================
25
26
    /**
27
     * @var bool
28
     */
29
    protected $allowAnonymous = true;
30
31
    /**
32
     * @var int
33
     */
34
    private $addedInApiVersion = 3;
35
36
    // Public Methods
37
    // =========================================================================
38
39
    /**
40
     * Metadata.
41
     *
42
     * @param array $variables
43
     *
44
     * @return Response
45
     */
46
    public function actionMetadata(array $variables = [])
47
    {
48
        $variables['dimensions'] = Analytics::$plugin->metadata->getDimensions();
49
        $variables['metrics'] = Analytics::$plugin->metadata->getMetrics();
50
51
        $variables['dimmetsFileExists'] = Analytics::$plugin->metadata->dimmetsFileExists();
52
53
        return $this->renderTemplate('analytics/utils/metadata/_index', $variables);
54
    }
55
56
    /**
57
     * Search metadata.
58
     */
59
    public function actionSearchMetadata()
60
    {
61
        $q = Craft::$app->getRequest()->getParam('q');
62
        $columns = Analytics::$plugin->metadata->searchColumns($q);
0 ignored issues
show
Bug introduced by
It seems like $q can also be of type array; however, parameter $q of dukt\analytics\services\Metadata::searchColumns() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

62
        $columns = Analytics::$plugin->metadata->searchColumns(/** @scrutinizer ignore-type */ $q);
Loading history...
63
64
        // Send the source back to the template
65
        Craft::$app->urlManager->setRouteVariables([
66
            'q' => $q,
67
            'columns' => $columns,
68
        ]);
69
    }
70
71
    /**
72
     * Loads metadata.
73
     *
74
     * @return Response
75
     */
76
    public function actionLoadMetadata()
77
    {
78
        $this->_deleteMetadata();
79
        $this->_importMetadata();
80
81
        Craft::$app->getSession()->setNotice(Craft::t('analytics', 'Metadata loaded.'));
82
83
        $referrer = Craft::$app->getRequest()->referrer;
84
85
        return $this->redirect($referrer);
86
    }
87
88
    // Private Methods
89
    // =========================================================================
90
91
92
    /**
93
     * Deletes metadata.
94
     */
95
    private function _deleteMetadata()
96
    {
97
        $path = Analytics::$plugin->metadata->getDimmetsFilePath();
98
99
        FileHelper::removeFile($path);
0 ignored issues
show
Deprecated Code introduced by
The function craft\helpers\FileHelper::removeFile() has been deprecated: in 3.0.0-RC11. Use [[unlink()]] instead. ( Ignorable by Annotation )

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

99
        /** @scrutinizer ignore-deprecated */ FileHelper::removeFile($path);

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...
Bug introduced by
It seems like $path can also be of type boolean; however, parameter $path of craft\helpers\FileHelper::removeFile() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

99
        FileHelper::removeFile(/** @scrutinizer ignore-type */ $path);
Loading history...
100
    }
101
102
    /**
103
     * Imports metadata.
104
     */
105
    private function _importMetadata()
106
    {
107
        $columns = [];
108
109
        $items = Analytics::$plugin->getApis()->getAnalytics()->getColumns();
110
111
        if ($items) {
0 ignored issues
show
introduced by
$items is of type Google_Service_Analytics_Columns, thus it always evaluated to true.
Loading history...
112
            foreach ($items as $item) {
113
                if ($item->attributes['status'] == 'DEPRECATED') {
114
                    continue;
115
                }
116
117
                if ($item->attributes['addedInApiVersion'] > $this->addedInApiVersion) {
118
                    continue;
119
                }
120
121
                $this->addColumnFromItem($columns, $item);
122
            }
123
        }
124
125
        $contents = json_encode($columns, JSON_PRETTY_PRINT);
126
127
        $path = Analytics::$plugin->metadata->getDimmetsFilePath();
128
129
        FileHelper::writeToFile($path, $contents);
0 ignored issues
show
Bug introduced by
It seems like $path can also be of type boolean; however, parameter $file of craft\helpers\FileHelper::writeToFile() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

129
        FileHelper::writeToFile(/** @scrutinizer ignore-type */ $path, $contents);
Loading history...
130
    }
131
132
    /**
133
     * Add column from item.
134
     *
135
     * @param $columns
136
     * @param $item
137
     */
138
    private function addColumnFromItem(&$columns, $item)
139
    {
140
        if (isset($item->attributes['minTemplateIndex'])) {
141
            for ($i = $item->attributes['minTemplateIndex']; $i <= $item->attributes['maxTemplateIndex']; $i++) {
142
                $column = [];
143
                $column['id'] = str_replace('XX', $i, $item->id);
144
                $column['uiName'] = str_replace('XX', $i, $item->attributes['uiName']);
145
                $column['description'] = str_replace('XX', $i, $item->attributes['description']);
146
147
                $columns[$column['id']] = $this->populateColumnAttributes($column, $item);
148
            }
149
        } else {
150
            $column = [];
151
            $column['id'] = $item->id;
152
            $column['uiName'] = $item->attributes['uiName'];
153
            $column['description'] = $item->attributes['description'];
154
155
            $columns[$column['id']] = $this->populateColumnAttributes($column, $item);
156
        }
157
    }
158
159
    /**
160
     * Populates the coloumn attribute.
161
     *
162
     * @param $column
163
     * @param $item
164
     *
165
     * @return mixed
166
     */
167
    private function populateColumnAttributes($column, $item)
168
    {
169
        $column['type'] = $item->attributes['type'];
170
        $column['dataType'] = $item->attributes['dataType'];
171
        $column['group'] = $item->attributes['group'];
172
        $column['status'] = $item->attributes['status'];
173
174
        if (isset($item->attributes['allowInSegments'])) {
175
            $column['allowInSegments'] = $item->attributes['allowInSegments'];
176
        }
177
178
        if (isset($item->attributes['addedInApiVersion'])) {
179
            $column['addedInApiVersion'] = $item->attributes['addedInApiVersion'];
180
        }
181
182
        return $column;
183
    }
184
}