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