Completed
Push — master ( ae5e03...0447ee )
by Jeroen
10:35 queued 04:37
created

Helper/Analytics/ChartDataCommandHelper.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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) {
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