Lines: 100.00%
| Line | Hits | Source |
|---|---|---|
| 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 | 7 | $this->database = $database; |
| 43 | 7 | $this->time = $time; |
| 44 | } | |
| 45 | ||
| 46 | /** | |
| 47 | * {@inheritdoc} | |
| 48 | */ | |
| 49 | public function getLast30Minutes() { | |
| 50 | 1 | $end = $this->time->getRequestTime(); |
| 51 | 1 | $start = $end - self::MINUTE_30; |
| 52 | ||
| 53 | 1 | return $this->query($start, $end); |
| 54 | } | |
| 55 | ||
| 56 | /** | |
| 57 | * {@inheritdoc} | |
| 58 | */ | |
| 59 | public function getLast24Hours() { | |
| 60 | 1 | $end = $this->time->getRequestTime(); |
| 61 | 1 | $start = $end - self::HOUR_24; |
| 62 | ||
| 63 | 1 | return $this->query($start, $end); |
| 64 | } | |
| 65 | ||
| 66 | /** | |
| 67 | * {@inheritdoc} | |
| 68 | */ | |
| 69 | public function getYesterday30Minutes() { | |
| 70 | 1 | $now = $this->time->getRequestTime(); |
| 71 | 1 | $end = $now - self::HOUR_24; |
| 72 | 1 | $start = $end - self::MINUTE_30; |
| 73 | ||
| 74 | 1 | return $this->query($start, $end); |
| 75 | } | |
| 76 | ||
| 77 | /** | |
| 78 | * {@inheritdoc} | |
| 79 | */ | |
| 80 | public function getYesterday24Hours() { | |
| 81 | 1 | $now = $this->time->getRequestTime(); |
| 82 | 1 | $end = $now - self::HOUR_24; |
| 83 | 1 | $start = $end - self::HOUR_24; |
| 84 | ||
| 85 | 1 | return $this->query($start, $end); |
| 86 | } | |
| 87 | ||
| 88 | /** | |
| 89 | * {@inheritdoc} | |
| 90 | */ | |
| 91 | public function getLastWeek30Minutes() { | |
| 92 | 1 | $now = $this->time->getRequestTime(); |
| 93 | 1 | $end = $now - self::DAY_7; |
| 94 | 1 | $start = $end - self::MINUTE_30; |
| 95 | ||
| 96 | 1 | return $this->query($start, $end); |
| 97 | } | |
| 98 | ||
| 99 | /** | |
| 100 | * {@inheritdoc} | |
| 101 | */ | |
| 102 | public function getLastWeek24Hours() { | |
| 103 | 1 | $now = $this->time->getRequestTime(); |
| 104 | 1 | $end = $now - self::DAY_7; |
| 105 | 1 | $start = $end - self::HOUR_24; |
| 106 | ||
| 107 | 1 | 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 | 6 | $query = $this->database->select('visitors_visit', 'v'); |
| 123 | 6 | $query->addExpression('COUNT(DISTINCT v.visitor_id)', 'count'); |
| 124 | ||
| 125 | 6 | $or = new Condition('OR'); |
| 126 | 6 | $or->condition('v.entry_time', [$start, $end], 'BETWEEN'); |
| 127 | 6 | $or->condition('v.exit_time', [$start, $end], 'BETWEEN'); |
| 128 | ||
| 129 | 6 | $query->condition($or); |
| 130 | ||
| 131 | 6 | $count = $query->execute()->fetchField(); |
| 132 | ||
| 133 | 6 | return (int) $count; |
| 134 | } | |
| 135 | ||
| 136 | } |