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