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

PluginTrait::getIsInstalled()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 5
nop 0
dl 0
loc 18
rs 9.2
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\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\Metadata                  $metadata                   The metadata service
23
 * @property \dukt\analytics\services\Oauth                     $oauth                      The oauth service
24
 * @property \dukt\analytics\services\Reports                   $reports                    The reports service
25
 * @property \dukt\analytics\services\Views                     $views                    The views service
26
 */
27
trait PluginTrait
28
{
29
    // Properties
30
    // =========================================================================
31
32
    /**
33
     * @var
34
     */
35
    private $_info;
36
37
    /**
38
     * @var
39
     */
40
    private $_isInstalled;
41
42
    // Public Methods
43
    // =========================================================================
44
45
    /**
46
     * Returns the analytics service.
47
     *
48
     * @return \dukt\analytics\services\Analytics The analytics service
49
     * @throws \yii\base\InvalidConfigException
50
     */
51
    public function getAnalytics()
52
    {
53
        /** @var Analytics $this */
54
        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...
55
    }
56
57
    /**
58
     * Returns the apis service.
59
     *
60
     * @return \dukt\analytics\services\Apis The apis service
61
     * @throws \yii\base\InvalidConfigException
62
     */
63
    public function getApis()
64
    {
65
        /** @var Analytics $this */
66
        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...
67
    }
68
69
    /**
70
     * Returns the cache service.
71
     *
72
     * @return \dukt\analytics\services\Cache The cache service
73
     * @throws \yii\base\InvalidConfigException
74
     */
75
    public function getCache()
76
    {
77
        /** @var Analytics $this */
78
        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...
79
    }
80
81
    /**
82
     * Returns the metadata service.
83
     *
84
     * @return \dukt\analytics\services\Metadata The metadata service
85
     * @throws \yii\base\InvalidConfigException
86
     */
87
    public function getMetadata()
88
    {
89
        /** @var Analytics $this */
90
        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...
91
    }
92
93
    /**
94
     * Returns the oauth service.
95
     *
96
     * @return \dukt\analytics\services\Oauth The oauth service
97
     * @throws \yii\base\InvalidConfigException
98
     */
99
    public function getOauth()
100
    {
101
        /** @var Analytics $this */
102
        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...
103
    }
104
105
    /**
106
     * Returns the reports service.
107
     *
108
     * @return \dukt\analytics\services\Reports The reports service
109
     * @throws \yii\base\InvalidConfigException
110
     */
111
    public function getReports()
112
    {
113
        /** @var Analytics $this */
114
        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...
115
    }
116
117
    /**
118
     * Returns the views service.
119
     *
120
     * @return \dukt\analytics\services\Views The views service
121
     * @throws \yii\base\InvalidConfigException
122
     */
123
    public function getViews()
124
    {
125
        /** @var Analytics $this */
126
        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...
127
    }
128
129
    /**
130
     * Updates the info row.
131
     *
132
     * @param Info $info
133
     *
134
     * @return bool
135
     * @throws \yii\db\Exception
136
     */
137
    public function saveInfo(Info $info): bool
138
    {
139
        $attributes = Db::prepareValuesForDb($info);
140
141
        if (array_key_exists('id', $attributes) && $attributes['id'] === null) {
142
            unset($attributes['id']);
143
        }
144
        echo 'a';
145
        if ($this->getIsInstalled()) {
146
            echo 'b';
147
148
            Craft::$app->getDb()->createCommand()
149
                ->update('{{%analytics_info}}', $attributes)
150
                ->execute();
151
        } else {
152
            echo 'c';
153
154
            Craft::$app->getDb()->createCommand()
155
                ->insert('{{%analytics_info}}', $attributes)
156
                ->execute();
157
158
            if (Craft::$app->getIsInstalled()) {
159
                // Set the new id
160
                $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 null|integer 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...
161
            }
162
        }
163
164
        $this->_info = $info;
165
166
        return true;
167
    }
168
169
    /**
170
     * Returns the info model, or just a particular attribute.
171
     *
172
     * @return Info
173
     * @throws ServerErrorHttpException if the info table is missing its row
174
     */
175
    public function getInfo(): Info
176
    {
177
        /** @var WebApplication|ConsoleApplication $this */
178
        if ($this->_info !== null) {
179
            return $this->_info;
180
        }
181
182
        if (!$this->getIsInstalled()) {
183
            return new Info();
184
        }
185
186
        $row = (new Query())
187
            ->from(['{{%analytics_info}}'])
188
            ->one();
189
190
        if (!$row) {
191
            $tableName = $this->getDb()->getSchema()->getRawTableName('{{%analytics_info}}');
192
            throw new ServerErrorHttpException("The {$tableName} table is missing its row");
0 ignored issues
show
Bug introduced by
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...
193
        }
194
195
        return $this->_info = new Info($row);
196
    }
197
198
    /**
199
     * Returns whether Craft is installed.
200
     *
201
     * @return bool
202
     */
203
    public function getIsInstalled(): bool
204
    {
205
        /** @var WebApplication|ConsoleApplication $this */
206
        if ($this->_isInstalled !== null) {
207
            return $this->_isInstalled;
208
        }
209
210
        $infoRowExists = false;
211
212
        if(Craft::$app->getDb()->tableExists('{{%analytics_info}}', false)) {
213
            $infoRowExists = (new Query())
214
                ->from(['{{%analytics_info}}'])
215
                ->one();
216
        }
217
218
        return $this->_isInstalled = (
219
            Craft::$app->getIsDbConnectionValid() &&
220
            $infoRowExists
221
        );
222
    }
223
}
224