Completed
Push — master ( 1b817e...6c15b7 )
by Jeroen
39:29
created

Helper/Analytics/ChartDataCommandHelper.php (1 issue)

Severity

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
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
            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));
0 ignored issues
show
json_encode($chartData, JSON_NUMERIC_CHECK) is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
90
    }
91
}
92