Complex classes like Provider often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Provider, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class Provider extends ActiveRecordWithId |
||
38 | { |
||
39 | use HandleRulesTrait, |
||
40 | StateAttributeTrait; |
||
41 | |||
42 | /** |
||
43 | * The table alias |
||
44 | */ |
||
45 | const TABLE_ALIAS = 'patron_providers'; |
||
46 | |||
47 | /** |
||
48 | * @deprecated |
||
49 | */ |
||
50 | const CLIENT_ID_LENGTH = ProviderInstance::CLIENT_ID_LENGTH; |
||
51 | |||
52 | /** |
||
53 | * @deprecated |
||
54 | */ |
||
55 | const CLIENT_SECRET_LENGTH = ProviderInstance::CLIENT_SECRET_LENGTH; |
||
56 | |||
57 | /** |
||
58 | * @var bool |
||
59 | */ |
||
60 | public $autoSaveInstances = false; |
||
61 | |||
62 | /** |
||
63 | * Environments that are temporarily set during the save process |
||
64 | * |
||
65 | * @var null|array |
||
66 | */ |
||
67 | private $insertInstances; |
||
68 | |||
69 | /** |
||
70 | * @inheritdoc |
||
71 | * @return ProviderActiveQuery |
||
72 | * @throws \yii\base\InvalidConfigException |
||
73 | */ |
||
74 | public static function find() |
||
75 | { |
||
76 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
||
77 | return Craft::createObject(ProviderActiveQuery::class, [get_called_class()]); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @return string|null |
||
82 | */ |
||
83 | public function getIcon() |
||
84 | { |
||
85 | if ($this->class === null) { |
||
86 | return null; |
||
87 | } |
||
88 | |||
89 | return Patron::getInstance()->getCp()->getProviderIcon( |
||
90 | $this->class |
||
91 | ); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @inheritdoc |
||
96 | */ |
||
97 | public function rules() |
||
98 | { |
||
99 | return array_merge( |
||
100 | parent::rules(), |
||
101 | $this->handleRules(), |
||
102 | $this->stateRules(), |
||
103 | [ |
||
104 | [ |
||
105 | [ |
||
106 | 'class' |
||
107 | ], |
||
108 | ProviderValidator::class |
||
109 | ], |
||
110 | [ |
||
111 | [ |
||
112 | 'class' |
||
113 | ], |
||
114 | 'required' |
||
115 | ], |
||
116 | [ |
||
117 | [ |
||
118 | 'class', |
||
119 | 'settings' |
||
120 | ], |
||
121 | 'safe', |
||
122 | 'on' => [ |
||
123 | ModelHelper::SCENARIO_DEFAULT |
||
124 | ] |
||
125 | ] |
||
126 | ] |
||
127 | ); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Get all of the associated tokens. |
||
132 | * |
||
133 | * @param array $config |
||
134 | * @return \yii\db\ActiveQuery |
||
135 | */ |
||
136 | public function getTokens(array $config = []) |
||
137 | { |
||
138 | $query = $this->hasMany( |
||
139 | Token::class, |
||
140 | ['providerId' => 'id'] |
||
141 | ); |
||
142 | |||
143 | if (!empty($config)) { |
||
144 | QueryHelper::configure( |
||
145 | $query, |
||
146 | $config |
||
147 | ); |
||
148 | } |
||
149 | |||
150 | return $query; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Get all of the associated tokens. |
||
155 | * |
||
156 | * @param array $config |
||
157 | * @return \yii\db\ActiveQuery |
||
158 | */ |
||
159 | public function getLocks(array $config = []) |
||
160 | { |
||
161 | $query = $this->hasMany( |
||
162 | ProviderLock::class, |
||
163 | ['providerId' => 'id'] |
||
164 | ); |
||
165 | |||
166 | if (!empty($config)) { |
||
167 | QueryHelper::configure( |
||
168 | $query, |
||
169 | $config |
||
170 | ); |
||
171 | } |
||
172 | |||
173 | return $query; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Get all of the associated instances. |
||
178 | * |
||
179 | * @param array $config |
||
180 | * @return \yii\db\ActiveQuery |
||
181 | */ |
||
182 | public function getInstances(array $config = []) |
||
183 | { |
||
184 | $query = $this->hasMany( |
||
185 | ProviderInstance::class, |
||
186 | ['providerId' => 'id'] |
||
187 | ) |
||
188 | ->indexBy('id'); |
||
189 | |||
190 | if (!empty($config)) { |
||
191 | QueryHelper::configure( |
||
192 | $query, |
||
193 | $config |
||
194 | ); |
||
195 | } |
||
196 | |||
197 | return $query; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * @param array $instances |
||
202 | * @return $this |
||
203 | */ |
||
204 | public function setInstances(array $instances = []) |
||
205 | { |
||
206 | $records = []; |
||
207 | foreach (array_filter($instances) as $environment) { |
||
208 | $records[] = $this->resolveInstance($environment); |
||
209 | } |
||
210 | |||
211 | $this->populateRelation('instances', $records); |
||
212 | return $this; |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * @param $instance |
||
217 | * @return ProviderInstance |
||
218 | */ |
||
219 | protected function resolveInstance($instance): ProviderInstance |
||
220 | { |
||
221 | if ($instance instanceof ProviderInstance) { |
||
222 | return $instance; |
||
223 | } |
||
224 | |||
225 | $record = new ProviderInstance(); |
||
226 | |||
227 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
||
228 | return ObjectHelper::populate( |
||
229 | $record, |
||
230 | $instance |
||
231 | ); |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Get all of the associated environments. |
||
236 | * |
||
237 | * @param array $config |
||
238 | * @return \yii\db\ActiveQuery |
||
239 | */ |
||
240 | public function getEnvironments(array $config = []) |
||
241 | { |
||
242 | $query = $this->hasMany( |
||
243 | ProviderEnvironment::class, |
||
244 | ['instanceId' => 'id'] |
||
245 | ) |
||
246 | ->via('instances') |
||
247 | ->indexBy('environment'); |
||
248 | |||
249 | if (!empty($config)) { |
||
250 | QueryHelper::configure( |
||
251 | $query, |
||
252 | $config |
||
253 | ); |
||
254 | } |
||
255 | |||
256 | return $query; |
||
257 | } |
||
258 | |||
259 | /******************************************* |
||
260 | * SAVE |
||
261 | *******************************************/ |
||
262 | |||
263 | /** |
||
264 | * @param PluginInterface $plugin |
||
265 | * @param bool $runValidation |
||
266 | * @param null $attributeNames |
||
267 | * @return bool |
||
268 | */ |
||
269 | public function saveAndLock(PluginInterface $plugin, $runValidation = true, $attributeNames = null): bool |
||
270 | { |
||
271 | if (!$this->save($runValidation, $attributeNames)) { |
||
272 | return false; |
||
273 | } |
||
274 | |||
275 | return $this->addLock($plugin); |
||
276 | } |
||
277 | |||
278 | |||
279 | /******************************************* |
||
280 | * LOCK |
||
281 | *******************************************/ |
||
282 | |||
283 | /** |
||
284 | * @param PluginInterface $plugin |
||
285 | * @return bool |
||
286 | */ |
||
287 | public function addLock(PluginInterface $plugin): bool |
||
288 | { |
||
289 | if (null === ($pluginId = $this->getPluginId($plugin))) { |
||
290 | return false; |
||
291 | } |
||
292 | |||
293 | $record = new ProviderLock(); |
||
294 | |||
295 | $record->setAttributes([ |
||
296 | 'providerId' => $this->getId(), |
||
297 | 'pluginId' => $pluginId |
||
298 | ]); |
||
299 | |||
300 | return (bool)$record->save(); |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * @param PluginInterface $plugin |
||
305 | * @return bool |
||
306 | * @throws \Throwable |
||
307 | */ |
||
308 | public function removeLock(PluginInterface $plugin): bool |
||
309 | { |
||
310 | if (null === ($pluginId = $this->getPluginId($plugin))) { |
||
311 | return false; |
||
312 | } |
||
313 | |||
314 | if (null === ($record = ProviderLock::findOne([ |
||
315 | 'providerId' => $this->getId() ?: 0, |
||
316 | 'pluginId' => $pluginId |
||
317 | ]))) { |
||
318 | return true; |
||
319 | } |
||
320 | |||
321 | return (bool)$record->delete(); |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * @return bool |
||
326 | */ |
||
327 | public function isLocked(): bool |
||
331 | |||
332 | /** |
||
333 | * @param PluginInterface $plugin |
||
334 | * @return int|null |
||
335 | */ |
||
336 | protected function getPluginId(PluginInterface $plugin) |
||
350 | |||
351 | /** |
||
352 | * @param PluginInterface $plugin |
||
353 | * @return int|null |
||
354 | */ |
||
355 | protected function getPluginName(PluginInterface $plugin) |
||
369 | |||
370 | |||
371 | /******************************************* |
||
372 | * DELETE |
||
373 | *******************************************/ |
||
374 | |||
375 | /** |
||
376 | * @param PluginInterface|null $plugin |
||
377 | * @return bool|false|int |
||
378 | * @throws \Throwable |
||
379 | * @throws \yii\db\StaleObjectException |
||
380 | */ |
||
381 | public function delete(PluginInterface $plugin = null) |
||
385 | |||
386 | /** |
||
387 | * @param PluginInterface|null $plugin |
||
388 | * @return bool |
||
389 | * @throws \craft\errors\InvalidPluginException |
||
390 | */ |
||
391 | protected function canDelete(PluginInterface $plugin = null) |
||
435 | |||
436 | /******************************************* |
||
437 | * EVENTS |
||
438 | *******************************************/ |
||
439 | |||
440 | /** |
||
441 | * @inheritdoc |
||
442 | */ |
||
443 | public function beforeSave($insert): bool |
||
460 | |||
461 | /******************************************* |
||
462 | * UPDATE / INSERT |
||
463 | *******************************************/ |
||
464 | |||
465 | /** |
||
466 | * We're extracting the environments that may have been explicitly set on the record. When the 'id' |
||
467 | * attribute is updated, it removes any associated relationships. |
||
468 | * |
||
469 | * @inheritdoc |
||
470 | * @throws \Throwable |
||
471 | */ |
||
472 | protected function insertInternal($attributes = null) |
||
487 | |||
488 | /** |
||
489 | * @inheritdoc |
||
490 | * @throws \Throwable |
||
491 | */ |
||
492 | protected function updateInternal($attributes = null) |
||
493 | { |
||
494 | if (false === ($response = parent::updateInternal($attributes))) { |
||
495 | return false; |
||
496 | } |
||
497 | |||
498 | return $this->upsertInternal($attributes) ? $response : false; |
||
499 | } |
||
500 | |||
501 | /** |
||
502 | * @param null $attributes |
||
503 | * @return bool |
||
504 | * @throws \Throwable |
||
505 | * @throws \yii\db\StaleObjectException |
||
506 | */ |
||
507 | protected function upsertInternal($attributes = null): bool |
||
519 | |||
520 | /** |
||
521 | * @param bool $force |
||
522 | * @return bool |
||
523 | * @throws \Throwable |
||
524 | * @throws \yii\db\StaleObjectException |
||
525 | */ |
||
526 | protected function saveInstances(bool $force = false): bool |
||
566 | |||
567 | /** |
||
568 | * @return string |
||
569 | * @throws \ReflectionException |
||
570 | */ |
||
571 | public function getDisplayName(): string |
||
572 | { |
||
573 | return ProviderHelper::displayName( |
||
574 | $this->class |
||
575 | ); |
||
576 | } |
||
577 | } |
||
578 |