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