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

AbstractAnalyticsCommandHelper::getData()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
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
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...
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
Bug introduced by
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) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
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);
0 ignored issues
show
Documentation introduced by
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...
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
96
97
}
98