Total Complexity | 40 |
Total Lines | 388 |
Duplicated Lines | 0 % |
Changes | 14 | ||
Bugs | 0 | Features | 0 |
Complex classes like InstantAnalytics 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 InstantAnalytics, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
43 | class InstantAnalytics extends Plugin |
||
44 | { |
||
45 | // Traits |
||
46 | // ========================================================================= |
||
47 | |||
48 | use ServicesTrait; |
||
49 | |||
50 | // Constants |
||
51 | // ========================================================================= |
||
52 | |||
53 | const COMMERCE_PLUGIN_HANDLE = 'commerce'; |
||
54 | const SEOMATIC_PLUGIN_HANDLE = 'seomatic'; |
||
55 | |||
56 | // Static Properties |
||
57 | // ========================================================================= |
||
58 | |||
59 | /** |
||
60 | * @var InstantAnalytics |
||
61 | */ |
||
62 | public static $plugin; |
||
63 | |||
64 | /** |
||
65 | * @var Settings |
||
66 | */ |
||
67 | public static $settings; |
||
68 | |||
69 | /** |
||
70 | * @var Commerce|null |
||
71 | */ |
||
72 | public static $commercePlugin; |
||
73 | |||
74 | /** |
||
75 | * @var Seomatic|null |
||
76 | */ |
||
77 | public static $seomaticPlugin; |
||
78 | |||
79 | /** |
||
80 | * @var string |
||
81 | */ |
||
82 | public static $currentTemplate = ''; |
||
83 | |||
84 | /** |
||
85 | * @var bool |
||
86 | */ |
||
87 | public static $pageViewSent = false; |
||
88 | |||
89 | /** |
||
90 | * @var bool |
||
91 | */ |
||
92 | public static $craft31 = false; |
||
93 | |||
94 | // Public Properties |
||
95 | // ========================================================================= |
||
96 | |||
97 | /** |
||
98 | * @var string |
||
99 | */ |
||
100 | public $schemaVersion = '1.0.0'; |
||
101 | |||
102 | /** |
||
103 | * @var bool |
||
104 | */ |
||
105 | public $hasCpSection = false; |
||
106 | |||
107 | /** |
||
108 | * @var bool |
||
109 | */ |
||
110 | public $hasCpSettings = true; |
||
111 | |||
112 | // Public Methods |
||
113 | // ========================================================================= |
||
114 | |||
115 | /** |
||
116 | * @inheritdoc |
||
117 | */ |
||
118 | public function init() |
||
119 | { |
||
120 | parent::init(); |
||
121 | self::$plugin = $this; |
||
122 | self::$settings = $this->getSettings(); |
||
123 | self::$craft31 = version_compare(Craft::$app->getVersion(), '3.1', '>='); |
||
124 | |||
125 | // Determine if Craft Commerce is installed & enabled |
||
126 | self::$commercePlugin = Craft::$app->getPlugins()->getPlugin(self::COMMERCE_PLUGIN_HANDLE); |
||
127 | // Determine if SEOmatic is installed & enabled |
||
128 | self::$seomaticPlugin = Craft::$app->getPlugins()->getPlugin(self::SEOMATIC_PLUGIN_HANDLE); |
||
129 | // Add in our Craft components |
||
130 | $this->addComponents(); |
||
131 | // Install our global event handlers |
||
132 | $this->installEventListeners(); |
||
133 | |||
134 | Craft::info( |
||
135 | Craft::t( |
||
136 | 'instant-analytics', |
||
137 | '{name} plugin loaded', |
||
138 | ['name' => $this->name] |
||
139 | ), |
||
140 | __METHOD__ |
||
141 | ); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @inheritdoc |
||
146 | */ |
||
147 | public function settingsHtml() |
||
148 | { |
||
149 | $commerceFields = []; |
||
150 | |||
151 | if (self::$commercePlugin) { |
||
152 | $productTypes = self::$commercePlugin->getProductTypes()->getAllProductTypes(); |
||
153 | |||
154 | foreach ($productTypes as $productType) { |
||
155 | $productFields = $this->getPullFieldsFromLayoutId($productType->fieldLayoutId); |
||
156 | /** @noinspection SlowArrayOperationsInLoopInspection */ |
||
157 | $commerceFields = \array_merge($commerceFields, $productFields); |
||
158 | if ($productType->hasVariants) { |
||
159 | $variantFields = $this->getPullFieldsFromLayoutId($productType->variantFieldLayoutId); |
||
160 | /** @noinspection SlowArrayOperationsInLoopInspection */ |
||
161 | $commerceFields = \array_merge($commerceFields, $variantFields); |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | |||
166 | // Rend the settings template |
||
167 | try { |
||
168 | return Craft::$app->getView()->renderTemplate( |
||
169 | 'instant-analytics/settings', |
||
170 | [ |
||
171 | 'settings' => $this->getSettings(), |
||
172 | 'commerceFields' => $commerceFields, |
||
173 | ] |
||
174 | ); |
||
175 | } catch (LoaderError $e) { |
||
176 | Craft::error($e->getMessage(), __METHOD__); |
||
177 | } catch (\Exception $e) { |
||
178 | Craft::error($e->getMessage(), __METHOD__); |
||
179 | } |
||
180 | |||
181 | return ''; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Handle the `{% hook iaSendPageView %}` |
||
186 | * |
||
187 | * @param array &$context |
||
188 | * |
||
189 | * @return string|null |
||
190 | */ |
||
191 | public function iaSendPageView(/** @noinspection PhpUnusedParameterInspection */ array &$context) |
||
192 | { |
||
193 | $this->sendPageView(); |
||
194 | |||
195 | return ''; |
||
196 | } |
||
197 | |||
198 | // Protected Methods |
||
199 | // ========================================================================= |
||
200 | |||
201 | /** |
||
202 | * Add in our Craft components |
||
203 | */ |
||
204 | protected function addComponents() |
||
205 | { |
||
206 | $view = Craft::$app->getView(); |
||
207 | // Add in our Twig extensions |
||
208 | $view->registerTwigExtension(new InstantAnalyticsTwigExtension()); |
||
209 | // Install our template hook |
||
210 | $view->hook('iaSendPageView', [$this, 'iaSendPageView']); |
||
211 | // Register our variables |
||
212 | Event::on( |
||
213 | CraftVariable::class, |
||
214 | CraftVariable::EVENT_INIT, |
||
215 | function (Event $event) { |
||
216 | /** @var CraftVariable $variable */ |
||
217 | $variable = $event->sender; |
||
218 | $variable->set('instantAnalytics', [ |
||
219 | 'class' => InstantAnalyticsVariable::class, |
||
220 | 'viteService' => $this->vite, |
||
221 | ]); |
||
222 | } |
||
223 | ); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Install our event listeners |
||
228 | */ |
||
229 | protected function installEventListeners() |
||
230 | { |
||
231 | // Handler: Plugins::EVENT_AFTER_INSTALL_PLUGIN |
||
232 | Event::on( |
||
233 | Plugins::class, |
||
234 | Plugins::EVENT_AFTER_INSTALL_PLUGIN, |
||
235 | function (PluginEvent $event) { |
||
236 | if ($event->plugin === $this) { |
||
237 | $request = Craft::$app->getRequest(); |
||
238 | if ($request->isCpRequest) { |
||
239 | Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('instant-analytics/welcome'))->send(); |
||
240 | } |
||
241 | } |
||
242 | } |
||
243 | ); |
||
244 | $request = Craft::$app->getRequest(); |
||
245 | // Install only for non-console site requests |
||
246 | if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) { |
||
247 | $this->installSiteEventListeners(); |
||
248 | } |
||
249 | // Install only for non-console Control Panel requests |
||
250 | if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) { |
||
251 | $this->installCpEventListeners(); |
||
252 | } |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Install site event listeners for site requests only |
||
257 | */ |
||
258 | protected function installSiteEventListeners() |
||
259 | { |
||
260 | // Handler: UrlManager::EVENT_REGISTER_SITE_URL_RULES |
||
261 | Event::on( |
||
262 | UrlManager::class, |
||
263 | UrlManager::EVENT_REGISTER_SITE_URL_RULES, |
||
264 | function (RegisterUrlRulesEvent $event) { |
||
265 | Craft::debug( |
||
266 | 'UrlManager::EVENT_REGISTER_SITE_URL_RULES', |
||
267 | __METHOD__ |
||
268 | ); |
||
269 | // Register our Control Panel routes |
||
270 | $event->rules = array_merge( |
||
271 | $event->rules, |
||
272 | $this->customFrontendRoutes() |
||
273 | ); |
||
274 | } |
||
275 | ); |
||
276 | // Remember the name of the currently rendering template |
||
277 | Event::on( |
||
278 | View::class, |
||
279 | View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE, |
||
280 | function (TemplateEvent $event) { |
||
281 | self::$currentTemplate = $event->template; |
||
282 | } |
||
283 | ); |
||
284 | // Remember the name of the currently rendering template |
||
285 | Event::on( |
||
286 | View::class, |
||
287 | View::EVENT_AFTER_RENDER_PAGE_TEMPLATE, |
||
288 | function (TemplateEvent $event) { |
||
289 | if (self::$settings->autoSendPageView) { |
||
290 | $this->sendPageView(); |
||
291 | } |
||
292 | } |
||
293 | ); |
||
294 | // Commerce-specific hooks |
||
295 | if (self::$commercePlugin) { |
||
296 | Event::on(Order::class, Order::EVENT_AFTER_COMPLETE_ORDER, function (Event $e) { |
||
297 | $order = $e->sender; |
||
298 | if (self::$settings->autoSendPurchaseComplete) { |
||
299 | $this->commerce->orderComplete($order); |
||
300 | } |
||
301 | }); |
||
302 | |||
303 | Event::on(Order::class, Order::EVENT_AFTER_ADD_LINE_ITEM, function (LineItemEvent $e) { |
||
304 | $lineItem = $e->lineItem; |
||
305 | if (self::$settings->autoSendAddToCart) { |
||
306 | $this->commerce->addToCart($lineItem->order, $lineItem); |
||
307 | } |
||
308 | }); |
||
309 | |||
310 | // Check to make sure Order::EVENT_AFTER_REMOVE_LINE_ITEM is defined |
||
311 | if (defined(Order::class . '::EVENT_AFTER_REMOVE_LINE_ITEM')) { |
||
312 | Event::on(Order::class, Order::EVENT_AFTER_REMOVE_LINE_ITEM, function (LineItemEvent $e) { |
||
313 | $lineItem = $e->lineItem; |
||
314 | if (self::$settings->autoSendRemoveFromCart) { |
||
315 | $this->commerce->removeFromCart($lineItem->order, $lineItem); |
||
316 | } |
||
317 | }); |
||
318 | } |
||
319 | } |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * Install site event listeners for Control Panel requests only |
||
324 | */ |
||
325 | protected function installCpEventListeners() |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * Return the custom frontend routes |
||
331 | * |
||
332 | * @return array |
||
333 | */ |
||
334 | protected function customFrontendRoutes(): array |
||
335 | { |
||
336 | return [ |
||
337 | 'instantanalytics/pageViewTrack/<filename:[-\w\.*]+>?' => |
||
338 | 'instant-analytics/track/track-page-view-url', |
||
339 | 'instantanalytics/eventTrack/<filename:[-\w\.*]+>?' => |
||
340 | 'instant-analytics/track/track-event-url', |
||
341 | ]; |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * @inheritdoc |
||
346 | */ |
||
347 | protected function createSettingsModel() |
||
350 | } |
||
351 | |||
352 | // Private Methods |
||
353 | // ========================================================================= |
||
354 | |||
355 | /** |
||
356 | * Send a page view with the pre-loaded IAnalytics object |
||
357 | */ |
||
358 | private function sendPageView() |
||
359 | { |
||
360 | $request = Craft::$app->getRequest(); |
||
361 | if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest() && !self::$pageViewSent) { |
||
362 | self::$pageViewSent = true; |
||
363 | /** @var IAnalytics $analytics */ |
||
364 | $analytics = self::$plugin->ia->getGlobals(self::$currentTemplate); |
||
365 | // Bail if we have no analytics object |
||
366 | if ($analytics === null) { |
||
367 | return; |
||
368 | } |
||
369 | // If SEOmatic is installed, set the page title from it |
||
370 | $this->setTitleFromSeomatic($analytics); |
||
371 | // Send the page view |
||
372 | if ($analytics) { |
||
373 | $response = $analytics->sendPageview(); |
||
374 | Craft::info( |
||
375 | Craft::t( |
||
376 | 'instant-analytics', |
||
377 | 'pageView sent, response:: {response}', |
||
378 | [ |
||
379 | 'response' => print_r($response, true), |
||
380 | ] |
||
381 | ), |
||
382 | __METHOD__ |
||
383 | ); |
||
384 | } else { |
||
385 | Craft::error( |
||
386 | Craft::t( |
||
387 | 'instant-analytics', |
||
388 | 'Analytics not sent because googleAnalyticsTracking is not set' |
||
389 | ), |
||
390 | __METHOD__ |
||
391 | ); |
||
392 | } |
||
393 | } |
||
394 | } |
||
395 | |||
396 | /** |
||
397 | * If SEOmatic is installed, set the page title from it |
||
398 | * |
||
399 | * @param $analytics |
||
400 | */ |
||
401 | private function setTitleFromSeomatic(IAnalytics $analytics) |
||
409 | } |
||
410 | } |
||
411 | } |
||
412 | } |
||
413 | |||
414 | /** |
||
415 | * @param $layoutId |
||
416 | * |
||
417 | * @return array |
||
418 | */ |
||
419 | private function getPullFieldsFromLayoutId($layoutId): array |
||
431 | } |
||
432 | } |
||
433 |