| Total Complexity | 41 |
| Total Lines | 532 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Reports 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 Reports, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Reports extends Component |
||
| 19 | { |
||
| 20 | // Public Methods |
||
| 21 | // ========================================================================= |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Returns a realtime report. |
||
| 25 | * |
||
| 26 | * @param array $request |
||
| 27 | * |
||
| 28 | * @return array |
||
| 29 | * @throws \yii\base\InvalidConfigException |
||
| 30 | */ |
||
| 31 | public function getRealtimeReport(array $request) |
||
| 32 | { |
||
| 33 | $view = Analytics::$plugin->getViews()->getViewById($request['viewId']); |
||
| 34 | |||
| 35 | $tableId = null; |
||
| 36 | |||
| 37 | if ($view) { |
||
| 38 | $tableId = 'ga:'.$view->gaViewId; |
||
| 39 | } |
||
| 40 | |||
| 41 | $metrics = $request['metrics']; |
||
| 42 | $optParams = $request['optParams']; |
||
| 43 | |||
| 44 | $cacheId = ['reports.getRealtimeReport', $tableId, $metrics, $optParams]; |
||
| 45 | $response = Analytics::$plugin->cache->get($cacheId); |
||
| 46 | |||
| 47 | if (!$response) { |
||
| 48 | $response = Analytics::$plugin->getApis()->getAnalytics()->getService()->data_realtime->get($tableId, $metrics, $optParams); |
||
| 49 | |||
| 50 | $cacheDuration = Analytics::$plugin->getSettings()->realtimeRefreshInterval; |
||
| 51 | Analytics::$plugin->cache->set($cacheId, $response, $cacheDuration); |
||
| 52 | } |
||
| 53 | |||
| 54 | return (array)$response; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Get e-commerce report. |
||
| 59 | * |
||
| 60 | * @param $viewId |
||
| 61 | * @param $period |
||
| 62 | * |
||
| 63 | * @return array |
||
| 64 | */ |
||
| 65 | public function getEcommerceReport($viewId, $period) |
||
| 66 | { |
||
| 67 | $startDate = '7daysAgo'; |
||
| 68 | $endDate = 'today'; |
||
| 69 | $dimensions = 'ga:date'; |
||
| 70 | |||
| 71 | switch ($period) { |
||
| 72 | case 'week': |
||
| 73 | $startDate = '7daysAgo'; |
||
| 74 | break; |
||
| 75 | case 'month': |
||
| 76 | $startDate = '30daysAgo'; |
||
| 77 | break; |
||
| 78 | case 'year': |
||
| 79 | $startDate = '365daysAgo'; |
||
| 80 | $dimensions = 'ga:yearMonth'; |
||
| 81 | break; |
||
| 82 | } |
||
| 83 | |||
| 84 | $metrics = 'ga:transactionRevenue,ga:revenuePerTransaction,ga:transactions,ga:transactionsPerSession'; |
||
| 85 | |||
| 86 | $criteria = new ReportRequestCriteria; |
||
| 87 | $criteria->viewId = $viewId; |
||
| 88 | $criteria->startDate = $startDate; |
||
| 89 | $criteria->endDate = $endDate; |
||
| 90 | $criteria->metrics = $metrics; |
||
| 91 | $criteria->dimensions = $dimensions; |
||
| 92 | $criteria->includeEmptyRows = true; |
||
| 93 | |||
| 94 | $reportResponse = Analytics::$plugin->getApis()->getAnalyticsReporting()->getReport($criteria); |
||
| 95 | $report = $reportResponse->toSimpleObject(); |
||
| 96 | $reportData = $this->parseReportingReport($reportResponse); |
||
| 97 | |||
| 98 | $view = Analytics::$plugin->getViews()->getViewById($viewId); |
||
| 99 | |||
| 100 | return [ |
||
| 101 | 'period' => $startDate.' - '.$endDate, |
||
| 102 | 'totalRevenue' => $report->data->totals[0]->values[0], |
||
| 103 | 'totalRevenuePerTransaction' => $report->data->totals[0]->values[1], |
||
| 104 | 'totalTransactions' => $report->data->totals[0]->values[2], |
||
| 105 | 'totalTransactionsPerSession' => $report->data->totals[0]->values[3], |
||
| 106 | 'reportData' => [ |
||
| 107 | 'view' => $view->name, |
||
| 108 | 'chart' => $reportData, |
||
| 109 | 'period' => $period, |
||
| 110 | 'periodLabel' => Craft::t('analytics', 'This '.$period) |
||
| 111 | ], |
||
| 112 | ]; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Returns an element report. |
||
| 117 | * |
||
| 118 | * @param int $elementId |
||
| 119 | * @param int|null $siteId |
||
| 120 | * @param string $metric |
||
| 121 | * |
||
| 122 | * @return array |
||
| 123 | * @throws \Exception |
||
| 124 | */ |
||
| 125 | public function getElementReport($elementId, $siteId, $metric) |
||
| 126 | { |
||
| 127 | $uri = Analytics::$plugin->getAnalytics()->getElementUrlPath($elementId, $siteId); |
||
| 128 | |||
| 129 | if (!$uri) { |
||
| 130 | throw new InvalidElementException("Element doesn't support URLs.", 1); |
||
| 131 | } |
||
| 132 | |||
| 133 | if ($uri === '__home__') { |
||
| 134 | $uri = ''; |
||
| 135 | } |
||
| 136 | |||
| 137 | $siteView = Analytics::$plugin->getViews()->getSiteViewBySiteId($siteId); |
||
| 138 | |||
| 139 | $viewId = null; |
||
| 140 | |||
| 141 | if ($siteView) { |
||
| 142 | $viewId = $siteView->viewId; |
||
| 143 | } |
||
| 144 | |||
| 145 | $startDate = date('Y-m-d', strtotime('-1 month')); |
||
| 146 | $endDate = date('Y-m-d'); |
||
| 147 | $dimensions = 'ga:date'; |
||
| 148 | $metrics = $metric; |
||
| 149 | $filters = 'ga:pagePath=='.$uri; |
||
| 150 | |||
| 151 | $request = [ |
||
| 152 | 'viewId' => $viewId, |
||
| 153 | 'startDate' => $startDate, |
||
| 154 | 'endDate' => $endDate, |
||
| 155 | 'metrics' => $metrics, |
||
| 156 | 'dimensions' => $dimensions, |
||
| 157 | 'filters' => $filters |
||
| 158 | ]; |
||
| 159 | |||
| 160 | $cacheId = ['reports.getElementReport', $request]; |
||
| 161 | $response = Analytics::$plugin->cache->get($cacheId); |
||
| 162 | |||
| 163 | if (!$response) { |
||
| 164 | |||
| 165 | $criteria = new ReportRequestCriteria; |
||
| 166 | $criteria->viewId = $viewId; |
||
| 167 | $criteria->startDate = $startDate; |
||
| 168 | $criteria->endDate = $endDate; |
||
| 169 | $criteria->metrics = $metrics; |
||
| 170 | $criteria->dimensions = $dimensions; |
||
| 171 | $criteria->filtersExpression = $filters; |
||
| 172 | |||
| 173 | $reportResponse = Analytics::$plugin->getApis()->getAnalyticsReporting()->getReport($criteria); |
||
| 174 | $response = $this->parseReportingReport($reportResponse); |
||
| 175 | |||
| 176 | if ($response) { |
||
| 177 | Analytics::$plugin->cache->set($cacheId, $response); |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | return $response; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Returns an area report. |
||
| 186 | * |
||
| 187 | * @param array $request |
||
| 188 | * |
||
| 189 | * @return array |
||
| 190 | * @throws \yii\base\InvalidConfigException |
||
| 191 | */ |
||
| 192 | public function getAreaReport(array $request) |
||
| 236 | ]; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Returns a counter report. |
||
| 241 | * |
||
| 242 | * @param array $request |
||
| 243 | * |
||
| 244 | * @return array |
||
| 245 | * @throws \yii\base\InvalidConfigException |
||
| 246 | */ |
||
| 247 | public function getCounterReport(array $request) |
||
| 248 | { |
||
| 249 | $viewId = ($request['viewId'] ?? null); |
||
| 250 | $period = ($request['period'] ?? null); |
||
| 251 | $metricString = ($request['options']['metric'] ?? null); |
||
| 252 | $startDate = date('Y-m-d', strtotime('-1 '.$period)); |
||
| 253 | $endDate = date('Y-m-d'); |
||
| 254 | |||
| 255 | $criteria = new ReportRequestCriteria; |
||
| 256 | $criteria->viewId = $viewId; |
||
| 257 | $criteria->startDate = $startDate; |
||
| 258 | $criteria->endDate = $endDate; |
||
| 259 | $criteria->metrics = $metricString; |
||
| 260 | |||
| 261 | |||
| 262 | $reportResponse = Analytics::$plugin->getApis()->getAnalyticsReporting()->getReport($criteria); |
||
| 263 | $report = $this->parseReportingReport($reportResponse); |
||
| 264 | |||
| 265 | $total = 0; |
||
| 266 | |||
| 267 | if (!empty($report['totals'][0])) { |
||
| 268 | $total = $report['totals'][0]; |
||
| 269 | } |
||
| 270 | |||
| 271 | $counter = [ |
||
| 272 | 'type' => $report['cols'][0]['type'], |
||
| 273 | 'value' => $total, |
||
| 274 | 'label' => StringHelper::toLowerCase(Craft::t('analytics', Analytics::$plugin->metadata->getDimMet($metricString))) |
||
| 275 | ]; |
||
| 276 | |||
| 277 | $view = Analytics::$plugin->getViews()->getViewById($viewId); |
||
| 278 | |||
| 279 | return [ |
||
| 280 | 'view' => $view->name, |
||
| 281 | 'type' => 'counter', |
||
| 282 | 'counter' => $counter, |
||
| 283 | 'response' => $report, |
||
| 284 | 'metric' => Craft::t('analytics', Analytics::$plugin->metadata->getDimMet($metricString)), |
||
| 285 | 'period' => $period, |
||
| 286 | 'periodLabel' => Craft::t('analytics', 'this '.$period) |
||
| 287 | ]; |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Returns a pie report. |
||
| 292 | * |
||
| 293 | * @param array $request |
||
| 294 | * |
||
| 295 | * @return array |
||
| 296 | * @throws \yii\base\InvalidConfigException |
||
| 297 | */ |
||
| 298 | public function getPieReport(array $request) |
||
| 327 | ]; |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Returns a table report. |
||
| 332 | * |
||
| 333 | * @param array $request |
||
| 334 | * |
||
| 335 | * @return array |
||
| 336 | * @throws \yii\base\InvalidConfigException |
||
| 337 | */ |
||
| 338 | public function getTableReport(array $request) |
||
| 366 | ]; |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Returns a geo report. |
||
| 371 | * |
||
| 372 | * @param array $request |
||
| 373 | * |
||
| 374 | * @return array |
||
| 375 | * @throws \yii\base\InvalidConfigException |
||
| 376 | */ |
||
| 377 | public function getGeoReport(array $request) |
||
| 411 | ]; |
||
| 412 | } |
||
| 413 | |||
| 414 | // Private Methods |
||
| 415 | // ========================================================================= |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @param Google_Service_AnalyticsReporting_Report $report |
||
| 419 | * @return array |
||
| 420 | */ |
||
| 421 | private function parseReportingReport(Google_Service_AnalyticsReporting_Report $report): array |
||
| 422 | { |
||
| 423 | $cols = $this->parseReportingReportCols($report); |
||
| 424 | $rows = $this->parseReportingReportRows($report); |
||
| 425 | $totals = $report->getData()->getTotals()[0]->getValues(); |
||
| 426 | |||
| 427 | return [ |
||
| 428 | 'cols' => $cols, |
||
| 429 | 'rows' => $rows, |
||
| 430 | 'totals' => $totals |
||
| 431 | ]; |
||
| 432 | } |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @param Google_Service_AnalyticsReporting_Report $report |
||
| 436 | * @return array |
||
| 437 | */ |
||
| 438 | private function parseReportingReportCols(Google_Service_AnalyticsReporting_Report $report): array |
||
| 439 | { |
||
| 440 | $columnHeader = $report->getColumnHeader(); |
||
| 441 | $columnHeaderDimensions = $columnHeader->getDimensions(); |
||
| 442 | $metricHeaderEntries = $columnHeader->getMetricHeader()->getMetricHeaderEntries(); |
||
| 443 | |||
| 444 | $cols = []; |
||
| 445 | |||
| 446 | if ($columnHeaderDimensions) { |
||
| 447 | foreach ($columnHeaderDimensions as $columnHeaderDimension) { |
||
| 448 | |||
| 449 | $id = $columnHeaderDimension; |
||
| 450 | $label = Analytics::$plugin->metadata->getDimMet($columnHeaderDimension); |
||
| 451 | |||
| 452 | switch ($columnHeaderDimension) { |
||
| 453 | case 'ga:date': |
||
| 454 | case 'ga:yearMonth': |
||
| 455 | $type = 'date'; |
||
| 456 | break; |
||
| 457 | |||
| 458 | case 'ga:continent': |
||
| 459 | $type = 'continent'; |
||
| 460 | break; |
||
| 461 | case 'ga:subContinent': |
||
| 462 | $type = 'subContinent'; |
||
| 463 | break; |
||
| 464 | |||
| 465 | case 'ga:latitude': |
||
| 466 | case 'ga:longitude': |
||
| 467 | $type = 'float'; |
||
| 468 | break; |
||
| 469 | |||
| 470 | default: |
||
| 471 | $type = 'string'; |
||
| 472 | } |
||
| 473 | |||
| 474 | $col = [ |
||
| 475 | 'type' => $type, |
||
| 476 | 'label' => Craft::t('analytics', $label), |
||
| 477 | 'id' => $id, |
||
| 478 | ]; |
||
| 479 | |||
| 480 | array_push($cols, $col); |
||
| 481 | } |
||
| 482 | } |
||
| 483 | |||
| 484 | foreach ($metricHeaderEntries as $metricHeaderEntry) { |
||
| 485 | $label = Analytics::$plugin->metadata->getDimMet($metricHeaderEntry['name']); |
||
| 486 | |||
| 487 | $col = [ |
||
| 488 | 'type' => strtolower($metricHeaderEntry['type']), |
||
| 489 | 'label' => Craft::t('analytics', $label), |
||
| 490 | 'id' => $metricHeaderEntry['name'], |
||
| 491 | ]; |
||
| 492 | |||
| 493 | array_push($cols, $col); |
||
| 494 | } |
||
| 495 | |||
| 496 | return $cols; |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * @param Google_Service_AnalyticsReporting_Report $report |
||
| 501 | * @return array |
||
| 502 | */ |
||
| 503 | private function parseReportingReportRows(Google_Service_AnalyticsReporting_Report $report): array |
||
| 550 | } |
||
| 551 | } |
||
| 552 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: