Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
29 / 29 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
OnlineService | |
100.00% |
29 / 29 |
|
100.00% |
8 / 8 |
8 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getLast30Minutes | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getLast24Hours | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getYesterday30Minutes | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
getYesterday24Hours | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
getLastWeek30Minutes | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
getLastWeek24Hours | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
query | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Drupal\visitors\Service; |
6 | |
7 | use Drupal\Component\Datetime\TimeInterface; |
8 | use Drupal\Core\Database\Connection; |
9 | use Drupal\visitors\VisitorsOnlineInterface; |
10 | |
11 | /** |
12 | * Provides a service for the visitors online. |
13 | * |
14 | * @package Drupal\visitors |
15 | */ |
16 | class OnlineService implements VisitorsOnlineInterface { |
17 | |
18 | /** |
19 | * The database connection. |
20 | * |
21 | * @var \Drupal\Core\Database\Connection |
22 | */ |
23 | protected $database; |
24 | |
25 | /** |
26 | * The time service. |
27 | * |
28 | * @var \Drupal\Component\Datetime\TimeInterface |
29 | */ |
30 | protected $time; |
31 | |
32 | /** |
33 | * Constructs a OnlineService object. |
34 | * |
35 | * @param \Drupal\Core\Database\Connection $database |
36 | * The database connection. |
37 | * @param \Drupal\Component\Datetime\TimeInterface $time |
38 | * The time service. |
39 | */ |
40 | public function __construct(Connection $database, TimeInterface $time) { |
41 | $this->database = $database; |
42 | $this->time = $time; |
43 | } |
44 | |
45 | /** |
46 | * {@inheritdoc} |
47 | */ |
48 | public function getLast30Minutes() { |
49 | $end = $this->time->getRequestTime(); |
50 | $start = $end - self::MINUTE_30; |
51 | |
52 | return $this->query($start, $end); |
53 | } |
54 | |
55 | /** |
56 | * {@inheritdoc} |
57 | */ |
58 | public function getLast24Hours() { |
59 | $end = $this->time->getRequestTime(); |
60 | $start = $end - self::HOUR_24; |
61 | |
62 | return $this->query($start, $end); |
63 | } |
64 | |
65 | /** |
66 | * {@inheritdoc} |
67 | */ |
68 | public function getYesterday30Minutes() { |
69 | $now = $this->time->getRequestTime(); |
70 | $end = $now - self::HOUR_24; |
71 | $start = $end - self::MINUTE_30; |
72 | |
73 | return $this->query($start, $end); |
74 | } |
75 | |
76 | /** |
77 | * {@inheritdoc} |
78 | */ |
79 | public function getYesterday24Hours() { |
80 | $now = $this->time->getRequestTime(); |
81 | $end = $now - self::HOUR_24; |
82 | $start = $end - self::HOUR_24; |
83 | |
84 | return $this->query($start, $end); |
85 | } |
86 | |
87 | /** |
88 | * {@inheritdoc} |
89 | */ |
90 | public function getLastWeek30Minutes() { |
91 | $now = $this->time->getRequestTime(); |
92 | $end = $now - self::DAY_7; |
93 | $start = $end - self::MINUTE_30; |
94 | |
95 | return $this->query($start, $end); |
96 | } |
97 | |
98 | /** |
99 | * {@inheritdoc} |
100 | */ |
101 | public function getLastWeek24Hours() { |
102 | $now = $this->time->getRequestTime(); |
103 | $end = $now - self::DAY_7; |
104 | $start = $end - self::HOUR_24; |
105 | |
106 | return $this->query($start, $end); |
107 | } |
108 | |
109 | /** |
110 | * Gets the current visitors online. |
111 | * |
112 | * @param int $start |
113 | * The start time. |
114 | * @param int $end |
115 | * The end time. |
116 | * |
117 | * @return int |
118 | * The current visitors online. |
119 | */ |
120 | protected function query(int $start, int $end): int { |
121 | $query = $this->database->select('visitors', 'v'); |
122 | $query->addExpression('COUNT(DISTINCT v.visitor_id)', 'count'); |
123 | $query->condition('v.visitors_date_time', [$start, $end], 'BETWEEN'); |
124 | |
125 | $count = $query->execute()->fetchField(); |
126 | |
127 | return (int) $count; |
128 | } |
129 | |
130 | } |