Completed
Pull Request — 6.0 (#1872)
by Sander
102:24 queued 60:56
created

Analytics/AbstractAnalyticsCommandHelper.php (3 issues)

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 Doctrine\ORM\EntityManager;
6
use Kunstmaan\DashboardBundle\Entity\AnalyticsOverview;
7
use Kunstmaan\DashboardBundle\Helper\Google\Analytics\ConfigHelper;
8
use Kunstmaan\DashboardBundle\Helper\Google\Analytics\QueryHelper;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
/**
12
 * Class AbstractAnalyticsCommandHelper
13
 */
14
abstract class AbstractAnalyticsCommandHelper
15
{
16
    /** @var ConfigHelper $configHelper */
17
    protected $configHelper;
18
19
    /** @var QueryHelper $queryHelper */
20
    protected $query;
21
22
    /** @var EntityManager $em */
23
    protected $em;
24
25
    /** @var OutputInterface $output */
26
    protected $output;
27
28
    /**
29
     * Constructor
30
     *
31
     * @param $configHelper
32
     * @param $queryHelper
33
     * @param $output
34
     * @param $em
35
     */
36
    public function __construct($configHelper, $queryHelper, $output, $em)
37
    {
38
        $this->configHelper = $configHelper;
39
        $this->query = $queryHelper;
40
        $this->output = $output;
41
        $this->em = $em;
42
    }
43
44
    /**
45
     * @param AnalyticsOverview $overview
46
     *
47
     * @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...
48
     *
49
     * @throws \Exception
50
     */
51
    protected function getTimestamps(AnalyticsOverview $overview)
52
    {
53
        // if yearoverview set the begin time to the first day of this year
54
        $profileStartDate = explode('T', $this->configHelper->getActiveProfile()['created'])[0];
55
        if ($overview->getUseYear()) {
56
            $begin_date = date('Y-m-d', mktime(0, 0, 0, 1, 1, date('Y')));
57
            $begin = strtotime($profileStartDate) > strtotime($begin_date) ? date('Y-m-d', strtotime($profileStartDate)) : $begin_date;
58
        } else {
59
            // check if timespan is't more than existence of the profile; if so, use the creation time in stead of the timespan time
60
            $begin = strtotime($profileStartDate) > strtotime('-'.$overview->getTimespan().' days') ? date(
61
                'Y-m-d',
62
                strtotime($profileStartDate)
63
            ) : date('Y-m-d', strtotime('-'.$overview->getTimespan().' days'));
64
        }
65
        // set the end time
66
        $end = date('Y-m-d', strtotime('-'.$overview->getStartOffset().' days'));
67
68
        return ['begin' => $begin, 'end' => $end];
69
    }
70
71
    /**
72
     * Get the extra data for an overview, can be overridden
73
     *
74
     * @param AnalyticsOverview $overview
75
     *
76
     * @return array
77
     */
78
    protected function getExtra(AnalyticsOverview $overview)
79
    {
80
        $extra = [];
81
82
        // add segment
83
        if ($overview->getSegment()) {
84
            $extra['segment'] = $overview->getSegment()->getQuery();
85
        }
86
87
        return $extra;
88
    }
89
90
    /**
91
     * Executes the query
92
     *
93
     * @param AnalyticsOverview $overview
94
     * @param                   $metrics
95
     *
96
     * @return mixed
97
     * @throws \Exception
98
     */
99 View Code Duplication
    protected function executeQuery(AnalyticsOverview $overview, $metrics)
100
    {
101
        $timestamps = $this->getTimestamps($overview);
102
        $extra = $this->getExtra($overview);
103
        // execute query
104
        $results = $this->query->getResultsByDate(
105
            $timestamps['begin'],
106
            $timestamps['end'],
107
            $metrics,
108
            $extra
109
        );
110
111
        return $results->getRows();
112
    }
113
114
    public abstract function getData(AnalyticsOverview $overview);
0 ignored issues
show
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
The abstract declaration must precede the visibility declaration
Loading history...
115
116
}
117