ProjectConfig   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 25
eloc 107
c 3
b 0
f 0
dl 0
loc 162
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B onProjectConfigRebuild() 0 45 11
C onProjectConfigChange() 0 77 11
A onProjectConfigDelete() 0 18 3
1
<?php
2
3
namespace mmikkel\cacheflag\services;
4
5
use Craft;
6
use craft\base\Component;
7
use craft\db\Query;
8
use craft\db\Table;
9
use craft\events\ConfigEvent;
10
use craft\events\RebuildConfigEvent;
11
use craft\helpers\Db;
12
13
use mmikkel\cacheflag\records\Flags;
14
15
/**
16
 * Class ProjectConfig
17
 * @package mmikkel\cacheflag\services
18
 * @since 1.2.0
19
 */
20
class ProjectConfig extends Component
21
{
22
23
    /**
24
     * @param ConfigEvent $event
25
     * @throws \yii\db\Exception
26
     */
27
    public function onProjectConfigChange(ConfigEvent $event)
28
    {
29
30
        $uid = $event->tokenMatches[0];
31
32
        $query = (new Query())
33
            ->select(['id'])
34
            ->from(Flags::tableName())
35
            ->where(['uid' => $uid]);
36
37
        $source = \explode(':', $event->newValue['source']);
38
        $sourceKey = $source[0] ?? null;
39
        $sourceValue = $source[1] ?? null;
40
41
        if (!$sourceKey || !$sourceValue) {
42
            return;
43
        }
44
45
        switch ($sourceKey) {
46
            case 'section':
47
                $column = 'sectionId';
48
                $value = (int)Db::idByUid(Table::SECTIONS, $sourceValue);
49
                break;
50
            case 'categoryGroup':
51
                $column = 'categoryGroupId';
52
                $value = (int)Db::idByUid(Table::CATEGORYGROUPS, $sourceValue);
53
                break;
54
            case 'tagGroup':
55
                $column = 'tagGroupId';
56
                $value = (int)Db::idByUid(Table::TAGGROUPS, $sourceValue);
57
                break;
58
            case 'userGroup':
59
                $column = 'userGroupId';
60
                $value = (int)Db::idByUid(Table::USERGROUPS, $sourceValue);
61
                break;
62
            case 'volume':
63
                $column = 'volumeId';
64
                $value = (int)Db::idByUid(Table::VOLUMES, $sourceValue);
65
                break;
66
            case 'globalSet':
67
                $column = 'globalSetId';
68
                $value = (int)Db::idByUid(Table::GLOBALSETS, $sourceValue);
69
                break;
70
            case 'elementType':
71
                $column = 'elementType';
72
                $value = $sourceValue;
73
                break;
74
            default:
75
                return;
76
        }
77
78
        $query->orWhere([$column => $value]);
79
80
        $id = $query->scalar();
81
82
        $isNew = empty($id);
83
84
        if ($isNew) {
85
86
            $flags = $event->newValue['flags'];
87
88
            Craft::$app->db->createCommand()
89
                ->insert(Flags::tableName(), [
90
                    'flags' => $flags,
91
                    $column => $value,
92
                    'uid' => $uid,
93
                ])
94
                ->execute();
95
96
        } else {
97
98
            Craft::$app->db->createCommand()
99
                ->update(Flags::tableName(), [
100
                    'flags' => $event->newValue['flags'],
101
                    'uid' => $uid,
102
                ], ['id' => $id])
103
                ->execute();
104
        }
105
106
    }
107
108
    /**
109
     * @param ConfigEvent $event
110
     * @return void
111
     */
112
    public function onProjectConfigDelete(ConfigEvent $event)
113
    {
114
115
        $uid = $event->tokenMatches[0];
116
117
        try {
118
            $id = (new Query())
119
                ->select(['id'])
120
                ->from(Flags::tableName())
121
                ->where(['uid' => $uid])
122
                ->scalar();
123
            if ($id) {
124
                Craft::$app->db->createCommand()
125
                    ->delete(Flags::tableName(), ['id' => $id])
126
                    ->execute();
127
            }
128
        } catch (\Throwable $e) {
129
            Craft::error($e, __METHOD__);
130
        }
131
    }
132
133
    /**
134
     * @param RebuildConfigEvent $event
135
     * @return void
136
     */
137
    public function onProjectConfigRebuild(RebuildConfigEvent $event)
138
    {
139
140
        Craft::$app->getProjectConfig()->remove('cacheFlags');
141
142
        $rows = (new Query())
143
            ->select(['flags', 'sectionId', 'categoryGroupId', 'tagGroupId', 'userGroupId', 'volumeId', 'globalSetId', 'elementType', 'uid'])
144
            ->from(Flags::tableName())
145
            ->all();
146
147
        foreach ($rows as $row) {
148
149
            $sourceKey = null;
150
            $sourceValue = null;
151
152
            if ($row['sectionId']) {
153
                $sourceKey = 'section';
154
                $sourceValue = Db::uidById(Table::SECTIONS, $row['sectionId']);
155
            } else if ($row['categoryGroupId']) {
156
                $sourceKey = 'categoryGroup';
157
                $sourceValue = Db::uidById(Table::CATEGORYGROUPS, $row['categoryGroupId']);
158
            } else if ($row['tagGroupId']) {
159
                $sourceKey = 'tagGroup';
160
                $sourceValue = Db::uidById(Table::TAGGROUPS, $row['tagGroupId']);
161
            } else if ($row['userGroupId']) {
162
                $sourceKey = 'userGroup';
163
                $sourceValue = Db::uidById(Table::USERGROUPS, $row['userGroupId']);
164
            } else if ($row['volumeId']) {
165
                $sourceKey = 'volume';
166
                $sourceValue = Db::uidById(Table::VOLUMES, $row['volumeId']);
167
            } else if ($row['globalSetId']) {
168
                $sourceKey = 'globalSet';
169
                $sourceValue = Db::uidById(Table::GLOBALSETS, $row['globalSetId']);
170
            } else if ($row['elementType']) {
171
                $sourceKey = 'elementType';
172
                $sourceValue = $row['elementType'];
173
            }
174
175
            if (!$sourceKey || !$sourceValue) {
176
                return;
177
            }
178
179
            $event->config['cacheFlags'][$row['uid']] = [
180
                'source' => "$sourceKey:$sourceValue",
181
                'flags' => $row['flags'],
182
            ];
183
        }
184
185
    }
186
187
}
188