Passed
Push — master ( 34ca9b...301366 )
by Paweł
02:37
created

Account::getDisplayName()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace app\models;
4
5
use Yii;
6
use yii\behaviors\TimestampBehavior;
7
use yii\db\Expression;
8
use yii\helpers\ArrayHelper;
9
10
/**
11
 * This is the model class for table "account".
12
 *
13
 * @property int $id
14
 * @property string $name
15
 * @property string $username
16
 * @property string $profile_pic_url
17
 * @property string $full_name
18
 * @property string $biography
19
 * @property string $external_url
20
 * @property string $instagram_id
21
 * @property string $updated_at
22
 * @property string $created_at
23
 * @property int $monitoring
24
 * @property int $proxy_id
25
 * @property int $proxy_tag_id
26
 * @property string $notes
27
 *
28
 * @property string $usernamePrefixed
29
 * @property string $displayName
30
 *
31
 * @property AccountStats $lastAccountStats
32
 * @property AccountStats $beforeLastAccountStats
33
 * @property AccountStats $beforeMonthAccountStats
34
 * @property AccountStats[] $monthAccountStats
35
 *
36
 * @property Proxy $proxy
37
 * @property Tag $proxyTag
38
 * @property AccountStats[] $accountStats
39
 * @property AccountTag[] $accountTags
40
 * @property Tag[] $tags
41
 * @property Media[] $media
42
 * @property MediaAccount[] $mediaAccounts
43
 */
44
class Account extends \yii\db\ActiveRecord
45
{
46
    public $occurs;
47
48
    /**
49
     * @var \app\models\AccountStats
50
     */
51
    protected $lastAccountStats;
52
53
    /**
54
     * @var \app\models\AccountStats
55
     */
56
    protected $beforeLastAccountStats;
57
58
    /**
59
     * @var \app\models\AccountStats
60
     */
61
    protected $beforeMonthAccountStats;
62
63
    /**
64
     * @var \app\models\AccountStats[]
65
     */
66
    private $statsCache;
67
68
    public function resetStatsCache()
69
    {
70
        $this->statsCache = null;
71
        $this->lastAccountStats = null;
72
        $this->beforeLastAccountStats = null;
73
        $this->beforeMonthAccountStats = null;
74
    }
75
76
    public function monthlyChange($attribute)
77
    {
78
        $last = $this->getLastAccountStats();
79
        $beforeMonth = $this->getBeforeMonthAccountStats();
80
        if (!$beforeMonth) {
81
            return 0;
82
        }
83
84
        return $last->$attribute - $beforeMonth->$attribute;
85
    }
86
87
    public function lastChange($attribute)
88
    {
89
        $last = $this->getLastAccountStats();
90
        $beforeLast = $this->getBeforeLastAccountStats();
91
        if (!$beforeLast) {
92
            return 0;
93
        }
94
95
        return $last->$attribute - $beforeLast->$attribute;
96
    }
97
98
    public function getBeforeMonthAccountStats()
99
    {
100
        if ($this->beforeMonthAccountStats) {
101
            return $this->beforeMonthAccountStats;
102
        }
103
        if (!$this->statsCache) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->statsCache of type app\models\AccountStats[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
104
            $this->statsCache = $this->getMonthAccountStats();
105
        }
106
        if (count($this->statsCache) >= 2) {
107
            return $this->beforeMonthAccountStats = end($this->statsCache);
108
        }
109
110
        return null;
111
    }
112
113
    public function getBeforeLastAccountStats()
114
    {
115
        if ($this->beforeLastAccountStats) {
116
            return $this->beforeLastAccountStats;
117
        }
118
        if (!$this->statsCache) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->statsCache of type app\models\AccountStats[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
119
            $this->statsCache = $this->getMonthAccountStats();
120
        }
121
        if (count($this->statsCache) >= 2) {
122
            return $this->beforeLastAccountStats = $this->statsCache['1'];
123
        }
124
125
        return null;
126
    }
127
128
    public function getLastAccountStats()
129
    {
130
        if ($this->lastAccountStats) {
131
            return $this->lastAccountStats;
132
        }
133
        if (!$this->statsCache) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->statsCache of type app\models\AccountStats[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
134
            $this->statsCache = $this->getMonthAccountStats();
135
        }
136
        if (count($this->statsCache) >= 1) {
137
            return $this->lastAccountStats = $this->statsCache['0'];
138
        }
139
140
        return null;
141
    }
142
143
    /**
144
     * @return \app\models\AccountStats[]
145
     */
146
    public function getMonthAccountStats()
147
    {
148
        return $this->getAccountStats()
149
            ->andWhere(new Expression('account_stats.created_at >= DATE_SUB(NOW(), INTERVAL 1 MONTH)'))
150
            ->orderBy('account_stats.id DESC')
151
            ->all();
152
    }
153
154
    public static function usedTags()
155
    {
156
        return Tag::find()
157
            ->innerJoin('account_tag', 'tag.id=account_tag.tag_id')
158
            ->orderBy('tag.slug ASC')
159
            ->all();
160
    }
161
162
    public function behaviors()
163
    {
164
        return ArrayHelper::merge(parent::behaviors(), [
165
            'time' => TimestampBehavior::class,
166
        ]);
167
    }
168
169
    public function getDisplayName()
170
    {
171
        return $this->name ?: $this->getUsernamePrefixed();
172
    }
173
174
    public function getUsernamePrefixed()
175
    {
176
        return "@{$this->username}";
177
    }
178
179
    /**
180
     * @inheritdoc
181
     */
182
    public static function tableName()
183
    {
184
        return 'account';
185
    }
186
187
    /**
188
     * @inheritdoc
189
     */
190
    public function rules()
191
    {
192
        return [
193
            [['username'], 'required'],
194
            [['updated_at', 'created_at'], 'safe'],
195
            [['monitoring', 'proxy_id', 'proxy_tag_id', 'occurs'], 'integer'],
196
            [['name', 'username', 'profile_pic_url', 'full_name', 'biography', 'external_url', 'instagram_id', 'notes'], 'string', 'max' => 255],
197
            [['username'], 'unique'],
198
            [['proxy_id'], 'exist', 'skipOnError' => true, 'targetClass' => Proxy::class, 'targetAttribute' => ['proxy_id' => 'id']],
199
            [['proxy_tag_id'], 'exist', 'skipOnError' => true, 'targetClass' => Tag::class, 'targetAttribute' => ['proxy_tag_id' => 'id']],
200
        ];
201
    }
202
203
    /**
204
     * @inheritdoc
205
     */
206
    public function attributeLabels()
207
    {
208
        return [
209
            'id' => 'ID',
210
            'name' => 'Name',
211
            'username' => 'Username',
212
            'profile_pic_url' => 'Profile Pic Url',
213
            'full_name' => 'Full Name',
214
            'biography' => 'Biography',
215
            'external_url' => 'External Url',
216
            'instagram_id' => 'Instagram ID',
217
            'updated_at' => 'Updated At',
218
            'created_at' => 'Created At',
219
            'monitoring' => 'Monitoring',
220
            'proxy_id' => 'Proxy ID',
221
            'proxy_tag_id' => 'Proxy Tag ID',
222
            'notes' => 'Notes',
223
        ];
224
    }
225
226
    public function getProxy()
227
    {
228
        if ($this->proxy_id) {
229
            return $this->hasOne(Proxy::class, ['id' => 'proxy_id']);
230
        }
231
232
        if ($this->proxy_tag_id) {
233
            return Proxy::find()
234
                ->innerJoinWith('proxyTags')
235
                ->andWhere(['proxy_tag.tag_id' => $this->proxy_tag_id])
236
                ->orderBy(new Expression('RAND()'))
237
                ->one();
238
        }
239
240
        return Proxy::find()
241
            ->defaultForAccounts()
242
            ->orderBy(new Expression('RAND()'))
243
            ->one();
244
    }
245
246
    /**
247
     * @return \yii\db\ActiveQuery
248
     */
249
    public function getProxyTag()
250
    {
251
        return $this->hasOne(Tag::class, ['id' => 'proxy_tag_id']);
252
    }
253
254
    /**
255
     * @return \yii\db\ActiveQuery
256
     */
257
    public function getAccountStats()
258
    {
259
        return $this->hasMany(AccountStats::class, ['account_id' => 'id']);
260
    }
261
262
    /**
263
     * @return \yii\db\ActiveQuery
264
     */
265
    public function getAccountTags()
266
    {
267
        return $this->hasMany(AccountTag::class, ['account_id' => 'id']);
268
    }
269
270
    /**
271
     * @return \yii\db\ActiveQuery
272
     */
273
    public function getTags()
274
    {
275
        return $this->hasMany(Tag::class, ['id' => 'tag_id'])->via('accountTags');
276
    }
277
278
    /**
279
     * @return \yii\db\ActiveQuery
280
     */
281
    public function getMedia()
282
    {
283
        return $this->hasMany(Media::class, ['account_id' => 'id']);
284
    }
285
286
    /**
287
     * @return \yii\db\ActiveQuery
288
     */
289
    public function getMediaAccounts()
290
    {
291
        return $this->hasMany(MediaAccount::class, ['account_id' => 'id']);
292
    }
293
294
    /**
295
     * @inheritdoc
296
     * @return AccountQuery the active query used by this AR class.
297
     */
298
    public static function find()
299
    {
300
        return new AccountQuery(get_called_class());
301
    }
302
}
303