View source
<?php
namespace Drupal\statistics;
use Drupal\Core\Database\Connection;
use Drupal\Core\State\StateInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class NodeStatisticsDatabaseStorage implements StatisticsStorageInterface {
protected $connection;
protected $state;
protected $requestStack;
public function __construct(Connection $connection, StateInterface $state, RequestStack $request_stack) {
$this->connection = $connection;
$this->state = $state;
$this->requestStack = $request_stack;
}
public function recordView($id) {
return (bool) $this->connection
->merge('node_counter')
->key('nid', $id)
->fields([
'daycount' => 1,
'totalcount' => 1,
'timestamp' => $this
->getRequestTime(),
])
->expression('daycount', '[daycount] + 1')
->expression('totalcount', '[totalcount] + 1')
->execute();
}
public function fetchViews($ids) {
$views = $this->connection
->select('node_counter', 'nc')
->fields('nc', [
'totalcount',
'daycount',
'timestamp',
])
->condition('nid', $ids, 'IN')
->execute()
->fetchAll();
foreach ($views as $id => $view) {
$views[$id] = new StatisticsViewsResult($view->totalcount, $view->daycount, $view->timestamp);
}
return $views;
}
public function fetchView($id) {
$views = $this
->fetchViews([
$id,
]);
return reset($views);
}
public function fetchAll($order = 'totalcount', $limit = 5) {
assert(in_array($order, [
'totalcount',
'daycount',
'timestamp',
]), "Invalid order argument.");
return $this->connection
->select('node_counter', 'nc')
->fields('nc', [
'nid',
])
->orderBy($order, 'DESC')
->range(0, $limit)
->execute()
->fetchCol();
}
public function deleteViews($id) {
return (bool) $this->connection
->delete('node_counter')
->condition('nid', $id)
->execute();
}
public function resetDayCount() {
$statistics_timestamp = $this->state
->get('statistics.day_timestamp', 0);
if ($this
->getRequestTime() - $statistics_timestamp >= 86400) {
$this->state
->set('statistics.day_timestamp', $this
->getRequestTime());
$this->connection
->update('node_counter')
->fields([
'daycount' => 0,
])
->execute();
}
}
public function maxTotalCount() {
$query = $this->connection
->select('node_counter', 'nc');
$query
->addExpression('MAX([totalcount])');
$max_total_count = (int) $query
->execute()
->fetchField();
return $max_total_count;
}
protected function getRequestTime() {
return $this->requestStack
->getCurrentRequest()->server
->get('REQUEST_TIME');
}
}