ProjectConfigHandler::handleChangedProvider()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 9.069
c 0
b 0
f 0
cc 2
nc 2
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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);
0 ignored issues
show
Bug introduced by
It seems like $provider defined by \flipbox\patron\records\...e(array('uid' => $uid)) on line 51 can also be of type array; however, yii\BaseYii::configure() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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
                'enabled' => null
102
            ])) {
103
            return;
104
        }
105
106
        $provider->delete();
107
108
        Event::on(
109
            Provider::class,
110
            Provider::EVENT_AFTER_DELETE,
111
            [
112
                ManageProviderProjectConfig::class,
113
                'delete'
114
            ]
115
        );
116
    }
117
118
    /**
119
     * @param ConfigEvent $event
120
     */
121
    public static function handleChangedToken(ConfigEvent $event)
122
    {
123
        Event::off(
124
            Token::class,
125
            Token::EVENT_AFTER_INSERT,
126
            [
127
                ManageTokenProjectConfig::class,
128
                'save'
129
            ]
130
        );
131
132
        Event::off(
133
            Token::class,
134
            Token::EVENT_AFTER_UPDATE,
135
            [
136
                ManageTokenProjectConfig::class,
137
                'save'
138
            ]
139
        );
140
141
        // Get the UID that was matched in the config path
142
        $uid = $event->tokenMatches[0];
143
144
        if (null === ($token = Token::findOne([
145
                'uid' => $uid,
146
                'enabled' => null
147
            ]))) {
148
            $token = new Token();
149
        }
150
151
        // Compare dates from config
152
        $configDateUpdated = $event->newValue['dateUpdated'] ?? null;
153
        $tokenDateUpdated = $token->dateUpdated ?? null;
154
155
        // If the token has been updated more recently in the database, use it
156
        if ($configDateUpdated && $tokenDateUpdated && strtotime($tokenDateUpdated) > strtotime($configDateUpdated)) {
157
            $event->newValue = array_merge(
158
                $event->newValue,
159
                [
160
                    'accessToken' => $token->accessToken,
161
                ]
162
            );
163
        }
164
165
        // Ignore
166
        unset($event->newValue['dateUpdated']);
167
168
        Craft::configure($token, $event->newValue);
0 ignored issues
show
Bug introduced by
It seems like $token defined by \flipbox\patron\records\...id, 'enabled' => null)) on line 144 can also be of type array; however, yii\BaseYii::configure() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
169
170
        $token->save();
171
172
        Event::on(
173
            Token::class,
174
            Token::EVENT_AFTER_INSERT,
175
            [
176
                ManageTokenProjectConfig::class,
177
                'save'
178
            ]
179
        );
180
181
        Event::on(
182
            Token::class,
183
            Token::EVENT_AFTER_UPDATE,
184
            [
185
                ManageTokenProjectConfig::class,
186
                'save'
187
            ]
188
        );
189
    }
190
191
    /**
192
     * @param ConfigEvent $event
193
     * @throws \Throwable
194
     * @throws \yii\db\StaleObjectException
195
     */
196
    public static function handleDeletedToken(ConfigEvent $event)
197
    {
198
        Event::off(
199
            Token::class,
200
            Token::EVENT_AFTER_DELETE,
201
            [
202
                ManageTokenProjectConfig::class,
203
                'delete'
204
            ]
205
        );
206
207
        // Get the UID that was matched in the config path
208
        $uid = $event->tokenMatches[0];
209
210
        if (null === $token = Token::findOne([
211
                'uid' => $uid
212
            ])) {
213
            return;
214
        }
215
216
        $token->delete();
217
218
        Event::on(
219
            Token::class,
220
            Token::EVENT_AFTER_DELETE,
221
            [
222
                ManageTokenProjectConfig::class,
223
                'delete'
224
            ]
225
        );
226
    }
227
228
    /**
229
     * @return array
230
     */
231
    public static function rebuild(): array
232
    {
233
        $return = [];
234
235
        foreach (Provider::findAll(['enabled' => null]) as $provider) {
236
            $return['plugins.patron.providers'][$provider->uid] = $provider->toProjectConfig();
237
        }
238
239
        foreach (Token::findAll(['enabled' => null]) as $token) {
240
            $return['plugins.patron.tokens'][$token->uid] = $token->toProjectConfig();
241
        }
242
243
        return $return;
244
    }
245
}
246