Passed
Push — master ( 6b49ff...b93f48 )
by Paweł
05:24
created

AccountManager::update()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 42
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 21
nc 6
nop 1
dl 0
loc 42
rs 8.439
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A AccountManager::monitor() 0 14 2
1
<?php
2
/**
3
 * Created for IG Monitoring.
4
 * User: jakim <[email protected]>
5
 * Date: 28.04.2018
6
 */
7
8
namespace app\components;
9
10
11
use app\components\traits\FindOrCreate;
12
use app\models\Account;
13
use app\models\Media;
14
use app\models\MediaAccount;
15
use yii\base\Component;
16
use yii\helpers\StringHelper;
17
18
class AccountManager extends Component
19
{
20
    use FindOrCreate;
21
22
    public function monitorRelatedAccounts(Account $parent, array $accounts)
23
    {
24
        foreach ($accounts as $account) {
25
            if (is_string($account)) {
26
                /** @var Account $account */
27
                $account = $this->findOrCreate(['username' => $account], Account::class);
28
            }
29
            //calculation monitoring level
30
            if ($parent->accounts_monitoring_level > 1) {
31
                $level = $parent->accounts_monitoring_level - 1;
32
                if ($level > $account->accounts_monitoring_level) {
33
                    $account->accounts_monitoring_level = $level;
34
                }
35
            }
36
37
            $this->monitor($account, $parent->proxy_id, $parent->proxy_tag_id);
38
39
            $tags = empty($parent->accounts_default_tags) ? $parent->tags : StringHelper::explode($parent->accounts_default_tags, ',', true, true);
40
            $tagManager = \Yii::createObject(TagManager::class);
41
            $tagManager->saveForAccount($account, $tags);
42
        }
43
    }
44
45
    public function monitor($account, $proxyId = null, $proxyTagId = null): Account
46
    {
47
        if (is_string($account)) {
48
            /** @var Account $account */
49
            $account = $this->findOrCreate(['username' => $account], Account::class);
50
        }
51
52
        $account->proxy_id = $proxyId;
53
        $account->proxy_tag_id = $proxyTagId;
54
        $account->monitoring = 1;
55
56
        $account->save();
57
58
        return $account;
59
    }
60
61
    public function saveForMedia(Media $media, array $usernames)
62
    {
63
        $this->saveUsernames($usernames);
64
65
        $createdAt = (new \DateTime())->format('Y-m-d H:i:s');
66
        $rows = array_map(function ($id) use ($media, $createdAt) {
67
            return [
68
                $media->id,
69
                $id,
70
                $createdAt,
71
            ];
72
        }, Account::find()
73
            ->andWhere(['username' => $usernames])
74
            ->column());
75
76
        $sql = \Yii::$app->db->queryBuilder
77
            ->batchInsert(MediaAccount::tableName(), ['media_id', 'account_id', 'created_at'], $rows);
78
        $sql = str_replace('INSERT INTO ', 'INSERT IGNORE INTO ', $sql);
79
        \Yii::$app->db->createCommand($sql)
80
            ->execute();
81
    }
82
83
    /**
84
     * @param array|string[] $usernames
85
     * @throws \yii\db\Exception
86
     */
87
    public function saveUsernames(array $usernames)
88
    {
89
        $createdAt = (new \DateTime())->format('Y-m-d H:i:s');
90
        $rows = array_map(function ($username) use ($createdAt) {
91
            return [
92
                $username,
93
                $createdAt,
94
                $createdAt,
95
            ];
96
        }, $usernames);
97
98
        $sql = \Yii::$app->db->getQueryBuilder()
99
            ->batchInsert(Account::tableName(), ['username', 'updated_at', 'created_at'], $rows);
100
        $sql = str_replace('INSERT INTO ', 'INSERT IGNORE INTO ', $sql);
101
        \Yii::$app->db->createCommand($sql)
102
            ->execute();
103
    }
104
}