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 $configHelper */ |
||
12 | protected $configHelper; |
||
13 | |||
14 | /** @var GooglequeryHelper $queryHelper */ |
||
15 | protected $query; |
||
16 | |||
17 | /** @var EntityManager $em */ |
||
18 | protected $em; |
||
19 | |||
20 | /** @var OutputInterface $output */ |
||
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 | * @param AnalyticsOverview $overview |
||
43 | * |
||
44 | * @return array |
||
45 | */ |
||
46 | protected function getTimestamps(AnalyticsOverview &$overview) |
||
47 | { |
||
48 | // if yearoverview set the begin time to the first day of this year |
||
49 | $profileStartDate = explode('T', $this->configHelper->getActiveProfile()['created'])[0]; |
||
50 | if ($overview->getUseYear()) { |
||
51 | $begin_date = date('Y-m-d', mktime(0, 0, 0, 1, 1, date('Y'))); |
||
52 | $begin = strtotime($profileStartDate) > strtotime($begin_date) ? date('Y-m-d', strtotime($profileStartDate)) : $begin_date; |
||
53 | } else { |
||
54 | // check if timespan is't more than existence of the profile; if so, use the creation time in stead of the timespan time |
||
55 | $begin = strtotime($profileStartDate) > strtotime('-' . $overview->getTimespan() . ' days') ? date('Y-m-d', strtotime($profileStartDate)) : date('Y-m-d', strtotime('-' . $overview->getTimespan() . ' days')); |
||
56 | } |
||
57 | // set the end time |
||
58 | $end = date('Y-m-d', strtotime('-' . $overview->getStartOffset() . ' days')); |
||
59 | |||
60 | return array('begin' => $begin, 'end' => $end); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * get the extra data for an overview, can be overridden |
||
65 | * |
||
66 | * @return array |
||
67 | */ |
||
68 | protected function getExtra(AnalyticsOverview $overview) |
||
69 | { |
||
70 | $extra = array(); |
||
71 | |||
72 | // add segment |
||
73 | if ($overview->getSegment()) { |
||
74 | $extra['segment'] = $overview->getSegment()->getQuery(); |
||
75 | } |
||
76 | |||
77 | return $extra; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Executes the query |
||
82 | * |
||
83 | * @return array the resultset |
||
84 | */ |
||
85 | View Code Duplication | protected function executeQuery(AnalyticsOverview $overview, $metrics) |
|
0 ignored issues
–
show
|
|||
86 | { |
||
87 | $timestamps = $this->getTimestamps($overview); |
||
88 | $extra = $this->getExtra($overview); |
||
89 | |||
90 | // execute query |
||
91 | $results = $this->query->getResultsByDate( |
||
92 | $timestamps['begin'], |
||
93 | $timestamps['end'], |
||
94 | $metrics, |
||
95 | $extra |
||
96 | ); |
||
97 | |||
98 | return $results->getRows(); |
||
99 | } |
||
100 | |||
101 | abstract public function getData(&$overview); |
||
102 | } |
||
103 |
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.