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\BatchInsertCommandTrait; |
||
12 | use app\components\traits\FindOrCreateTrait; |
||
13 | use app\components\builders\AccountBuilder; |
||
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 FindOrCreateTrait, BatchInsertCommandTrait; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
27 | |||
28 | public function startMonitoring($account): 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' => AccountBuilder::class, |
||
37 | 'account' => $account, |
||
38 | ]); |
||
39 | $accountUpdater |
||
40 | ->setMonitoring() |
||
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 | } |