Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

Analytics/AbstractAnalyticsCommandHelper.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
namespace Kunstmaan\DashboardBundle\Command\Helper\Analytics;
3
4
use Doctrine\ORM\EntityManager;
5
use Kunstmaan\DashboardBundle\Entity\AnalyticsOverview;
6
use Symfony\Component\Console\Output\OutputInterface;
7
8
abstract class AbstractAnalyticsCommandHelper
9
{
10
11
    /** @var ConfigHelper $configHelper */
12
    protected $configHelper;
13
    /** @var GooglequeryHelper $queryHelper */
14
    protected $query;
15
    /** @var EntityManager $em */
16
    protected $em;
17
    /** @var OutputInterface $output */
18
    protected $output;
19
20
    /**
21
     * Constructor
22
     *
23
     * @param $configHelper
24
     * @param $queryHelper
25
     * @param $output
26
     * @param $em
27
     */
28
    public function __construct($configHelper, $queryHelper, $output, $em)
29
    {
30
        $this->configHelper = $configHelper;
31
        $this->query = $queryHelper;
32
        $this->output = $output;
33
        $this->em = $em;
34
    }
35
36
    /**
37
     * Constructor
38
     *
39
     * @param AnalyticsOverview $overview
40
     *
41
     * @return array
42
     */
43
    protected function getTimestamps(AnalyticsOverview &$overview) {
44
        // if yearoverview set the begin time to the first day of this year
45
        $profileStartDate = explode('T', $this->configHelper->getActiveProfile()['created'])[0];
46
        if ($overview->getUseYear()) {
47
            $begin_date = date('Y-m-d', mktime(0, 0, 0, 1, 1, date('Y')));
48
            $begin = strtotime($profileStartDate) > strtotime($begin_date) ? date('Y-m-d', strtotime($profileStartDate)) : $begin_date;
49
        } else {
50
            // check if timespan is't more than existence of the profile; if so, use the creation time in stead of the timespan time
51
            $begin = strtotime($profileStartDate) > strtotime('-' . $overview->getTimespan() . ' days') ? date('Y-m-d', strtotime($profileStartDate)) : date('Y-m-d', strtotime('-' . $overview->getTimespan() . ' days'));
52
        }
53
        // set the end time
54
        $end = date('Y-m-d', strtotime('-' . $overview->getStartOffset() . ' days'));
55
56
        return array('begin' => $begin, 'end' => $end);
57
    }
58
59
    /**
60
     * get the extra data for an overview, can be overridden
61
     *
62
     * @return array
63
     */
64
    protected function getExtra(AnalyticsOverview $overview) {
65
        $extra = array();
66
67
        // add segment
68
        if ($overview->getSegment()) {
69
            $extra['segment'] = $overview->getSegment()->getQuery();
0 ignored issues
show
The method getQuery cannot be called on $overview->getSegment() (of type integer).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
70
        }
71
72
        return $extra;
73
    }
74
75
    /**
76
     * Executes the query
77
     *
78
     * @return array the resultset
79
     */
80 View Code Duplication
    protected function executeQuery(AnalyticsOverview $overview, $metrics) {
81
        $timestamps = $this->getTimestamps($overview);
82
        $extra = $this->getExtra($overview);
83
84
        // execute query
85
        $results = $this->query->getResultsByDate(
86
            $timestamps['begin'],
87
            $timestamps['end'],
88
            $metrics,
89
            $extra
90
        );
91
92
        return $results->getRows();
93
    }
94
95
    public abstract function getData(&$overview);
96
97
}
98