Passed
Push — master ( 01a84c...98164f )
by Paweł
03:03
created

Account::monthlyChange()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
rs 9.6666
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
 *
33
 * @property Proxy $proxy
34
 * @property Tag $proxyTag
35
 * @property AccountStats[] $accountStats
36
 * @property AccountTag[] $accountTags
37
 * @property Tag[] $tags
38
 * @property Media[] $media
39
 * @property MediaAccount[] $mediaAccounts
40
 */
41
class Account extends \yii\db\ActiveRecord
42
{
43
    public $occurs;
44
45
    public static function usedTags()
46
    {
47
        return Tag::find()
48
            ->innerJoin('account_tag', 'tag.id=account_tag.tag_id')
49
            ->orderBy('tag.slug ASC')
50
            ->all();
51
    }
52
53
    public function behaviors()
54
    {
55
        return ArrayHelper::merge(parent::behaviors(), [
56
            'time' => TimestampBehavior::class,
57
        ]);
58
    }
59
60
    public function getDisplayName()
61
    {
62
        return $this->name ?: $this->getUsernamePrefixed();
63
    }
64
65
    public function getUsernamePrefixed()
66
    {
67
        return "@{$this->username}";
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73
    public static function tableName()
74
    {
75
        return 'account';
76
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81
    public function rules()
82
    {
83
        return [
84
            [['username'], 'required'],
85
            [['updated_at', 'created_at'], 'safe'],
86
            [['monitoring', 'proxy_id', 'proxy_tag_id', 'occurs'], 'integer'],
87
            [['name', 'username', 'profile_pic_url', 'full_name', 'biography', 'external_url', 'instagram_id', 'notes'], 'string', 'max' => 255],
88
            [['username'], 'unique'],
89
            [['proxy_id'], 'exist', 'skipOnError' => true, 'targetClass' => Proxy::class, 'targetAttribute' => ['proxy_id' => 'id']],
90
            [['proxy_tag_id'], 'exist', 'skipOnError' => true, 'targetClass' => Tag::class, 'targetAttribute' => ['proxy_tag_id' => 'id']],
91
        ];
92
    }
93
94
    /**
95
     * @inheritdoc
96
     */
97
    public function attributeLabels()
98
    {
99
        return [
100
            'id' => 'ID',
101
            'name' => 'Name',
102
            'username' => 'Username',
103
            'profile_pic_url' => 'Profile Pic Url',
104
            'full_name' => 'Full Name',
105
            'biography' => 'Biography',
106
            'external_url' => 'External Url',
107
            'instagram_id' => 'Instagram ID',
108
            'updated_at' => 'Updated At',
109
            'created_at' => 'Created At',
110
            'monitoring' => 'Monitoring',
111
            'proxy_id' => 'Proxy ID',
112
            'proxy_tag_id' => 'Proxy Tag ID',
113
            'notes' => 'Notes',
114
        ];
115
    }
116
117
    public function getProxy()
118
    {
119
        if ($this->proxy_id) {
120
            return $this->hasOne(Proxy::class, ['id' => 'proxy_id']);
121
        }
122
123
        if ($this->proxy_tag_id) {
124
            return Proxy::find()
125
                ->innerJoinWith('proxyTags')
126
                ->andWhere(['proxy_tag.tag_id' => $this->proxy_tag_id])
127
                ->orderBy(new Expression('RAND()'))
128
                ->one();
129
        }
130
131
        return Proxy::find()
132
            ->defaultForAccounts()
133
            ->orderBy(new Expression('RAND()'))
134
            ->one();
135
    }
136
137
    /**
138
     * @return \yii\db\ActiveQuery
139
     */
140
    public function getProxyTag()
141
    {
142
        return $this->hasOne(Tag::class, ['id' => 'proxy_tag_id']);
143
    }
144
145
    /**
146
     * @return \yii\db\ActiveQuery
147
     */
148
    public function getAccountStats()
149
    {
150
        return $this->hasMany(AccountStats::class, ['account_id' => 'id']);
151
    }
152
153
    /**
154
     * @return \yii\db\ActiveQuery
155
     */
156
    public function getLastAccountStats()
157
    {
158
        return $this->hasOne(AccountStats::class, ['account_id' => 'id'])
159
            ->orderBy('account_stats.id DESC')
160
            ->limit(1);
161
    }
162
163
    /**
164
     * @return \yii\db\ActiveQuery
165
     */
166
    public function getAccountTags()
167
    {
168
        return $this->hasMany(AccountTag::class, ['account_id' => 'id']);
169
    }
170
171
    /**
172
     * @return \yii\db\ActiveQuery
173
     */
174
    public function getTags()
175
    {
176
        return $this->hasMany(Tag::class, ['id' => 'tag_id'])->via('accountTags');
177
    }
178
179
    /**
180
     * @return \yii\db\ActiveQuery
181
     */
182
    public function getMedia()
183
    {
184
        return $this->hasMany(Media::class, ['account_id' => 'id']);
185
    }
186
187
    /**
188
     * @return \yii\db\ActiveQuery
189
     */
190
    public function getMediaAccounts()
191
    {
192
        return $this->hasMany(MediaAccount::class, ['account_id' => 'id']);
193
    }
194
195
    /**
196
     * @inheritdoc
197
     * @return AccountQuery the active query used by this AR class.
198
     */
199
    public static function find()
200
    {
201
        return new AccountQuery(get_called_class());
202
    }
203
}
204