| Conditions | 8 |
| Paths | 3 |
| Total Lines | 84 |
| Code Lines | 50 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 77 | public function init() |
||
| 78 | { |
||
| 79 | parent::init(); |
||
| 80 | self::$plugin = $this; |
||
| 81 | $view = Craft::$app->getView(); |
||
| 82 | $request = Craft::$app->getRequest(); |
||
| 83 | // Add in our Twig extensions |
||
| 84 | $view->registerTwigExtension(new InstantAnalyticsTwigExtension()); |
||
| 85 | // Install our template hook |
||
| 86 | $view->hook('iaSendPageView', [$this, 'iaSendPageView']); |
||
| 87 | // Determine if Craft Commerce is installed & enabled |
||
| 88 | self::$commercePlugin = Craft::$app->getPlugins()->getPlugin('commerce'); |
||
| 89 | // Determine if SEOmatic is installed & enabled |
||
| 90 | self::$seomaticPlugin = Craft::$app->getPlugins()->getPlugin('seomatic'); |
||
| 91 | // Register our variables |
||
| 92 | Event::on( |
||
| 93 | CraftVariable::class, |
||
| 94 | CraftVariable::EVENT_INIT, |
||
| 95 | function (Event $event) { |
||
| 96 | /** @var CraftVariable $variable */ |
||
| 97 | $variable = $event->sender; |
||
| 98 | $variable->set('instantAnalytics', InstantAnalyticsVariable::class); |
||
| 99 | } |
||
| 100 | ); |
||
| 101 | |||
| 102 | // Do something after we're installed |
||
| 103 | Event::on( |
||
| 104 | Plugins::class, |
||
| 105 | Plugins::EVENT_AFTER_INSTALL_PLUGIN, |
||
| 106 | function (PluginEvent $event) { |
||
| 107 | if ($event->plugin === $this) { |
||
| 108 | $request = Craft::$app->getRequest(); |
||
| 109 | if (($request->isCpRequest) && (!$request->isConsoleRequest)) { |
||
| 110 | Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('instant-analytics/welcome'))->send(); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | } |
||
| 114 | ); |
||
| 115 | |||
| 116 | // We're only interested in site requests that are not console requests |
||
| 117 | if (($request->isSiteRequest) && (!$request->isConsoleRequest)) { |
||
| 118 | // Remember the name of the currently rendering template |
||
| 119 | Event::on( |
||
| 120 | View::class, |
||
| 121 | View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE, |
||
| 122 | function (TemplateEvent $event) { |
||
| 123 | self::$currentTemplate = $event->template; |
||
| 124 | } |
||
| 125 | ); |
||
| 126 | // Remember the name of the currently rendering template |
||
| 127 | Event::on( |
||
| 128 | View::class, |
||
| 129 | View::EVENT_AFTER_RENDER_PAGE_TEMPLATE, |
||
| 130 | function (TemplateEvent $event) { |
||
| 131 | $settings = InstantAnalytics::$plugin->getSettings(); |
||
| 132 | if ($settings->autoSendPageView) { |
||
| 133 | $this->sendPageView(); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | ); |
||
| 137 | // Register our site routes |
||
| 138 | Event::on( |
||
| 139 | UrlManager::class, |
||
| 140 | UrlManager::EVENT_REGISTER_SITE_URL_RULES, |
||
| 141 | function (RegisterUrlRulesEvent $event) { |
||
| 142 | $event->rules['instantanalytics/pageViewTrack/<filename:[-\w\.*]+>?'] = |
||
| 143 | 'instant-analytics/track/track-page-view-url'; |
||
| 144 | $event->rules['instantanalytics/eventTrack/<filename:[-\w\.*]+>?'] = |
||
| 145 | 'instant-analytics/track/track-event-url'; |
||
| 146 | } |
||
| 147 | ); |
||
| 148 | // Commerce-specific hooks |
||
| 149 | if (self::$commercePlugin) { |
||
| 150 | // TODO: pending Commerce for Craft 3 |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | Craft::info( |
||
| 155 | Craft::t( |
||
| 156 | 'instant-analytics', |
||
| 157 | '{name} plugin loaded', |
||
| 158 | ['name' => $this->name] |
||
| 159 | ), |
||
| 160 | __METHOD__ |
||
| 161 | ); |
||
| 286 |