Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
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
3
namespace Kunstmaan\DashboardBundle\Command\Helper\Analytics;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\DashboardBundle\Entity\AnalyticsOverview;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
abstract class AbstractAnalyticsCommandHelper
10
{
11
    /** @var ConfigHelper */
12
    protected $configHelper;
13
14
    /** @var GooglequeryHelper */
15
    protected $query;
16
17
    /** @var EntityManager */
18
    protected $em;
19
20
    /** @var OutputInterface */
21
    protected $output;
22
23
    /**
24
     * Constructor
25
     *
26
     * @param $configHelper
27
     * @param $queryHelper
28
     * @param $output
29
     * @param $em
30
     */
31
    public function __construct($configHelper, $queryHelper, $output, $em)
32
    {
33
        $this->configHelper = $configHelper;
34
        $this->query = $queryHelper;
35
        $this->output = $output;
36
        $this->em = $em;
37
    }
38
39
    /**
40
     * Constructor
41
     *
42
     * @return array
43
     */
44
    protected function getTimestamps(AnalyticsOverview $overview)
45
    {
46
        // if yearoverview set the begin time to the first day of this year
47
        $profileStartDate = explode('T', $this->configHelper->getActiveProfile()['created'])[0];
48
        if ($overview->getUseYear()) {
49
            $begin_date = date('Y-m-d', mktime(0, 0, 0, 1, 1, date('Y')));
50
            $begin = strtotime($profileStartDate) > strtotime($begin_date) ? date('Y-m-d', strtotime($profileStartDate)) : $begin_date;
51
        } else {
52
            // check if timespan is't more than existence of the profile; if so, use the creation time in stead of the timespan time
53
            $begin = strtotime($profileStartDate) > strtotime('-' . $overview->getTimespan() . ' days') ? date('Y-m-d', strtotime($profileStartDate)) : date('Y-m-d', strtotime('-' . $overview->getTimespan() . ' days'));
54
        }
55
        // set the end time
56
        $end = date('Y-m-d', strtotime('-' . $overview->getStartOffset() . ' days'));
57
58
        return ['begin' => $begin, 'end' => $end];
59
    }
60
61
    /**
62
     * get the extra data for an overview, can be overridden
63
     *
64
     * @return array
65
     */
66
    protected function getExtra(AnalyticsOverview $overview)
67
    {
68
        $extra = [];
69
70
        // add segment
71
        if ($overview->getSegment()) {
72
            $extra['segment'] = $overview->getSegment()->getQuery();
73
        }
74
75
        return $extra;
76
    }
77
78
    /**
79
     * Executes the query
80
     *
81
     * @return array the resultset
82
     */
83 View Code Duplication
    protected function executeQuery(AnalyticsOverview $overview, $metrics)
0 ignored issues
show
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...
84
    {
85
        $timestamps = $this->getTimestamps($overview);
86
        $extra = $this->getExtra($overview);
87
88
        // execute query
89
        $results = $this->query->getResultsByDate(
90
            $timestamps['begin'],
91
            $timestamps['end'],
92
            $metrics,
93
            $extra
94
        );
95
96
        return $results->getRows();
97
    }
98
99
    abstract public function getData(&$overview);
100
}
101