Passed
Push — master ( b2168e...f1daef )
by Paweł
03:26
created

AccountManager::findByCategories()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 4
nop 2
dl 0
loc 19
rs 9.8333
c 0
b 0
f 0
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\BatchInsertCommand;
12
use app\components\traits\FindOrCreate;
13
use app\components\updaters\AccountUpdater;
14
use app\models\Account;
15
use app\models\AccountCategory;
16
use app\models\Media;
17
use app\models\MediaAccount;
18
use DateTime;
19
use Yii;
20
use yii\base\Component;
21
use yii\helpers\StringHelper;
22
use function is_string;
23
24
class AccountManager extends Component
25
{
26
    use FindOrCreate, BatchInsertCommand;
0 ignored issues
show
introduced by
The trait app\components\traits\BatchInsertCommand requires some properties which are not provided by app\components\AccountManager: $db, $queryBuilder
Loading history...
27
28
    public function startMonitoring($account, $proxyId = null): Account
29
    {
30
        if (is_string($account)) {
31
            /** @var Account $account */
32
            $account = $this->findOrCreate(['username' => $account], Account::class);
33
        }
34
35
        $accountUpdater = Yii::createObject([
36
            'class' => AccountUpdater::class,
37
            'account' => $account,
38
        ]);
39
        $accountUpdater
40
            ->setMonitoring($proxyId)
41
            ->setIsValid()
42
            ->save();
43
44
        return $account;
45
    }
46
47
    public function addToMedia(Media $media, array $usernames)
48
    {
49
        $this->saveUsernames($usernames);
50
51
        $accounts = Account::find()
52
            ->andWhere(['username' => $usernames])
53
            ->column();
54
55
        $rows = array_map(function ($id) use ($media) {
56
            return [
57
                $media->id,
58
                $id,
59
                $media->taken_at,
60
            ];
61
        }, $accounts);
62
63
        $this->batchInsertIgnoreCommand(MediaAccount::tableName(), ['media_id', 'account_id', 'created_at'], $rows)
64
            ->execute();
65
    }
66
67
    /**
68
     * @param array|string[] $usernames
69
     * @throws \yii\db\Exception
70
     */
71
    public function saveUsernames(array $usernames)
72
    {
73
        $createdAt = (new DateTime())->format('Y-m-d H:i:s');
74
        $rows = array_map(function ($username) use ($createdAt) {
75
            return [
76
                $username,
77
                $createdAt,
78
                $createdAt,
79
            ];
80
        }, $usernames);
81
82
        $this->batchInsertIgnoreCommand(Account::tableName(), ['username', 'updated_at', 'created_at'], $rows)
83
            ->execute();
84
    }
85
86
    /**
87
     * @param string|string[] $categories
88
     * @param null|int $userId
89
     * @return array|int[]
90
     */
91
    public function findByCategories($categories, $userId = null): array
92
    {
93
        if (is_string($categories)) {
94
            $categories = StringHelper::explode($categories, ',', true, true);
95
            $categories = array_unique($categories);
96
        }
97
98
        $ids = [];
99
        foreach ($categories as $category) {
100
            $ids[] = AccountCategory::find()
101
                ->distinct()
102
                ->select('account_id')
103
                ->innerJoinWith('category')
104
                ->andFilterWhere(['user_id' => $userId])
105
                ->andFilterWhere(['like', 'category.name', $category])
106
                ->column();
107
        }
108
109
        return array_intersect(...$ids, ...$ids);
110
    }
111
}