EntityInteractionCollector.php in Better Statistics 8
File
src/EntityInteractionCollector.php
View source
<?php
namespace Drupal\better_statistics;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountProxy;
use Drupal\sapi\ActionTypeInterface;
use Drupal\sapi\Dispatcher;
use Drupal\sapi\ActionTypeManager;
class EntityInteractionCollector {
protected $sapiDispatcher;
protected $sapiActionTypeManager;
public function __construct(Dispatcher $sapiDispatcher, ActionTypeManager $sapiActionTypeManager) {
$this->sapiDispatcher = $sapiDispatcher;
$this->sapiActionTypeManager = $sapiActionTypeManager;
}
public function actionTypeTrigger(EntityInterface $entity, AccountProxy $account, $operation) {
$allowed_entities = [
'node',
'user',
'comments',
'taxonomy_term',
];
if (!in_array($entity
->getEntityTypeId(), $allowed_entities)) {
return;
}
try {
$action = $this->sapiActionTypeManager
->createInstance('entity_interaction', [
'entity' => $entity,
'action' => $operation,
'account' => $account,
]);
if (!$action instanceof ActionTypeInterface) {
throw new \Exception('No entity_interaction plugin was found');
}
$this->sapiDispatcher
->dispatch($action);
} catch (\Exception $e) {
watchdog_exception('better_statistics', $e);
}
}
}