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

CategoryManager::addToAccount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 3
dl 0
loc 13
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created for IG Monitoring.
4
 * User: jakim <[email protected]>
5
 * Date: 2019-01-31
6
 */
7
8
namespace app\components;
9
10
11
use app\components\traits\BatchInsertCommand;
12
use app\models\Account;
13
use app\models\AccountCategory;
14
use app\models\Category;
15
use app\models\Tag;
16
use app\models\User;
17
use yii\base\BaseObject;
18
19
class CategoryManager extends BaseObject
20
{
21
    use BatchInsertCommand;
0 ignored issues
show
introduced by
The trait app\components\traits\BatchInsertCommand requires some properties which are not provided by app\components\CategoryManager: $db, $queryBuilder
Loading history...
22
23
    public function getForUser(User $user, bool $asInt = false)
24
    {
25
        $q = Category::find()
26
            ->distinct()
27
            ->andWhere(['or',
28
                ['category.id' => $user->getAccountCategories()->select('account_category.category_id')],
29
//                ['category.id' => $user->getTagCategories()->select('tag_category.category_id')],
30
            ]);
31
32
        return $asInt ? $q->select('category.id')->column() : $q->all();
33
    }
34
35
    /**
36
     * @param \app\models\User $user
37
     * @param Account|Account[]|null $accounts
38
     * @param bool $asInt
39
     * @return Category[]|int[]
40
     */
41
    public function getForUserAccounts(User $user, $accounts = null, bool $asInt = false)
42
    {
43
        $accounts = $accounts instanceof Account ? [$accounts->id] : ArrayHelper::getColumn($accounts, 'id');
44
45
        $q = Category::find()
46
            ->innerJoin('account_category ac', 'category.id=ac.category_id AND ac.user_id=' . (int)$user->id)
47
            ->andFilterWhere(['ac.account_id' => $accounts]);
48
49
50
        return $asInt ? $q->select('category.id')->column() : $q->all();
51
    }
52
53
    /**
54
     * @param \app\models\User $user
55
     * @param Tag|Tag[]|null $tags
56
     * @param bool $asInt
57
     * @return Category[]|int[]
58
     */
59
    public function getForUserTags(User $user, $tags = null, bool $asInt = false)
60
    {
61
        $tags = $tags instanceof Tag ? [$tags->id] : ArrayHelper::getColumn($tags, 'id');
62
63
        $q = Category::find()
64
            ->innerJoin('tag_category ac', 'category.id=ac.category_id AND ac.user_id=' . (int)$user->id)
65
            ->andFilterWhere(['ac.tag_id' => $tags]);
66
67
68
        return $asInt ? $q->select('category.id')->column() : $q->all();
69
    }
70
71
    /**
72
     * It deletes the previous ones and sets new ones.
73
     *
74
     * @param \app\models\Account $account
75
     * @param array $categories
76
     * @param \app\models\User $user
77
     * @throws \yii\db\Exception
78
     */
79
    public function saveForAccount(Account $account, array $categories, User $user)
80
    {
81
        AccountCategory::deleteAll([
82
            'AND',
83
            [
84
                'account_id' => $account->id,
85
                'user_id' => $user->id,
86
            ],
87
            ['NOT', ['category_id' => Category::find()->andWhere(['name' => $categories])->column()]],
88
        ]);
89
        $this->addToAccount($account, $categories, $user);
90
    }
91
92
    /**
93
     * Adds new ones.
94
     *
95
     * @param \app\models\Account $account
96
     * @param array $categories
97
     * @param \app\models\User $user
98
     * @throws \yii\db\Exception
99
     */
100
    public function addToAccount(Account $account, array $categories, User $user)
101
    {
102
        $this->saveCategories($categories);
103
        $rows = array_map(function ($categoryId) use ($account, $user) {
104
            return [
105
                $account->id,
106
                $categoryId,
107
                $user->id,
108
            ];
109
        }, Category::find()->andWhere(['name' => $categories])->column());
110
111
        $this->batchInsertIgnoreCommand(AccountCategory::tableName(), ['account_id', 'category_id', 'user_id'], $rows)
112
            ->execute();
113
    }
114
115
//    /**
116
//     * It deletes the previous ones and sets new ones.
117
//     *
118
//     * @param \app\models\Tag $tag
119
//     * @param array $categories
120
//     * @param \app\models\User $user
121
//     * @throws \yii\db\Exception
122
//     */
123
//    public function saveForTag(Tag $tag, array $categories, User $user)
124
//    {
125
//        TagCategory::deleteAll([
126
//            'AND',
127
//            [
128
//                'tag_id' => $tag->id,
129
//                'user_id' => $user->id,
130
//            ],
131
//            ['NOT', ['category_id' => Category::find()->andWhere(['name' => $categories])->column()]],
132
//        ]);
133
//        $this->addToTag($tag, $categories, $user);
134
//    }
135
136
//    /**
137
//     * Adds new ones.
138
//     *
139
//     * @param \app\models\Tag $tag
140
//     * @param array $categories
141
//     * @param \app\models\User $user
142
//     * @throws \yii\db\Exception
143
//     */
144
//    public function addToTag(Tag $tag, array $categories, User $user)
145
//    {
146
//        $this->saveCategories($categories);
147
//        $rows = array_map(function ($categoryId) use ($tag, $user) {
148
//            return [
149
//                $tag->id,
150
//                $categoryId,
151
//                $user->id,
152
//            ];
153
//        }, Category::find()->andWhere(['name' => $categories])->column());
154
//
155
//        $this->batchInsertIgnoreCommand(TagCategory::tableName(), ['tag_id', 'category_id', 'user_id'], $rows)
156
//            ->execute();
157
//    }
158
159
160
    public function saveCategories(array $categories)
161
    {
162
        $rows = array_map(function ($category) {
163
            return [$category];
164
        }, $categories);
165
166
        $this->batchInsertIgnoreCommand(Category::tableName(), ['name'], $rows)
167
            ->execute();
168
    }
169
}