| Conditions | 12 |
| Paths | 13 |
| Total Lines | 108 |
| Code Lines | 61 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| Bugs | 0 | 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 |
||
| 37 | public function getInputHtml($value, ElementInterface $element = null): string |
||
| 38 | { |
||
| 39 | $view = Craft::$app->getView(); |
||
| 40 | $name = $this->handle; |
||
| 41 | |||
| 42 | if (!Analytics::$plugin->getAnalytics()->checkPluginRequirements()) { |
||
| 43 | return $view->renderTemplate('analytics/_special/plugin-not-configured'); |
||
| 44 | } |
||
| 45 | |||
| 46 | if (!Analytics::$plugin->getSettings()->enableFieldtype) { |
||
| 47 | return $view->renderTemplate('analytics/_components/fieldtypes/Report/disabled'); |
||
| 48 | } |
||
| 49 | |||
| 50 | $siteView = Analytics::$plugin->getViews()->getSiteViewBySiteId($element->siteId); |
||
|
|
|||
| 51 | |||
| 52 | if (!$siteView) { |
||
| 53 | return $view->renderTemplate('analytics/_special/view-not-configured'); |
||
| 54 | } |
||
| 55 | |||
| 56 | $reportingView = $siteView->getView(); |
||
| 57 | |||
| 58 | if (!$reportingView) { |
||
| 59 | return $view->renderTemplate('analytics/_special/view-not-configured'); |
||
| 60 | } |
||
| 61 | |||
| 62 | $variables = [ |
||
| 63 | 'hasUrl' => false, |
||
| 64 | 'isNew' => false, |
||
| 65 | ]; |
||
| 66 | |||
| 67 | if ($element) { |
||
| 68 | // Reformat the input name into something that looks more like an ID |
||
| 69 | $id = $view->formatInputId($name); |
||
| 70 | |||
| 71 | // Figure out what that ID is going to look like once it has been namespaced |
||
| 72 | $namespacedId = $view->namespaceInputId($id); |
||
| 73 | |||
| 74 | if ($element->id && $element->uri) { |
||
| 75 | $uri = Analytics::$plugin->getAnalytics()->getElementUrlPath($element->id, $element->siteId); |
||
| 76 | |||
| 77 | $startDate = date('Y-m-d', strtotime('-1 month')); |
||
| 78 | $endDate = date('Y-m-d'); |
||
| 79 | $metrics = 'ga:pageviews'; |
||
| 80 | $dimensions = 'ga:date'; |
||
| 81 | $filters = 'ga:pagePath=='.$uri; |
||
| 82 | |||
| 83 | $request = [ |
||
| 84 | 'startDate' => $startDate, |
||
| 85 | 'endDate' => $endDate, |
||
| 86 | 'metrics' => $metrics, |
||
| 87 | 'dimensions' => $dimensions, |
||
| 88 | 'filters' => $filters |
||
| 89 | ]; |
||
| 90 | |||
| 91 | // JS Options |
||
| 92 | $jsOptions = [ |
||
| 93 | 'chartLanguage' => Analytics::$plugin->getAnalytics()->getChartLanguage(), |
||
| 94 | ]; |
||
| 95 | |||
| 96 | // Add locale definition to JS options |
||
| 97 | $siteView = Analytics::$plugin->getViews()->getSiteViewBySiteId($element->siteId); |
||
| 98 | |||
| 99 | if ($siteView) { |
||
| 100 | $reportingView = $siteView->getView(); |
||
| 101 | |||
| 102 | if ($reportingView) { |
||
| 103 | // Currency definition |
||
| 104 | $jsOptions['currencyDefinition'] = Analytics::$plugin->getAnalytics()->getCurrencyDefinition($reportingView->gaViewCurrency); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | // Add cached response to JS options if any |
||
| 109 | $cacheId = ['reports.getElementReport', $request]; |
||
| 110 | $response = Analytics::$plugin->cache->get($cacheId); |
||
| 111 | |||
| 112 | if ($response) { |
||
| 113 | $response = [ |
||
| 114 | 'type' => 'area', |
||
| 115 | 'chart' => $response |
||
| 116 | ]; |
||
| 117 | |||
| 118 | $jsOptions['cachedResponse'] = $response; |
||
| 119 | } |
||
| 120 | |||
| 121 | // Register JS & Styles |
||
| 122 | $view->registerAssetBundle(ReportFieldAsset::class); |
||
| 123 | $view->registerJs('new AnalyticsReportField("'.$namespacedId.'-field", '.Json::encode($jsOptions).');'); |
||
| 124 | |||
| 125 | // Variables |
||
| 126 | $variables = [ |
||
| 127 | 'isNew' => false, |
||
| 128 | 'hasUrl' => true, |
||
| 129 | 'id' => $id, |
||
| 130 | 'uri' => $uri, |
||
| 131 | 'name' => $name, |
||
| 132 | 'value' => $value, |
||
| 133 | 'model' => $this, |
||
| 134 | 'element' => $element |
||
| 135 | ]; |
||
| 136 | } elseif (!$element->id) { |
||
| 137 | $variables = [ |
||
| 138 | 'hasUrl' => false, |
||
| 139 | 'isNew' => true, |
||
| 140 | ]; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | return $view->renderTemplate('analytics/_components/fieldtypes/Report/input', $variables); |
||
| 145 | } |
||
| 147 |