RecentlyReadService.php in Recently Read 8
File
src/RecentlyReadService.php
View source
<?php
namespace Drupal\recently_read;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\SessionManager;
class RecentlyReadService implements RecentlyReadServiceInterface {
protected $currentUser;
protected $entityTypeManager;
protected $sessionManager;
protected $configFactory;
protected $recentlyReadStorage;
public function __construct(AccountInterface $current_user, EntityTypeManagerInterface $entity_type_manager, SessionManager $sessionManager, ConfigFactory $configFactory) {
$this->currentUser = $current_user;
$this->entityTypeManager = $entity_type_manager;
$this->sessionManager = $sessionManager;
$this->configFactory = $configFactory;
$this->recentlyReadStorage = $this->entityTypeManager
->getStorage('recently_read');
}
public function insertEntity(EntityInterface $entity, AccountInterface $user = NULL) {
$config = $this->configFactory
->getEditable('recently_read.configuration');
$maxRecords = NULL;
if ($config
->get('delete_config') == "count") {
$maxRecords = $config
->get('count');
}
$user = $user ?? $this->currentUser;
if ($user
->isAnonymous()) {
if (!isset($_SESSION['recently_read'])) {
$_SESSION['recently_read'] = TRUE;
$this->sessionManager
->start();
}
$exists = $this->recentlyReadStorage
->loadByProperties([
'session_id' => $this->sessionManager
->getId(),
'type' => $entity
->getEntityTypeId(),
'entity_id' => $entity
->id(),
]);
}
else {
$exists = $this->recentlyReadStorage
->loadByProperties([
'user_id' => $user
->id(),
'type' => $entity
->getEntityTypeId(),
'entity_id' => $entity
->id(),
]);
}
if (!empty($exists)) {
foreach ($exists as $entry) {
$entry
->setCreatedTime(time())
->save();
}
}
else {
$recentlyRead = $this->recentlyReadStorage
->create([
'type' => $entity
->getEntityTypeId(),
'user_id' => $user
->id(),
'entity_id' => $entity
->id(),
'session_id' => $user
->id() ? 0 : $this->sessionManager
->getId(),
'created' => time(),
]);
$recentlyRead
->save();
}
$userRecords = $this
->getRecords($user
->id());
if ($maxRecords && count($userRecords) > $maxRecords) {
$records = array_slice($userRecords, $maxRecords, count($userRecords));
$this
->deleteRecords($records);
}
}
public function deleteRecords(array $records) {
foreach ($records as $rid) {
$recently_read = $this->recentlyReadStorage
->load($rid);
$recently_read
->delete();
}
}
public function getRecords($user_id) {
if ($user_id != 0) {
$records = $this->recentlyReadStorage
->getQuery()
->condition('user_id', $user_id)
->sort('created', 'DESC')
->execute();
}
else {
$records = $this->recentlyReadStorage
->getQuery()
->condition('session_id', session_id())
->sort('created', 'DESC')
->execute();
}
return $records;
}
public function deleteEntityRecords(EntityInterface $entity, AccountInterface $user = NULL) {
$properties = [
'type' => $entity
->getEntityTypeId(),
'entity_id' => $entity
->id(),
];
if ($user) {
if ($user
->isAnonymous()) {
$properties['session_id'] = $this->sessionManager
->getId();
}
else {
$properties['user_id'] = $user
->id();
}
}
$recently_read_entities = $this->recentlyReadStorage
->loadByProperties($properties);
$this->recentlyReadStorage
->delete($recently_read_entities);
}
}