You are here

class NodeViewCountRecordsManager in Node view count 8

Provides a class for interacting with nodeviewcount records.

Hierarchy

Expanded class hierarchy of NodeViewCountRecordsManager

1 file declares its use of NodeViewCountRecordsManager
NodeViewCountController.php in src/Controller/NodeViewCountController.php
1 string reference to 'NodeViewCountRecordsManager'
nodeviewcount.services.yml in ./nodeviewcount.services.yml
nodeviewcount.services.yml
1 service uses NodeViewCountRecordsManager
nodeviewcount.records_manager in ./nodeviewcount.services.yml
Drupal\nodeviewcount\NodeViewCountRecordsManager

File

src/NodeViewCountRecordsManager.php, line 17

Namespace

Drupal\nodeviewcount
View source
class NodeViewCountRecordsManager implements NodeViewCountRecordsManagerInterface {

  /**
   * The database connection object.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * Nodeviewcount configuration object.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $config;

  /**
   * Constructs a NodeViewCountRecordsManager object.
   *
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection object.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   Configuration object factory.
   */
  public function __construct(Connection $connection, ConfigFactoryInterface $config_factory) {
    $this->connection = $connection;
    $this->config = $config_factory
      ->get('nodeviewcount.settings');
  }

  /**
   * {@inheritdoc}
   *
   * @throws \Exception
   */
  public function insertRecord($uid, $nid, $uip) {
    $timeZone = date_default_timezone_get();
    $dateTime = new DrupalDateTime('NOW', $timeZone);
    $stringDate = $dateTime
      ->format(DEFAULT_TIME_FORMAT);
    $fields = [
      'nid' => $nid,
      'uid' => $uid,
      'datetime' => strtotime($stringDate),
      'uip' => $uip,
    ];
    $this->connection
      ->insert('nodeviewcount')
      ->fields($fields)
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function getNodeViewsCount(NodeInterface $node, UserInterface $user = NULL, $distinct_users = FALSE) {
    if (!$this
      ->isRecordableForNodeType($node)) {
      return FALSE;
    }
    if (!is_null($user) && !$this
      ->isRecordableForUserRole($user)) {
      return FALSE;
    }
    $query = $this->connection
      ->select('nodeviewcount', 'nvc');
    $query
      ->condition('nid', $node
      ->id(), '=');
    if ($user) {
      $query
        ->condition('uid', $user
        ->id(), '=');
    }
    if ($distinct_users) {
      $query
        ->addExpression('COUNT(DISTINCT uid)');
    }
    else {
      $query
        ->addExpression('COUNT(*)');
    }
    return $query
      ->execute()
      ->fetchAll();
  }

  /**
   * {@inheritdoc}
   */
  public function deleteOldRecords($time) {
    $timeZone = date_default_timezone_get();
    $timeNow = new DrupalDateTime('NOW', $timeZone);
    $unixDate = strtotime($timeNow) - $time;
    $this->connection
      ->delete('nodeviewcount')
      ->condition('datetime', $unixDate, '<')
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function isRecordableForViewMode($view_mode) {
    $tracked_view_modes = $this->config
      ->get('view_modes');
    return in_array($view_mode, $tracked_view_modes, TRUE);
  }

  /**
   * {@inheritdoc}
   */
  public function isRecordableForUserRole(AccountInterface $account) {
    $tracked_user_roles = $this->config
      ->get('user_roles');
    $excluded_user_roles = $this->config
      ->get('excluded_user_roles');
    $is_checked_user_role = array_intersect($tracked_user_roles, $account
      ->getRoles()) !== [];
    $is_in_excluded_user_roles = array_intersect($excluded_user_roles, $account
      ->getRoles()) !== [];
    return $is_checked_user_role && !$is_in_excluded_user_roles;
  }

  /**
   * {@inheritdoc}
   */
  public function isRecordableForNodeType(NodeInterface $node) {
    $tracked_unode_types = $this->config
      ->get('node_types');
    return in_array($node
      ->bundle(), $tracked_unode_types, TRUE);
  }

  /**
   * {@inheritdoc}
   */
  public function getLogsLifeTime() {
    return $this->config
      ->get('logs_life_time');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NodeViewCountRecordsManager::$config protected property Nodeviewcount configuration object.
NodeViewCountRecordsManager::$connection protected property The database connection object.
NodeViewCountRecordsManager::deleteOldRecords public function Delete old nodeviewcount records from database. Overrides NodeViewCountRecordsManagerInterface::deleteOldRecords
NodeViewCountRecordsManager::getLogsLifeTime public function Get lifetime of the nodeviewcount records. Overrides NodeViewCountRecordsManagerInterface::getLogsLifeTime
NodeViewCountRecordsManager::getNodeViewsCount public function Return count of node views. Overrides NodeViewCountRecordsManagerInterface::getNodeViewsCount
NodeViewCountRecordsManager::insertRecord public function Overrides NodeViewCountRecordsManagerInterface::insertRecord
NodeViewCountRecordsManager::isRecordableForNodeType public function Checks whether node of given node type should be counted in statistics. Overrides NodeViewCountRecordsManagerInterface::isRecordableForNodeType
NodeViewCountRecordsManager::isRecordableForUserRole public function Checks whether node should be counted in statistics for given user role. Overrides NodeViewCountRecordsManagerInterface::isRecordableForUserRole
NodeViewCountRecordsManager::isRecordableForViewMode public function Checks whether node in given view mode should be counted in statistics. Overrides NodeViewCountRecordsManagerInterface::isRecordableForViewMode
NodeViewCountRecordsManager::__construct public function Constructs a NodeViewCountRecordsManager object.