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