Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
88 / 88 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
DateRangeService | |
100.00% |
88 / 88 |
|
100.00% |
9 / 9 |
27 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getPeriod | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getStartTimestamp | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
getStartDate | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getEndTimestamp | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
getEndDate | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
setPeriodAndDates | |
100.00% |
45 / 45 |
|
100.00% |
1 / 1 |
9 | |||
setPeriod | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getSummary | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
7 |
1 | <?php |
2 | |
3 | namespace Drupal\visitors\Service; |
4 | |
5 | use Drupal\Core\Datetime\DrupalDateTime; |
6 | use Drupal\visitors\VisitorsDateRangeInterface; |
7 | |
8 | /** |
9 | * The Date Range service. |
10 | */ |
11 | class DateRangeService implements VisitorsDateRangeInterface { |
12 | |
13 | /** |
14 | * The date service. |
15 | * |
16 | * @var \Drupal\Core\Datetime\DateFormatterInterface |
17 | */ |
18 | protected $dateFormatter; |
19 | |
20 | /** |
21 | * The session object. |
22 | * |
23 | * @var \Symfony\Component\HttpFoundation\Session\SessionInterface |
24 | */ |
25 | protected $session; |
26 | |
27 | /** |
28 | * Valid periods values. |
29 | * |
30 | * @var array |
31 | */ |
32 | protected $validPeriods = [ |
33 | 'day', |
34 | 'week', |
35 | 'month', |
36 | 'year', |
37 | 'range', |
38 | ]; |
39 | |
40 | /** |
41 | * DateService constructor. |
42 | * |
43 | * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter |
44 | * The date formatter service. |
45 | * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack |
46 | * The request stack. |
47 | */ |
48 | public function __construct($date_formatter, $request_stack) { |
49 | $this->dateFormatter = $date_formatter; |
50 | $request = $request_stack->getCurrentRequest(); |
51 | $this->session = $request->getSession(); |
52 | } |
53 | |
54 | /** |
55 | * {@inheritdoc} |
56 | */ |
57 | public function getPeriod() { |
58 | $period = $this->session->get('visitors_period'); |
59 | if (!in_array($period, $this->validPeriods)) { |
60 | $period = 'day'; |
61 | } |
62 | |
63 | return $period; |
64 | } |
65 | |
66 | /** |
67 | * {@inheritdoc} |
68 | */ |
69 | public function getStartTimestamp() { |
70 | $start_timestamp = $this->session->get('visitors_from'); |
71 | $start_timestamp = intval($start_timestamp); |
72 | if ($start_timestamp == 0) { |
73 | // $timezone = date_default_timezone_get(); |
74 | $date = new DrupalDateTime('yesterday'); |
75 | $start_timestamp = $date->getTimestamp(); |
76 | } |
77 | |
78 | return $start_timestamp; |
79 | } |
80 | |
81 | /** |
82 | * {@inheritdoc} |
83 | */ |
84 | public function getStartDate() { |
85 | $start_timestamp = $this->getStartTimestamp(); |
86 | $start_date = DrupalDateTime::createFromTimestamp($start_timestamp); |
87 | |
88 | return $start_date->format('Y-m-d'); |
89 | } |
90 | |
91 | /** |
92 | * {@inheritdoc} |
93 | */ |
94 | public function getEndTimestamp() { |
95 | $end_timestamp = $this->session->get('visitors_to'); |
96 | $end_timestamp = intval($end_timestamp); |
97 | if ($end_timestamp == 0) { |
98 | // $timezone = date_default_timezone_get(); |
99 | $date = new DrupalDateTime('today'); |
100 | $end_timestamp = $date->getTimestamp(); |
101 | } |
102 | |
103 | return $end_timestamp; |
104 | } |
105 | |
106 | /** |
107 | * {@inheritdoc} |
108 | */ |
109 | public function getEndDate() { |
110 | $end_timestamp = $this->getEndTimestamp(); |
111 | |
112 | $end_date = DrupalDateTime::createFromTimestamp($end_timestamp - self::ONE_DAY); |
113 | |
114 | return $end_date->format('Y-m-d'); |
115 | } |
116 | |
117 | /** |
118 | * {@inheritdoc} |
119 | */ |
120 | public function setPeriodAndDates($period, $start_date, $end_date) { |
121 | $period = $this->setPeriod($period); |
122 | |
123 | $start_timestamp = strtotime($start_date); |
124 | $end_timestamp = strtotime($end_date); |
125 | if (!$start_timestamp) { |
126 | $start_timestamp = 0; |
127 | } |
128 | if (!$end_timestamp) { |
129 | $end_timestamp = 0; |
130 | } |
131 | $start_date = DrupalDateTime::createFromTimestamp($start_timestamp); |
132 | $end_date = DrupalDateTime::createFromTimestamp($end_timestamp); |
133 | |
134 | $visitors_from = 0; |
135 | $visitors_to = 0; |
136 | if ($period == 'range') { |
137 | $visitors_from = $start_date->getTimestamp(); |
138 | $visitors_to = $end_date->getTimestamp(); |
139 | $visitors_to += self::ONE_DAY; |
140 | } |
141 | else { |
142 | $selected_date = $start_date->getTimestamp(); |
143 | switch ($period) { |
144 | case 'year': |
145 | $start = date('Y-01-01', $selected_date); |
146 | $end = strtotime("$start +1 year"); |
147 | $start = strtotime($start); |
148 | |
149 | $start = DrupalDateTime::createFromTimestamp($start); |
150 | $end = DrupalDateTime::createFromTimestamp($end); |
151 | |
152 | $visitors_from = $start->getTimestamp(); |
153 | $visitors_to = $end->getTimestamp(); |
154 | break; |
155 | |
156 | case 'month': |
157 | $start = date('Y-m-01', $selected_date); |
158 | $end = strtotime("$start +1 month"); |
159 | $start = strtotime($start); |
160 | |
161 | $start = DrupalDateTime::createFromTimestamp($start); |
162 | $end = DrupalDateTime::createFromTimestamp($end); |
163 | |
164 | $visitors_from = $start->getTimestamp(); |
165 | $visitors_to = $end->getTimestamp(); |
166 | break; |
167 | |
168 | case 'week': |
169 | $start = strtotime('Last Sunday', $selected_date); |
170 | $end = strtotime('Next Sunday', $start); |
171 | |
172 | $start = DrupalDateTime::createFromTimestamp($start); |
173 | $end = DrupalDateTime::createFromTimestamp($end); |
174 | |
175 | $visitors_from = $start->getTimestamp(); |
176 | $visitors_to = $end->getTimestamp(); |
177 | break; |
178 | |
179 | case 'day': |
180 | default: |
181 | $visitors_from = $start_date->getTimestamp(); |
182 | $visitors_to = $visitors_from + self::ONE_DAY; |
183 | break; |
184 | } |
185 | |
186 | } |
187 | |
188 | $this->session->set('visitors_from', (int) $visitors_from); |
189 | $this->session->set('visitors_to', (int) $visitors_to); |
190 | } |
191 | |
192 | /** |
193 | * Set the period. |
194 | * |
195 | * @param string $period |
196 | * The period. |
197 | * |
198 | * @return string |
199 | * The period. |
200 | */ |
201 | protected function setPeriod($period) { |
202 | if (!in_array($period, $this->validPeriods)) { |
203 | $period = 'day'; |
204 | } |
205 | $this->session->set('visitors_period', $period); |
206 | |
207 | return $period; |
208 | } |
209 | |
210 | /** |
211 | * {@inheritdoc} |
212 | */ |
213 | public function getSummary() { |
214 | $period = $this->getPeriod(); |
215 | $start_timestamp = $this->getStartTimestamp(); |
216 | $end_timestamp = $this->getEndTimestamp(); |
217 | |
218 | $summary = ''; |
219 | switch ($period) { |
220 | case 'day': |
221 | $summary = $this->dateFormatter->format($start_timestamp, 'custom', 'l, F j, Y'); |
222 | break; |
223 | |
224 | case 'month': |
225 | $summary = $this->dateFormatter->format($start_timestamp, 'custom', 'F Y'); |
226 | break; |
227 | |
228 | case 'year': |
229 | $summary = $this->dateFormatter->format($start_timestamp, 'custom', 'Y'); |
230 | break; |
231 | |
232 | case 'week': |
233 | case 'range': |
234 | default: |
235 | $formatted_start = $this->dateFormatter->format($start_timestamp, 'custom', 'F j, Y'); |
236 | $summary = $formatted_start . ' - ' . $this->dateFormatter->format($end_timestamp - self::ONE_DAY, 'custom', 'F j, Y'); |
237 | break; |
238 | } |
239 | |
240 | return $summary; |
241 | } |
242 | |
243 | } |