Issues (127)

src/controllers/UtilsController.php (5 issues)

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\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 string|null $q
43
     * @param array|null $columns
44
     * @return Response
45
     */
46
    public function actionMetadata(string $q = null, array $columns = null): Response
47
    {
48
        $variables['q'] = $q;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$variables was never initialized. Although not strictly required by PHP, it is generally a good practice to add $variables = array(); before regardless.
Loading history...
49
        $variables['columns'] = $columns;
50
        $variables['dimensions'] = Analytics::$plugin->metadata->getDimensions();
51
        $variables['metrics'] = Analytics::$plugin->metadata->getMetrics();
52
53
        $variables['dimmetsFileExists'] = Analytics::$plugin->metadata->dimmetsFileExists();
54
55
        return $this->renderTemplate('analytics/utils/metadata/_index', $variables);
56
    }
57
58
    /**
59
     * Searches the meta data.
60
     *
61
     * @return null
62
     */
63
    public function actionSearchMetadata()
64
    {
65
        $q = Craft::$app->getRequest()->getParam('q');
66
        $columns = Analytics::$plugin->metadata->searchColumns($q);
67
68
        // Send the source back to the template
69
        Craft::$app->getUrlManager()->setRouteParams([
70
            'q' => $q,
71
            'columns' => $columns,
72
        ]);
73
74
        return null;
75
    }
76
77
    /**
78
     * Loads metadata.
79
     *
80
     * @return Response
81
     */
82
    public function actionLoadMetadata()
83
    {
84
        $this->_deleteMetadata();
85
        $this->_importMetadata();
86
87
        Craft::$app->getSession()->setNotice(Craft::t('analytics', 'Metadata loaded.'));
88
89
        $referrer = Craft::$app->getRequest()->referrer;
90
91
        return $this->redirect($referrer);
92
    }
93
94
    // Private Methods
95
    // =========================================================================
96
97
98
    /**
99
     * Deletes metadata.
100
     */
101
    private function _deleteMetadata()
102
    {
103
        $path = Analytics::$plugin->metadata->getDimmetsFilePath();
104
105
        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

105
        /** @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...
It seems like $path can also be of type false; 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

105
        FileHelper::removeFile(/** @scrutinizer ignore-type */ $path);
Loading history...
106
    }
107
108
    /**
109
     * Imports metadata.
110
     */
111
    private function _importMetadata()
112
    {
113
        $columns = [];
114
115
        $items = Analytics::$plugin->getApis()->getAnalytics()->getColumns();
116
117
        if ($items) {
0 ignored issues
show
$items is of type Google_Service_Analytics_Columns, thus it always evaluated to true.
Loading history...
118
            foreach ($items as $item) {
119
                if ($item->attributes['status'] == 'DEPRECATED') {
120
                    continue;
121
                }
122
123
                if ($item->attributes['addedInApiVersion'] > $this->addedInApiVersion) {
124
                    continue;
125
                }
126
127
                $this->addColumnFromItem($columns, $item);
128
            }
129
        }
130
131
        $contents = json_encode($columns, JSON_PRETTY_PRINT);
132
133
        $path = Analytics::$plugin->metadata->getDimmetsFilePath();
134
135
        FileHelper::writeToFile($path, $contents);
0 ignored issues
show
It seems like $path can also be of type false; 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

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