Completed
Push — develop ( adf8b4...de1a14 )
by Nate
13:19
created

ProjectConfigHandler   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 204
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A handleChangedOrganizationType() 0 51 2
A handleDeletedOrganizationType() 0 31 2
A handleChangedUserType() 0 51 2
A handleDeletedUserType() 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\organizations\events\handlers;
10
11
use Craft;
12
use craft\events\ConfigEvent;
13
use flipbox\organizations\events\ManageOrganizationTypeProjectConfig;
14
use flipbox\organizations\events\ManageUserTypeProjectConfig;
15
use flipbox\organizations\records\OrganizationType;
16
use flipbox\organizations\records\UserType;
17
use yii\base\Event;
18
19
/**
20
 * @author Flipbox Factory <[email protected]>
21
 * @since 3.0.0
22
 */
23
class ProjectConfigHandler
24
{
25
    /**
26
     * @param ConfigEvent $event
27
     */
28
    public static function handleChangedOrganizationType(ConfigEvent $event)
29
    {
30
        Event::off(
31
            OrganizationType::class,
32
            OrganizationType::EVENT_AFTER_INSERT,
33
            [
34
                ManageOrganizationTypeProjectConfig::class,
35
                'save'
36
            ]
37
        );
38
39
        Event::off(
40
            OrganizationType::class,
41
            OrganizationType::EVENT_AFTER_UPDATE,
42
            [
43
                ManageOrganizationTypeProjectConfig::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 = OrganizationType::findOne([
52
                'uid' => $uid
53
            ]))) {
54
            $provider = new OrganizationType();
55
        }
56
57
        Craft::configure($provider, $event->newValue);
0 ignored issues
show
Bug introduced by
It seems like $provider defined by \flipbox\organizations\r...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
            OrganizationType::class,
63
            OrganizationType::EVENT_AFTER_INSERT,
64
            [
65
                ManageOrganizationTypeProjectConfig::class,
66
                'save'
67
            ]
68
        );
69
70
        Event::on(
71
            OrganizationType::class,
72
            OrganizationType::EVENT_AFTER_UPDATE,
73
            [
74
                ManageOrganizationTypeProjectConfig::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 handleDeletedOrganizationType(ConfigEvent $event)
86
    {
87
        Event::off(
88
            OrganizationType::class,
89
            OrganizationType::EVENT_AFTER_DELETE,
90
            [
91
                ManageOrganizationTypeProjectConfig::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 = OrganizationType::findOne([
100
                'uid' => $uid
101
            ])) {
102
            return;
103
        }
104
105
        $provider->delete();
106
107
        Event::on(
108
            OrganizationType::class,
109
            OrganizationType::EVENT_AFTER_DELETE,
110
            [
111
                ManageOrganizationTypeProjectConfig::class,
112
                'delete'
113
            ]
114
        );
115
    }
116
117
    /**
118
     * @param ConfigEvent $event
119
     */
120
    public static function handleChangedUserType(ConfigEvent $event)
121
    {
122
        Event::off(
123
            UserType::class,
124
            UserType::EVENT_AFTER_INSERT,
125
            [
126
                ManageUserTypeProjectConfig::class,
127
                'save'
128
            ]
129
        );
130
131
        Event::off(
132
            UserType::class,
133
            UserType::EVENT_AFTER_UPDATE,
134
            [
135
                ManageUserTypeProjectConfig::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 = UserType::findOne([
144
                'uid' => $uid
145
            ]))) {
146
            $token = new UserType();
147
        }
148
149
        Craft::configure($token, $event->newValue);
0 ignored issues
show
Bug introduced by
It seems like $token defined by \flipbox\organizations\r...e(array('uid' => $uid)) on line 143 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...
150
151
        $token->save();
152
153
        Event::on(
154
            UserType::class,
155
            UserType::EVENT_AFTER_INSERT,
156
            [
157
                ManageUserTypeProjectConfig::class,
158
                'save'
159
            ]
160
        );
161
162
        Event::on(
163
            UserType::class,
164
            UserType::EVENT_AFTER_UPDATE,
165
            [
166
                ManageUserTypeProjectConfig::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 handleDeletedUserType(ConfigEvent $event)
178
    {
179
        Event::off(
180
            UserType::class,
181
            UserType::EVENT_AFTER_DELETE,
182
            [
183
                ManageUserTypeProjectConfig::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 = UserType::findOne([
192
                'uid' => $uid
193
            ])) {
194
            return;
195
        }
196
197
        $token->delete();
198
199
        Event::on(
200
            UserType::class,
201
            UserType::EVENT_AFTER_DELETE,
202
            [
203
                ManageUserTypeProjectConfig::class,
204
                'delete'
205
            ]
206
        );
207
    }
208
209
    /**
210
     * @return array
211
     */
212
    public static function rebuild(): array
213
    {
214
        $return = [];
215
216
        foreach (OrganizationType::findAll([]) as $record) {
217
            $return['plugins']['organizations']['organizationTypes'][$record->uid] = $record->toProjectConfig();
218
        }
219
220
        foreach (UserType::findAll([]) as $token) {
221
            $return['plugins']['organizations']['userTypes'][$record->uid] = $record->toProjectConfig();
0 ignored issues
show
Bug introduced by
The variable $record seems to be defined by a foreach iteration on line 216. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
222
        }
223
224
        return $return;
225
    }
226
}
227