Completed
Push — master ( 87f5f3...5d6f7d )
by Nate
03:14
created

ProjectConfigHandler   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 204
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 6
dl 0
loc 204
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A handleChangedProvider() 0 51 2
A handleDeletedProvider() 0 31 2
A handleChangedToken() 0 51 2
A handleDeletedToken() 0 31 2
A rebuild() 0 14 3
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/patron/license
6
 * @link       https://www.flipboxfactory.com/software/patron/
7
 */
8
9
namespace flipbox\patron\events\handlers;
10
11
use Craft;
12
use craft\events\ConfigEvent;
13
use flipbox\patron\events\ManageProviderProjectConfig;
14
use flipbox\patron\events\ManageTokenProjectConfig;
15
use flipbox\patron\records\Provider;
16
use flipbox\patron\records\Token;
17
use yii\base\Event;
18
19
/**
20
 * @author Flipbox Factory <[email protected]>
21
 * @since 2.1.0
22
 */
23
class ProjectConfigHandler
24
{
25
    /**
26
     * @param ConfigEvent $event
27
     */
28
    public static function handleChangedProvider(ConfigEvent $event)
29
    {
30
        Event::off(
31
            Provider::class,
32
            Provider::EVENT_AFTER_INSERT,
33
            [
34
                ManageProviderProjectConfig::class,
35
                'save'
36
            ]
37
        );
38
39
        Event::off(
40
            Provider::class,
41
            Provider::EVENT_AFTER_UPDATE,
42
            [
43
                ManageProviderProjectConfig::class,
44
                'save'
45
            ]
46
        );
47
48
        // Get the UID that was matched in the config path
49
        $uid = $event->tokenMatches[0];
50
51
        if (null === ($provider = Provider::findOne([
52
                'uid' => $uid
53
            ]))) {
54
            $provider = new Provider();
55
        }
56
57
        Craft::configure($provider, $event->newValue);
58
59
        $provider->save();
60
61
        Event::on(
62
            Provider::class,
63
            Provider::EVENT_AFTER_INSERT,
64
            [
65
                ManageProviderProjectConfig::class,
66
                'save'
67
            ]
68
        );
69
70
        Event::on(
71
            Provider::class,
72
            Provider::EVENT_AFTER_UPDATE,
73
            [
74
                ManageProviderProjectConfig::class,
75
                'save'
76
            ]
77
        );
78
    }
79
80
    /**
81
     * @param ConfigEvent $event
82
     * @throws \Throwable
83
     * @throws \yii\db\StaleObjectException
84
     */
85
    public static function handleDeletedProvider(ConfigEvent $event)
86
    {
87
        Event::off(
88
            Provider::class,
89
            Provider::EVENT_AFTER_DELETE,
90
            [
91
                ManageProviderProjectConfig::class,
92
                'delete'
93
            ]
94
        );
95
96
        // Get the UID that was matched in the config path
97
        $uid = $event->tokenMatches[0];
98
99
        if (null === $provider = Provider::findOne([
100
                'uid' => $uid
101
            ])) {
102
            return;
103
        }
104
105
        $provider->delete();
106
107
        Event::on(
108
            Provider::class,
109
            Provider::EVENT_AFTER_DELETE,
110
            [
111
                ManageProviderProjectConfig::class,
112
                'delete'
113
            ]
114
        );
115
    }
116
117
    /**
118
     * @param ConfigEvent $event
119
     */
120
    public static function handleChangedToken(ConfigEvent $event)
121
    {
122
        Event::off(
123
            Token::class,
124
            Token::EVENT_AFTER_INSERT,
125
            [
126
                ManageTokenProjectConfig::class,
127
                'save'
128
            ]
129
        );
130
131
        Event::off(
132
            Token::class,
133
            Token::EVENT_AFTER_UPDATE,
134
            [
135
                ManageTokenProjectConfig::class,
136
                'save'
137
            ]
138
        );
139
140
        // Get the UID that was matched in the config path
141
        $uid = $event->tokenMatches[0];
142
143
        if (null === ($token = Token::findOne([
144
                'uid' => $uid
145
            ]))) {
146
            $token = new Token();
147
        }
148
149
        Craft::configure($token, $event->newValue);
150
151
        $token->save();
152
153
        Event::on(
154
            Token::class,
155
            Token::EVENT_AFTER_INSERT,
156
            [
157
                ManageTokenProjectConfig::class,
158
                'save'
159
            ]
160
        );
161
162
        Event::on(
163
            Token::class,
164
            Token::EVENT_AFTER_UPDATE,
165
            [
166
                ManageTokenProjectConfig::class,
167
                'save'
168
            ]
169
        );
170
    }
171
172
    /**
173
     * @param ConfigEvent $event
174
     * @throws \Throwable
175
     * @throws \yii\db\StaleObjectException
176
     */
177
    public static function handleDeletedToken(ConfigEvent $event)
178
    {
179
        Event::off(
180
            Token::class,
181
            Token::EVENT_AFTER_DELETE,
182
            [
183
                ManageTokenProjectConfig::class,
184
                'delete'
185
            ]
186
        );
187
188
        // Get the UID that was matched in the config path
189
        $uid = $event->tokenMatches[0];
190
191
        if (null === $token = Token::findOne([
192
                'uid' => $uid
193
            ])) {
194
            return;
195
        }
196
197
        $token->delete();
198
199
        Event::on(
200
            Token::class,
201
            Token::EVENT_AFTER_DELETE,
202
            [
203
                ManageTokenProjectConfig::class,
204
                'delete'
205
            ]
206
        );
207
    }
208
209
    /**
210
     * @return array
211
     */
212
    public static function rebuild(): array
213
    {
214
        $return = [];
215
216
        foreach (Provider::findAll([]) as $provider) {
217
            $return['patronProviders'][$provider->uid] = $provider->toProjectConfig();
218
        }
219
220
        foreach (Token::findAll([]) as $token) {
221
            $return['patronProviders'][$token->uid] = $token->toProjectConfig();
222
        }
223
224
        return $return;
225
    }
226
}
227