Completed
Push — master ( 446c01...fcb59d )
by Ruud
113:27 queued 101:23
created

ChartDataCommandHelper   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 85
Duplicated Lines 15.29 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 13
loc 85
ccs 0
cts 57
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getExtra() 0 17 4
B getData() 13 54 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Kunstmaan\DashboardBundle\Command\Helper\Analytics;
4
5
use Kunstmaan\DashboardBundle\Entity\AnalyticsOverview;
6
7
class ChartDataCommandHelper extends AbstractAnalyticsCommandHelper
8
{
9
    /**
10
     * get extra data
11
     *
12
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
13
     */
14
    protected function getExtra(AnalyticsOverview $overview)
15
    {
16
        $timespan = $overview->getTimespan() - $overview->getStartOffset();
17
        $extra = parent::getExtra($overview);
18
19
        if ($timespan <= 1) {
20
            $extra['dimensions'] = 'ga:date,ga:hour';
21
        } elseif ($timespan <= 7) {
22
            $extra['dimensions'] = 'ga:date,ga:hour';
23
        } elseif ($timespan <= 31) {
24
            $extra['dimensions'] = 'ga:week,ga:day,ga:date';
25
        } else {
26
            $extra['dimensions'] = 'ga:isoYearIsoWeek';
27
        }
28
29
        return $extra;
30
    }
31
32
    /**
33
     * get data and save it for the overview
34
     *
35
     * @param AnalyticsOverview $overview The overview
36
     */
37
    public function getData(&$overview)
38
    {
39
        $this->output->writeln("\t" . 'Fetching chart data..');
40
41
        // execute the query
42
        $metrics = 'ga:sessions, ga:users, ga:newUsers, ga:pageviews';
43
        $rows = $this->executeQuery($overview, $metrics);
44
45
        $chartData = array();
46
        $chartDataMaxValue = 0;
47
        $timespan = $overview->getTimespan() - $overview->getStartOffset();
48
        foreach ($rows as $row) {
49
            // metrics
50
            $sessions = $row[\count($row) - 4];
51
            $users = $row[\count($row) - 3];
52
            $newusers = $row[\count($row) - 2];
53
            $pageviews = $row[\count($row) - 1];
54
55
            $maxvalue = max($sessions, $users, $newusers, $pageviews);
56
            // set max chartdata value
57
            if ($chartDataMaxValue < $maxvalue) {
58
                $chartDataMaxValue = $maxvalue;
59
            }
60
61
            // timestamp
62 View Code Duplication
            if ($timespan <= 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
                $timestamp = mktime($row[1], 0, 0, substr($row[0], 4, 2), substr($row[0], 6, 2), substr($row[0], 0, 4));
64
                $timestamp = date('Y-m-d H:00', $timestamp);
65
            } elseif ($timespan <= 7) {
66
                $timestamp = mktime($row[1], 0, 0, substr($row[0], 4, 2), substr($row[0], 6, 2), substr($row[0], 0, 4));
67
                $timestamp = date('Y-m-d H:00', $timestamp);
68
            } elseif ($timespan <= 31) {
69
                $timestamp = mktime(0, 0, 0, substr($row[2], 4, 2), substr($row[2], 6, 2), substr($row[2], 0, 4));
70
                $timestamp = date('Y-m-d H:00', $timestamp);
71
            } else {
72
                $timestamp = strtotime(substr($row[0], 0, 4) . 'W' . substr($row[0], 4, 2));
73
                $timestamp = date('Y-m-d H:00', $timestamp);
74
            }
75
76
            // add to chart array
77
            $chartEntry = array(
78
                'timestamp' => $timestamp,
79
                'sessions' => $sessions,
80
                'users' => $users,
81
                'newusers' => $newusers,
82
                'pageviews' => $pageviews,
83
            );
84
            $chartData[] = $chartEntry;
85
        }
86
87
        // adding data to the overview
88
        $overview->setChartDataMaxValue($chartDataMaxValue);
89
        $overview->setChartData(json_encode($chartData, JSON_NUMERIC_CHECK));
90
    }
91
}
92