Issues (127)

src/base/PluginTrait.php (10 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\base;
9
10
use Craft;
11
use craft\db\Query;
12
use craft\helpers\Db;
13
use dukt\analytics\models\Info;
14
use dukt\analytics\Plugin as Analytics;
15
16
/**
17
 * PluginTrait implements the common methods and properties for plugin classes.
18
 *
19
 * @property \dukt\analytics\services\Analytics                 $analytics                  The analytics service
20
 * @property \dukt\analytics\services\Apis                      $apis                       The apis service
21
 * @property \dukt\analytics\services\Cache                     $cache                      The cache service
22
 * @property \dukt\analytics\services\Geo                       $geo                        The geo service
23
 * @property \dukt\analytics\services\Metadata                  $metadata                   The metadata service
24
 * @property \dukt\analytics\services\Oauth                     $oauth                      The oauth service
25
 * @property \dukt\analytics\services\Reports                   $reports                    The reports service
26
 * @property \dukt\analytics\services\Views                     $views                      The views service
27
 */
28
trait PluginTrait
29
{
30
    // Properties
31
    // =========================================================================
32
33
    /**
34
     * @var
35
     */
36
    private $_info;
37
38
    /**
39
     * @var
40
     */
41
    private $_isInstalled;
42
43
    // Public Methods
44
    // =========================================================================
45
46
    /**
47
     * Returns the analytics service.
48
     *
49
     * @return \dukt\analytics\services\Analytics The analytics service
50
     * @throws \yii\base\InvalidConfigException
51
     */
52
    public function getAnalytics()
53
    {
54
        /** @var Analytics $this */
55
        return $this->get('analytics');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->get('analytics') also could return the type mixed which is incompatible with the documented return type dukt\analytics\services\Analytics.
Loading history...
56
    }
57
58
    /**
59
     * Returns the apis service.
60
     *
61
     * @return \dukt\analytics\services\Apis The apis service
62
     * @throws \yii\base\InvalidConfigException
63
     */
64
    public function getApis()
65
    {
66
        /** @var Analytics $this */
67
        return $this->get('apis');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->get('apis') also could return the type mixed which is incompatible with the documented return type dukt\analytics\services\Apis.
Loading history...
68
    }
69
70
    /**
71
     * Returns the cache service.
72
     *
73
     * @return \dukt\analytics\services\Cache The cache service
74
     * @throws \yii\base\InvalidConfigException
75
     */
76
    public function getCache()
77
    {
78
        /** @var Analytics $this */
79
        return $this->get('cache');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->get('cache') also could return the type mixed which is incompatible with the documented return type dukt\analytics\services\Cache.
Loading history...
80
    }
81
82
    /**
83
     * Returns the geo service.
84
     *
85
     * @return \dukt\analytics\services\Geo The geo service
86
     * @throws \yii\base\InvalidConfigException
87
     */
88
    public function getGeo()
89
    {
90
        /** @var Analytics $this */
91
        return $this->get('geo');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->get('geo') also could return the type mixed which is incompatible with the documented return type dukt\analytics\services\Geo.
Loading history...
92
    }
93
94
    /**
95
     * Returns the metadata service.
96
     *
97
     * @return \dukt\analytics\services\Metadata The metadata service
98
     * @throws \yii\base\InvalidConfigException
99
     */
100
    public function getMetadata()
101
    {
102
        /** @var Analytics $this */
103
        return $this->get('metadata');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->get('metadata') also could return the type mixed which is incompatible with the documented return type dukt\analytics\services\Metadata.
Loading history...
104
    }
105
106
    /**
107
     * Returns the oauth service.
108
     *
109
     * @return \dukt\analytics\services\Oauth The oauth service
110
     * @throws \yii\base\InvalidConfigException
111
     */
112
    public function getOauth()
113
    {
114
        /** @var Analytics $this */
115
        return $this->get('oauth');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->get('oauth') also could return the type mixed which is incompatible with the documented return type dukt\analytics\services\Oauth.
Loading history...
116
    }
117
118
    /**
119
     * Returns the reports service.
120
     *
121
     * @return \dukt\analytics\services\Reports The reports service
122
     * @throws \yii\base\InvalidConfigException
123
     */
124
    public function getReports()
125
    {
126
        /** @var Analytics $this */
127
        return $this->get('reports');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->get('reports') also could return the type mixed which is incompatible with the documented return type dukt\analytics\services\Reports.
Loading history...
128
    }
129
130
    /**
131
     * Returns the views service.
132
     *
133
     * @return \dukt\analytics\services\Views The views service
134
     * @throws \yii\base\InvalidConfigException
135
     */
136
    public function getViews()
137
    {
138
        /** @var Analytics $this */
139
        return $this->get('views');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->get('views') also could return the type mixed which is incompatible with the documented return type dukt\analytics\services\Views.
Loading history...
140
    }
141
142
    /**
143
     * Updates the info row.
144
     *
145
     * @param Info $info
146
     *
147
     * @return bool
148
     * @throws \yii\db\Exception
149
     */
150
    public function saveInfo(Info $info): bool
151
    {
152
        $attributes = Db::prepareValuesForDb($info);
153
154
        if (array_key_exists('id', $attributes) && $attributes['id'] === null) {
155
            unset($attributes['id']);
156
        }
157
158
        if ($this->getIsInstalled()) {
159
            Craft::$app->getDb()->createCommand()
160
                ->update('{{%analytics_info}}', $attributes)
161
                ->execute();
162
        } else {
163
            Craft::$app->getDb()->createCommand()
164
                ->insert('{{%analytics_info}}', $attributes)
165
                ->execute();
166
167
            if (Craft::$app->getIsInstalled()) {
168
                // Set the new id
169
                $info->id = Craft::$app->getDb()->getLastInsertID('{{%analytics_info}}');
0 ignored issues
show
Documentation Bug introduced by
It seems like Craft::app->getDb()->get...('{{%analytics_info}}') of type string is incompatible with the declared type integer|null of property $id.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
170
            }
171
        }
172
173
        $this->_info = $info;
174
175
        return true;
176
    }
177
178
    /**
179
     * Returns the info model, or just a particular attribute.
180
     *
181
     * @return Info
182
     * @throws ServerErrorHttpException if the info table is missing its row
183
     */
184
    public function getInfo(): Info
185
    {
186
        /** @var WebApplication|ConsoleApplication $this */
187
        if ($this->_info !== null) {
188
            return $this->_info;
189
        }
190
191
        if (!$this->getIsInstalled()) {
192
            return new Info();
193
        }
194
195
        $row = (new Query())
196
            ->from(['{{%analytics_info}}'])
197
            ->one();
198
199
        if (!$row) {
200
            $tableName = $this->getDb()->getSchema()->getRawTableName('{{%analytics_info}}');
201
            throw new ServerErrorHttpException("The {$tableName} table is missing its row");
0 ignored issues
show
The type dukt\analytics\base\ServerErrorHttpException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
202
        }
203
204
        return $this->_info = new Info($row);
205
    }
206
207
    /**
208
     * Returns whether Craft is installed.
209
     *
210
     * @return bool
211
     */
212
    public function getIsInstalled(): bool
213
    {
214
        /** @var WebApplication|ConsoleApplication $this */
215
        if ($this->_isInstalled !== null) {
216
            return $this->_isInstalled;
217
        }
218
219
        $infoRowExists = false;
220
221
        if(Craft::$app->getDb()->tableExists('{{%analytics_info}}', false)) {
222
            $infoRowExists = (new Query())
223
                ->from(['{{%analytics_info}}'])
224
                ->one();
225
        }
226
227
        return $this->_isInstalled = (
228
            Craft::$app->getIsDbConnectionValid() &&
229
            $infoRowExists
230
        );
231
    }
232
}
233