Total Complexity | 42 |
Total Lines | 398 |
Duplicated Lines | 0 % |
Changes | 32 | ||
Bugs | 0 | Features | 0 |
Complex classes like Plugin 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.
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 Plugin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class Plugin extends \craft\base\Plugin |
||
35 | { |
||
36 | // Traits |
||
37 | // ========================================================================= |
||
38 | |||
39 | use PluginTrait; |
||
40 | |||
41 | // Properties |
||
42 | // ========================================================================= |
||
43 | |||
44 | /** |
||
45 | * @var bool |
||
46 | */ |
||
47 | public $hasCpSettings = true; |
||
48 | |||
49 | /** |
||
50 | * @inheritdoc |
||
51 | */ |
||
52 | public $minVersionRequired = '1.1.0'; |
||
53 | |||
54 | // Public Methods |
||
55 | // ========================================================================= |
||
56 | |||
57 | /** |
||
58 | * @inheritdoc |
||
59 | */ |
||
60 | public function init() |
||
61 | { |
||
62 | parent::init(); |
||
63 | |||
64 | $this->_setPluginComponents(); |
||
65 | $this->_registerCpRoutes(); |
||
66 | $this->_registerVariable(); |
||
67 | $this->_registerEventHandlers(); |
||
68 | $this->_registerTableAttributes(); |
||
69 | $this->_initLoginAccountsUserPane(); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @inheritdoc |
||
74 | */ |
||
75 | public function getSettingsResponse() |
||
76 | { |
||
77 | $url = UrlHelper::cpUrl('settings/social/loginproviders'); |
||
78 | |||
79 | Craft::$app->controller->redirect($url); |
||
80 | |||
81 | return ''; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Get OAuth provider config. |
||
86 | * |
||
87 | * @param $handle |
||
88 | * @param bool $parse |
||
89 | * @return array |
||
90 | * @throws \yii\base\InvalidConfigException |
||
91 | */ |
||
92 | public function getOauthProviderConfig(string $handle, bool $parse = true): array |
||
93 | { |
||
94 | $config = [ |
||
95 | 'options' => $this->getOauthConfigItem($handle, 'options', $parse), |
||
96 | 'scope' => $this->getOauthConfigItem($handle, 'scope'), |
||
97 | 'authorizationOptions' => $this->getOauthConfigItem($handle, 'authorizationOptions'), |
||
98 | ]; |
||
99 | |||
100 | $provider = $this->getLoginProviders()->getLoginProvider($handle); |
||
101 | |||
102 | if ($provider && !isset($config['options']['redirectUri'])) { |
||
103 | $config['options']['redirectUri'] = $provider->getRedirectUri(); |
||
|
|||
104 | } |
||
105 | |||
106 | return $config; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Get login provider config. |
||
111 | * |
||
112 | * @param $handle |
||
113 | * |
||
114 | * @return array |
||
115 | */ |
||
116 | public function getLoginProviderConfig($handle) |
||
117 | { |
||
118 | $configSettings = Craft::$app->config->getConfigFromFile($this->id); |
||
119 | |||
120 | if (isset($configSettings['loginProviders'][$handle])) { |
||
121 | return $configSettings['loginProviders'][$handle]; |
||
122 | } |
||
123 | |||
124 | return []; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Save plugin settings. |
||
129 | * |
||
130 | * @param array $settings |
||
131 | * |
||
132 | * @param Plugin|null $plugin |
||
133 | * @return bool |
||
134 | */ |
||
135 | public function savePluginSettings(array $settings, Plugin $plugin = null) |
||
136 | { |
||
137 | if ($plugin === null) { |
||
138 | $plugin = Craft::$app->getPlugins()->getPlugin('social'); |
||
139 | |||
140 | if ($plugin === null) { |
||
141 | throw new NotFoundHttpException('Plugin not found'); |
||
142 | } |
||
143 | } |
||
144 | |||
145 | $storedSettings = Craft::$app->plugins->getStoredPluginInfo('social')['settings']; |
||
146 | |||
147 | $settings['loginProviders'] = []; |
||
148 | |||
149 | if (isset($storedSettings['loginProviders'])) { |
||
150 | $settings['loginProviders'] = $storedSettings['loginProviders']; |
||
151 | } |
||
152 | |||
153 | return Craft::$app->getPlugins()->savePluginSettings($plugin, $settings); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Save login provider settings. |
||
158 | * |
||
159 | * @param $handle |
||
160 | * @param $providerSettings |
||
161 | * |
||
162 | * @return bool |
||
163 | */ |
||
164 | public function saveLoginProviderSettings($handle, $providerSettings) |
||
180 | } |
||
181 | |||
182 | // Protected Methods |
||
183 | // ========================================================================= |
||
184 | |||
185 | /** |
||
186 | * @inheritdoc |
||
187 | */ |
||
188 | protected function createSettingsModel() |
||
189 | { |
||
190 | return new Settings(); |
||
191 | } |
||
192 | |||
193 | // Private Methods |
||
194 | // ========================================================================= |
||
195 | |||
196 | /** |
||
197 | * Social login for the control panel. |
||
198 | * |
||
199 | * @return null |
||
200 | * @throws \craft\errors\MissingComponentException |
||
201 | * @throws \yii\base\InvalidConfigException |
||
202 | */ |
||
203 | private function initCpSocialLogin() |
||
204 | { |
||
205 | if (!Craft::$app->getRequest()->getIsConsoleRequest() && $this->getSettings()->enableCpLogin && Craft::$app->getRequest()->getIsCpRequest() && Craft::$app->getRequest()->getSegment(1) === 'login') { |
||
206 | |||
207 | $loginProviders = $this->loginProviders->getLoginProviders(); |
||
208 | $jsLoginProviders = []; |
||
209 | |||
210 | foreach ($loginProviders as $loginProvider) { |
||
211 | $jsLoginProvider = [ |
||
212 | 'name' => $loginProvider->getName(), |
||
213 | 'handle' => $loginProvider->getHandle(), |
||
214 | 'url' => $this->getLoginAccounts()->getLoginUrl($loginProvider->getHandle()), |
||
215 | 'iconUrl' => $loginProvider->getIconUrl(), |
||
216 | ]; |
||
217 | |||
218 | $jsLoginProviders[] = $jsLoginProvider; |
||
219 | } |
||
220 | |||
221 | $error = Craft::$app->getSession()->getFlash('error'); |
||
222 | |||
223 | Craft::$app->getView()->registerAssetBundle(LoginAsset::class); |
||
224 | |||
225 | Craft::$app->getView()->registerJs('var socialLoginForm = new Craft.SocialLoginForm(' . json_encode($jsLoginProviders) . ', ' . json_encode($error) . ');'); |
||
226 | } |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Initialize login accounts user pane. |
||
231 | * |
||
232 | * @return null |
||
233 | */ |
||
234 | private function _initLoginAccountsUserPane() |
||
244 | } |
||
245 | }); |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Get OAuth config item |
||
250 | * |
||
251 | * @param string $providerHandle |
||
252 | * @param string $key |
||
253 | * |
||
254 | * @return array |
||
255 | */ |
||
256 | private function getOauthConfigItem(string $providerHandle, string $key, bool $parse = true): array |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * Parse OAuth config item environment variables. |
||
275 | * |
||
276 | * @param string $key |
||
277 | * @param array $configItem |
||
278 | * @param bool $parse |
||
279 | * @return array |
||
280 | */ |
||
281 | private function parseOauthConfigItemEnv(string $key, array $configItem, bool $parse = true): array |
||
282 | { |
||
283 | // Parse config item options environment variables |
||
284 | if ($parse && $key === 'options') { |
||
285 | return array_map('Craft::parseEnv', $configItem); |
||
286 | } |
||
287 | |||
288 | return $configItem; |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * Set plugin components. |
||
293 | */ |
||
294 | private function _setPluginComponents() |
||
299 | ]); |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * Register CP routes. |
||
304 | */ |
||
305 | private function _registerCpRoutes() |
||
322 | }); |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * Register Social template variable. |
||
327 | */ |
||
328 | private function _registerVariable() |
||
329 | { |
||
330 | Event::on(CraftVariable::class, CraftVariable::EVENT_INIT, function(Event $event): void { |
||
331 | /** @var CraftVariable $variable */ |
||
332 | $variable = $event->sender; |
||
333 | $variable->set('social', SocialVariable::class); |
||
334 | }); |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * Register Social user table attributes. |
||
339 | */ |
||
340 | private function _registerTableAttributes() |
||
363 | } |
||
364 | } |
||
365 | }); |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * Register event handlers. |
||
370 | */ |
||
371 | private function _registerEventHandlers() |
||
435 |