Coverage index

src/Service/OnlineService.php

Lines: 100.00%

LineHitsSource
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) {
427
    $this->database = $database;
437
    $this->time = $time;
44
  }
45
46
  /**
47
   * {@inheritdoc}
48
   */
49
  public function getLast30Minutes() {
501
    $end = $this->time->getRequestTime();
511
    $start = $end - self::MINUTE_30;
52
531
    return $this->query($start, $end);
54
  }
55
56
  /**
57
   * {@inheritdoc}
58
   */
59
  public function getLast24Hours() {
601
    $end = $this->time->getRequestTime();
611
    $start = $end - self::HOUR_24;
62
631
    return $this->query($start, $end);
64
  }
65
66
  /**
67
   * {@inheritdoc}
68
   */
69
  public function getYesterday30Minutes() {
701
    $now = $this->time->getRequestTime();
711
    $end = $now - self::HOUR_24;
721
    $start = $end - self::MINUTE_30;
73
741
    return $this->query($start, $end);
75
  }
76
77
  /**
78
   * {@inheritdoc}
79
   */
80
  public function getYesterday24Hours() {
811
    $now = $this->time->getRequestTime();
821
    $end = $now - self::HOUR_24;
831
    $start = $end - self::HOUR_24;
84
851
    return $this->query($start, $end);
86
  }
87
88
  /**
89
   * {@inheritdoc}
90
   */
91
  public function getLastWeek30Minutes() {
921
    $now = $this->time->getRequestTime();
931
    $end = $now - self::DAY_7;
941
    $start = $end - self::MINUTE_30;
95
961
    return $this->query($start, $end);
97
  }
98
99
  /**
100
   * {@inheritdoc}
101
   */
102
  public function getLastWeek24Hours() {
1031
    $now = $this->time->getRequestTime();
1041
    $end = $now - self::DAY_7;
1051
    $start = $end - self::HOUR_24;
106
1071
    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 {
1226
    $query = $this->database->select('visitors_visit', 'v');
1236
    $query->addExpression('COUNT(DISTINCT v.visitor_id)', 'count');
124
1256
    $or = new Condition('OR');
1266
    $or->condition('v.entry_time', [$start, $end], 'BETWEEN');
1276
    $or->condition('v.exit_time', [$start, $end], 'BETWEEN');
128
1296
    $query->condition($or);
130
1316
    $count = $query->execute()->fetchField();
132
1336
    return (int) $count;
134
  }
135
136
}