Coverage index

src/Service/CounterService.php

Lines: 100.00%

LineHitsSource
1
<?php
2
3
namespace Drupal\visitors\Service;
4
5
use Drupal\Component\Datetime\TimeInterface;
6
use Drupal\Core\Database\Connection;
7
use Drupal\Core\State\StateInterface;
8
use Drupal\visitors\StatisticsViewsResult;
9
use Drupal\visitors\VisitorsCounterInterface;
10
11
/**
12
 * Counts entity views.
13
 */
14
class CounterService implements VisitorsCounterInterface {
15
16
  /**
17
   * The number of seconds in one day.
18
   */
19
  const ONE_DAY = 86400;
20
21
  /**
22
   * The entities viewed.
23
   *
24
   * @var array
25
   */
26
  protected static $entities = [];
27
28
  /**
29
   * The database connection.
30
   *
31
   * @var \Drupal\Core\Database\Connection
32
   */
33
  protected $database;
34
35
  /**
36
   * The date service.
37
   *
38
   * @var \Drupal\Component\Datetime\TimeInterface
39
   */
40
  protected $time;
41
42
  /**
43
   * The state service.
44
   *
45
   * @var \Drupal\Core\State\StateInterface
46
   */
47
  protected $state;
48
49
  /**
50
   * Constructs the counter service.
51
   *
52
   * @param \Drupal\Core\Database\Connection $connection
53
   *   The database connection for the node view storage.
54
   * @param \Drupal\Component\Datetime\TimeInterface $time
55
   *   The date service.
56
   * @param \Drupal\Core\State\StateInterface $state
57
   *   The state service.
58
   */
59
  public function __construct(Connection $connection, TimeInterface $time, StateInterface $state) {
6011
    $this->database = $connection;
6111
    $this->time = $time;
6211
    $this->state = $state;
63
  }
64
65
  /**
66
   * {@inheritdoc}
67
   */
68
  public function recordView(string $type, int $id) {
691
    return (bool) $this->database
701
      ->merge('visitors_counter')
711
      ->key('entity_type', $type)
721
      ->key('entity_id', $id)
731
      ->fields([
741
        'today' => 1,
751
        'total' => 1,
761
        'timestamp' => $this->time->getRequestTime(),
771
      ])
781
      ->expression('today', '[today] + 1')
791
      ->expression('total', '[total] + 1')
801
      ->execute();
81
  }
82
83
  /**
84
   * {@inheritdoc}
85
   */
86
  public function fetchViews(string $type, array $ids): array {
872
    $views = $this->database
882
      ->select('visitors_counter', 'vc')
892
      ->fields('vc', ['total', 'today', 'timestamp'])
902
      ->condition('entity_type', $type)
912
      ->condition('entity_id', $ids, 'IN')
922
      ->execute()
932
      ->fetchAll();
942
    foreach ($views as $id => $view) {
952
      $views[$id] = new StatisticsViewsResult($view->total, $view->today, $view->timestamp);
96
    }
972
    return $views;
98
  }
99
100
  /**
101
   * {@inheritdoc}
102
   */
103
  public function fetchView(string $type, int $id) {
1041
    $views = $this->fetchViews($type, [$id]);
1051
    return reset($views);
106
  }
107
108
  /**
109
   * {@inheritdoc}
110
   */
111
  public function fetchAll(string $type, string $order = 'total', int $limit = 5) {
1122
    assert(in_array($order, ['total', 'today', 'timestamp']), "Invalid order argument.");
113
1141
    return $this->database
1151
      ->select('visitors_counter', 'vc')
1161
      ->fields('vc', ['entity_id'])
1171
      ->condition('entity_type', $type)
1181
      ->orderBy($order, 'DESC')
1191
      ->range(0, $limit)
1201
      ->execute()
1211
      ->fetchCol();
122
  }
123
124
  /**
125
   * {@inheritdoc}
126
   */
127
  public function resetDayCount() {
1282
    $counter_timestamp = $this->state->get('visitors.count_timestamp', 0);
1292
    $now = $this->time->getRequestTime();
1302
    if (($now - $counter_timestamp) >= self::ONE_DAY) {
1311
      $this->state->set('visitors.count_timestamp', $now);
1321
      $this->database->update('visitors_counter')
1331
        ->fields(['today' => 0])
1341
        ->execute();
135
    }
136
  }
137
138
  /**
139
   * {@inheritdoc}
140
   */
141
  public function deleteViews(string $type, int $id) {
1421
    return (bool) $this->database
1431
      ->delete('visitors_counter')
1441
      ->condition('entity_type', $type)
1451
      ->condition('entity_id', $id)
1461
      ->execute();
147
  }
148
149
  /**
150
   * {@inheritdoc}
151
   */
152
  public function maxTotalCount(string $type) {
1531
    $query = $this->database->select('visitors_counter', 'vc');
1541
    $query->addExpression('MAX([total])');
1551
    $query->condition('entity_type', $type);
1561
    $max_total_count = (int) $query->execute()->fetchField();
1571
    return $max_total_count;
158
  }
159
160
}