Completed
Push — master ( c41cfc...336e2b )
by Nate
07:56 queued 06:48
created

ProjectConfigHandler::createFieldLayout()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 9
cp 0
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
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\db\Table;
13
use craft\events\ConfigEvent;
14
use craft\helpers\Db;
15
use craft\models\FieldLayout;
16
use flipbox\organizations\events\ManageOrganizationTypeProjectConfig;
17
use flipbox\organizations\events\ManageUserTypeProjectConfig;
18
use flipbox\organizations\records\OrganizationType;
19
use flipbox\organizations\records\UserType;
20
use yii\base\Event;
21
22
/**
23
 * @author Flipbox Factory <[email protected]>
24
 * @since 3.0.0
25
 */
26
class ProjectConfigHandler
27
{
28
    /**
29
     * @param ConfigEvent $event
30
     */
31
    public static function handleChangedOrganizationType(ConfigEvent $event)
32
    {
33
        Event::off(
34
            OrganizationType::class,
35
            OrganizationType::EVENT_AFTER_INSERT,
36
            [
37
                ManageOrganizationTypeProjectConfig::class,
38
                'save'
39
            ]
40
        );
41
42
        Event::off(
43
            OrganizationType::class,
44
            OrganizationType::EVENT_AFTER_UPDATE,
45
            [
46
                ManageOrganizationTypeProjectConfig::class,
47
                'save'
48
            ]
49
        );
50
51
        Event::off(
52
            OrganizationType::class,
53
            OrganizationType::EVENT_AFTER_INSERT,
54
            [
55
                ManageOrganizationTypeProjectConfig::class,
56
                'save'
57
            ]
58
        );
59
60
        // Get the UID that was matched in the config path
61
        $uid = $event->tokenMatches[0];
62
63
        if (null === ($type = OrganizationType::findOne([
64
                'uid' => $uid
65
            ]))
66
        ) {
67
            $type = new OrganizationType();
68
        }
69
70
        // Field Layout
71
        if (isset($event->newValue['fieldLayout'])) {
72
            $event->newValue['fieldLayout'] = static::createFieldLayout($event->newValue['fieldLayout'] ?? []);
73
        }
74
75
        // Sites
76
        if (isset($event->newValue['siteSettings'])) {
77
            $siteSettings = [];
78
            foreach ((array)$event->newValue['siteSettings'] as $id => $settings) {
79
                // id may be a uid
80
                if (!is_numeric($id)) {
81
                    $uid = $id;
82
                    $id = Db::idByUid(Table::SITES, $uid);
83
                }
84
85
                $settings['typeId'] = $type->getId();
86
87
                if (!empty($uid)) {
88
                    $settings['uid'] = $uid;
89
                }
90
91
                $siteSettings[$id] = $settings;
92
            }
93
            $event->newValue['siteSettings'] = $siteSettings;
94
        }
95
96
        Craft::configure($type, $event->newValue);
0 ignored issues
show
Bug introduced by
It seems like $type defined by \flipbox\organizations\r...e(array('uid' => $uid)) on line 63 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...
97
98
        $type->save();
99
100
        Event::on(
101
            OrganizationType::class,
102
            OrganizationType::EVENT_AFTER_INSERT,
103
            [
104
                ManageOrganizationTypeProjectConfig::class,
105
                'save'
106
            ]
107
        );
108
109
        Event::on(
110
            OrganizationType::class,
111
            OrganizationType::EVENT_AFTER_UPDATE,
112
            [
113
                ManageOrganizationTypeProjectConfig::class,
114
                'save'
115
            ]
116
        );
117
    }
118
119
    /**
120
     * @param array $config
121
     * @return FieldLayout
122
     */
123
    protected static function createFieldLayout(array $config = [])
124
    {
125
        $fieldLayout = FieldLayout::createFromConfig($config);
126
127
        if (isset($config['uid'])) {
128
            $fieldLayout->uid = $config['uid'];
129
            $fieldLayout->id = Db::idByUid(Table::FIELDLAYOUTS, $config['uid']);
130
        }
131
132
        return $fieldLayout;
133
    }
134
135
    /**
136
     * @param ConfigEvent $event
137
     * @throws \Throwable
138
     * @throws \yii\db\StaleObjectException
139
     */
140
    public static function handleDeletedOrganizationType(ConfigEvent $event)
141
    {
142
        Event::off(
143
            OrganizationType::class,
144
            OrganizationType::EVENT_AFTER_DELETE,
145
            [
146
                ManageOrganizationTypeProjectConfig::class,
147
                'delete'
148
            ]
149
        );
150
151
        // Get the UID that was matched in the config path
152
        $uid = $event->tokenMatches[0];
153
154
        if (null === $type = OrganizationType::findOne([
155
                'uid' => $uid
156
            ])) {
157
            return;
158
        }
159
160
        $type->delete();
161
162
        Event::on(
163
            OrganizationType::class,
164
            OrganizationType::EVENT_AFTER_DELETE,
165
            [
166
                ManageOrganizationTypeProjectConfig::class,
167
                'delete'
168
            ]
169
        );
170
    }
171
172
    /**
173
     * @param ConfigEvent $event
174
     */
175
    public static function handleChangedUserType(ConfigEvent $event)
176
    {
177
        Event::off(
178
            UserType::class,
179
            UserType::EVENT_AFTER_INSERT,
180
            [
181
                ManageUserTypeProjectConfig::class,
182
                'save'
183
            ]
184
        );
185
186
        Event::off(
187
            UserType::class,
188
            UserType::EVENT_AFTER_UPDATE,
189
            [
190
                ManageUserTypeProjectConfig::class,
191
                'save'
192
            ]
193
        );
194
195
        // Get the UID that was matched in the config path
196
        $uid = $event->tokenMatches[0];
197
198
        if (null === ($token = UserType::findOne([
199
                'uid' => $uid
200
            ]))) {
201
            $token = new UserType();
202
        }
203
204
        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 198 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...
205
206
        $token->save();
207
208
        Event::on(
209
            UserType::class,
210
            UserType::EVENT_AFTER_INSERT,
211
            [
212
                ManageUserTypeProjectConfig::class,
213
                'save'
214
            ]
215
        );
216
217
        Event::on(
218
            UserType::class,
219
            UserType::EVENT_AFTER_UPDATE,
220
            [
221
                ManageUserTypeProjectConfig::class,
222
                'save'
223
            ]
224
        );
225
    }
226
227
    /**
228
     * @param ConfigEvent $event
229
     * @throws \Throwable
230
     * @throws \yii\db\StaleObjectException
231
     */
232
    public static function handleDeletedUserType(ConfigEvent $event)
233
    {
234
        Event::off(
235
            UserType::class,
236
            UserType::EVENT_AFTER_DELETE,
237
            [
238
                ManageUserTypeProjectConfig::class,
239
                'delete'
240
            ]
241
        );
242
243
        // Get the UID that was matched in the config path
244
        $uid = $event->tokenMatches[0];
245
246
        if (null === $token = UserType::findOne([
247
                'uid' => $uid
248
            ])) {
249
            return;
250
        }
251
252
        $token->delete();
253
254
        Event::on(
255
            UserType::class,
256
            UserType::EVENT_AFTER_DELETE,
257
            [
258
                ManageUserTypeProjectConfig::class,
259
                'delete'
260
            ]
261
        );
262
    }
263
264
    /**
265
     * @return array
266
     */
267
    public static function rebuild(): array
268
    {
269
        $return = [];
270
271
        foreach (OrganizationType::findAll([]) as $record) {
272
            $return['plugins']['organizations']['organizationTypes'][$record->uid] = $record->toProjectConfig();
273
        }
274
275
        foreach (UserType::findAll([]) as $token) {
276
            $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 271. 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...
277
        }
278
279
        return $return;
280
    }
281
}
282