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

ChartDataCommandHelper::getData()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 54

Duplication

Lines 13
Ratio 24.07 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 13
loc 54
ccs 0
cts 42
cp 0
rs 8.3814
c 0
b 0
f 0
cc 6
nc 9
nop 1
crap 42

How to fix   Long Method   

Long Method

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:

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