| Conditions | 7 |
| Paths | 30 |
| Total Lines | 63 |
| Code Lines | 34 |
| 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 |
||
| 88 | public function getBodyHtml() |
||
| 89 | { |
||
| 90 | $view = Craft::$app->getView(); |
||
| 91 | |||
| 92 | try { |
||
| 93 | if (Analytics::$plugin->getAnalytics()->checkPluginRequirements()) { |
||
| 94 | if (Analytics::$plugin->getSettings()->enableWidgets) { |
||
| 95 | $reportingViews = Analytics::$plugin->getViews()->getViews(); |
||
| 96 | |||
| 97 | if (count($reportingViews) > 0) { |
||
| 98 | $widgetSettings = $this->settings; |
||
| 99 | |||
| 100 | $reportingView = Analytics::$plugin->getViews()->getViewById($widgetSettings['viewId']); |
||
| 101 | |||
| 102 | if ($reportingView) { |
||
| 103 | $request = [ |
||
| 104 | 'viewId' => $widgetSettings['viewId'] ?? null, |
||
| 105 | 'chart' => $widgetSettings['chart'] ?? null, |
||
| 106 | 'period' => $widgetSettings['period'] ?? null, |
||
| 107 | 'options' => $widgetSettings['options'][$widgetSettings['chart']] ?? null, |
||
| 108 | ]; |
||
| 109 | |||
| 110 | |||
| 111 | // use cached response if available |
||
| 112 | |||
| 113 | if (Analytics::$plugin->getSettings()->enableCache === true) { |
||
| 114 | $cacheId = ['getReport', $request]; |
||
| 115 | $cachedResponse = Analytics::$plugin->cache->get($cacheId); |
||
| 116 | } |
||
| 117 | |||
| 118 | |||
| 119 | // render |
||
| 120 | |||
| 121 | $localeDefinition = Analytics::$plugin->getAnalytics()->getD3LocaleDefinition(['currency' => $reportingView->gaViewCurrency]); |
||
| 122 | |||
| 123 | $jsOptions = [ |
||
| 124 | 'localeDefinition' => $localeDefinition, |
||
| 125 | 'chartLanguage' => Analytics::$plugin->getAnalytics()->getChartLanguage(), |
||
| 126 | 'request' => $request, |
||
| 127 | 'cachedResponse' => $cachedResponse ?? null, |
||
| 128 | ]; |
||
| 129 | |||
| 130 | $view->registerJsFile('//www.gstatic.com/charts/loader.js'); |
||
| 131 | $view->registerAssetBundle(ReportWidgetAsset::class); |
||
| 132 | |||
| 133 | $view->registerJs('new Analytics.ReportWidget("widget'.$this->id.'", '.Json::encode($jsOptions).');'); |
||
| 134 | |||
| 135 | return $view->renderTemplate('analytics/_components/widgets/Report/body'); |
||
| 136 | } |
||
| 137 | |||
| 138 | return $view->renderTemplate('analytics/_special/view-not-configured'); |
||
| 139 | } |
||
| 140 | |||
| 141 | return $view->renderTemplate('analytics/_special/no-views'); |
||
| 142 | } |
||
| 143 | |||
| 144 | return $view->renderTemplate('analytics/_components/widgets/Report/disabled'); |
||
| 145 | } |
||
| 146 | |||
| 147 | return $view->renderTemplate('analytics/_special/not-connected'); |
||
| 148 | } catch (\Exception $e) { |
||
| 149 | Craft::info('Couldn’t load report widget: '.$e->getMessage(), __METHOD__); |
||
| 150 | return $view->renderTemplate('analytics/_special/error'); |
||
| 151 | } |
||
| 286 |