From c7b95174f68320930553f6ee1dfcec24415ec65e Mon Sep 17 00:00:00 2001 From: Chris MacDonald <31731869+chrismacdonaldw@users.noreply.github.com> Date: Thu, 15 Feb 2024 15:52:29 -0400 Subject: [PATCH 1/3] Process fixity as user 1 --- src/Plugin/QueueWorker/ProcessSourceWorker.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Plugin/QueueWorker/ProcessSourceWorker.php b/src/Plugin/QueueWorker/ProcessSourceWorker.php index 77e5266..58c8d4b 100644 --- a/src/Plugin/QueueWorker/ProcessSourceWorker.php +++ b/src/Plugin/QueueWorker/ProcessSourceWorker.php @@ -6,6 +6,7 @@ use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Queue\QueueWorkerBase; use Drupal\Core\Queue\RequeueException; use Drupal\dgi_fixity\FixityCheckServiceInterface; +use Drupal\user\Entity\User; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -59,6 +60,10 @@ class ProcessSourceWorker extends QueueWorkerBase implements ContainerFactoryPlu * {@inheritdoc} */ public function processItem($data) { + // To avoid expensive access calls + $account_switcher = \Drupal::service('account_switcher'); + $account_switcher->switchTo(User::load(1)); + /** @var \Drupal\dgi_fixity\FixityCheckServiceInterface $fixity */ $fixity = \Drupal::service('dgi_fixity.fixity_check'); $view = $fixity->source($data, 1000); @@ -74,6 +79,8 @@ class ProcessSourceWorker extends QueueWorkerBase implements ContainerFactoryPlu if (count($view->result) !== 0) { throw new RequeueException(); } + + $account_switcher->switchBack(); } } From 44d17ef7bf5318b3bd99a75b362c93a13571cebb Mon Sep 17 00:00:00 2001 From: Chris MacDonald <31731869+chrismacdonaldw@users.noreply.github.com> Date: Thu, 15 Feb 2024 16:18:52 -0400 Subject: [PATCH 2/3] Dependency injection --- .../QueueWorker/ProcessSourceWorker.php | 69 ++++++++++++++----- 1 file changed, 50 insertions(+), 19 deletions(-) diff --git a/src/Plugin/QueueWorker/ProcessSourceWorker.php b/src/Plugin/QueueWorker/ProcessSourceWorker.php index 58c8d4b..7d39444 100644 --- a/src/Plugin/QueueWorker/ProcessSourceWorker.php +++ b/src/Plugin/QueueWorker/ProcessSourceWorker.php @@ -2,9 +2,12 @@ namespace Drupal\dgi_fixity\Plugin\QueueWorker; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Queue\QueueWorkerBase; use Drupal\Core\Queue\RequeueException; +use Drupal\Core\Session\AccountInterface; +use Drupal\Core\Session\AccountSwitcherInterface; use Drupal\dgi_fixity\FixityCheckServiceInterface; use Drupal\user\Entity\User; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -27,6 +30,20 @@ class ProcessSourceWorker extends QueueWorkerBase implements ContainerFactoryPlu */ protected $fixity; + /** + * The account switcher service. + * + * @var \Drupal\Core\Session\AccountSwitcherInterface + */ + protected $accountSwitcher; + + /** + * The entity type manager. + * + * @var \Drupal\Core\Entity\EntityTypeManagerInterface + */ + protected $entityTypeManager; + /** * Constructs a new FixityCheckWorker instance. * @@ -38,10 +55,17 @@ class ProcessSourceWorker extends QueueWorkerBase implements ContainerFactoryPlu * The plugin implementation definition. * @param \Drupal\dgi_fixity\FixityCheckServiceInterface $fixity * The fixity check service. + * @param \Drupal\Core\Session\AccountSwitcherInterface $account_switcher + * The account switcher service. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * The entity type manager. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, FixityCheckServiceInterface $fixity) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, FixityCheckServiceInterface $fixity, + AccountSwitcherInterface $account_switcher, EntityTypeManagerInterface $entity_type_manager) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->fixity = $fixity; + $this->accountSwitcher = $account_switcher; + $this->entityTypeManager = $entity_type_manager; } /** @@ -53,6 +77,8 @@ class ProcessSourceWorker extends QueueWorkerBase implements ContainerFactoryPlu $plugin_id, $plugin_definition, $container->get('dgi_fixity.fixity_check'), + $container->get('account_switcher'), + $container->get('entity_type.manager'), ); } @@ -61,26 +87,31 @@ class ProcessSourceWorker extends QueueWorkerBase implements ContainerFactoryPlu */ public function processItem($data) { // To avoid expensive access calls - $account_switcher = \Drupal::service('account_switcher'); - $account_switcher->switchTo(User::load(1)); + $user_storage = $this->entityTypeManager->getStorage('user'); + $account = $user_storage->load(1); - /** @var \Drupal\dgi_fixity\FixityCheckServiceInterface $fixity */ - $fixity = \Drupal::service('dgi_fixity.fixity_check'); - $view = $fixity->source($data, 1000); - $view->execute(); - // Only processes those which have not already enabled periodic checks. - foreach ($view->result as $row) { - /** @var \Drupal\dgi_fixity\FixityCheckInterface $check */ - $check = $view->field['periodic']->getEntity($row); - $check->setPeriodic(TRUE); - $check->save(); - } - // Not finished processing. - if (count($view->result) !== 0) { - throw new RequeueException(); - } + if ($account instanceof AccountInterface) { + $this->accountSwitcher->switchTo($account); + + /** @var \Drupal\dgi_fixity\FixityCheckServiceInterface $fixity */ + $fixity = \Drupal::service('dgi_fixity.fixity_check'); + $view = $fixity->source($data, 1000); + $view->execute(); + // Only processes those which have not already enabled periodic checks. + foreach ($view->result as $row) { + /** @var \Drupal\dgi_fixity\FixityCheckInterface $check */ + $check = $view->field['periodic']->getEntity($row); + $check->setPeriodic(TRUE); + $check->save(); + } + // Not finished processing. + if (count($view->result) !== 0) { + $this->accountSwitcher->switchBack(); + throw new RequeueException(); + } - $account_switcher->switchBack(); + $this->accountSwitcher->switchBack(); + } } } From 9371f099d1c95d5ca1fd317bd2f313269481b39e Mon Sep 17 00:00:00 2001 From: Chris MacDonald <31731869+chrismacdonaldw@users.noreply.github.com> Date: Thu, 15 Feb 2024 16:20:26 -0400 Subject: [PATCH 3/3] Address linting issues --- src/Plugin/QueueWorker/ProcessSourceWorker.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Plugin/QueueWorker/ProcessSourceWorker.php b/src/Plugin/QueueWorker/ProcessSourceWorker.php index 7d39444..d8fdb8d 100644 --- a/src/Plugin/QueueWorker/ProcessSourceWorker.php +++ b/src/Plugin/QueueWorker/ProcessSourceWorker.php @@ -9,7 +9,6 @@ use Drupal\Core\Queue\RequeueException; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountSwitcherInterface; use Drupal\dgi_fixity\FixityCheckServiceInterface; -use Drupal\user\Entity\User; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -60,8 +59,7 @@ class ProcessSourceWorker extends QueueWorkerBase implements ContainerFactoryPlu * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, FixityCheckServiceInterface $fixity, - AccountSwitcherInterface $account_switcher, EntityTypeManagerInterface $entity_type_manager) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, FixityCheckServiceInterface $fixity, AccountSwitcherInterface $account_switcher, EntityTypeManagerInterface $entity_type_manager) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->fixity = $fixity; $this->accountSwitcher = $account_switcher; @@ -86,7 +84,7 @@ class ProcessSourceWorker extends QueueWorkerBase implements ContainerFactoryPlu * {@inheritdoc} */ public function processItem($data) { - // To avoid expensive access calls + // To avoid expensive access calls. $user_storage = $this->entityTypeManager->getStorage('user'); $account = $user_storage->load(1);