Browse Source

Dependency injection

pull/14/head
Chris MacDonald 11 months ago
parent
commit
44d17ef7bf
  1. 69
      src/Plugin/QueueWorker/ProcessSourceWorker.php

69
src/Plugin/QueueWorker/ProcessSourceWorker.php

@ -2,9 +2,12 @@
namespace Drupal\dgi_fixity\Plugin\QueueWorker; namespace Drupal\dgi_fixity\Plugin\QueueWorker;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\QueueWorkerBase; use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\Core\Queue\RequeueException; use Drupal\Core\Queue\RequeueException;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\AccountSwitcherInterface;
use Drupal\dgi_fixity\FixityCheckServiceInterface; use Drupal\dgi_fixity\FixityCheckServiceInterface;
use Drupal\user\Entity\User; use Drupal\user\Entity\User;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
@ -27,6 +30,20 @@ class ProcessSourceWorker extends QueueWorkerBase implements ContainerFactoryPlu
*/ */
protected $fixity; 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. * Constructs a new FixityCheckWorker instance.
* *
@ -38,10 +55,17 @@ class ProcessSourceWorker extends QueueWorkerBase implements ContainerFactoryPlu
* The plugin implementation definition. * The plugin implementation definition.
* @param \Drupal\dgi_fixity\FixityCheckServiceInterface $fixity * @param \Drupal\dgi_fixity\FixityCheckServiceInterface $fixity
* The fixity check service. * 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); parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->fixity = $fixity; $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_id,
$plugin_definition, $plugin_definition,
$container->get('dgi_fixity.fixity_check'), $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) { public function processItem($data) {
// To avoid expensive access calls // To avoid expensive access calls
$account_switcher = \Drupal::service('account_switcher'); $user_storage = $this->entityTypeManager->getStorage('user');
$account_switcher->switchTo(User::load(1)); $account = $user_storage->load(1);
/** @var \Drupal\dgi_fixity\FixityCheckServiceInterface $fixity */ if ($account instanceof AccountInterface) {
$fixity = \Drupal::service('dgi_fixity.fixity_check'); $this->accountSwitcher->switchTo($account);
$view = $fixity->source($data, 1000);
$view->execute(); /** @var \Drupal\dgi_fixity\FixityCheckServiceInterface $fixity */
// Only processes those which have not already enabled periodic checks. $fixity = \Drupal::service('dgi_fixity.fixity_check');
foreach ($view->result as $row) { $view = $fixity->source($data, 1000);
/** @var \Drupal\dgi_fixity\FixityCheckInterface $check */ $view->execute();
$check = $view->field['periodic']->getEntity($row); // Only processes those which have not already enabled periodic checks.
$check->setPeriodic(TRUE); foreach ($view->result as $row) {
$check->save(); /** @var \Drupal\dgi_fixity\FixityCheckInterface $check */
} $check = $view->field['periodic']->getEntity($row);
// Not finished processing. $check->setPeriodic(TRUE);
if (count($view->result) !== 0) { $check->save();
throw new RequeueException(); }
} // Not finished processing.
if (count($view->result) !== 0) {
$this->accountSwitcher->switchBack();
throw new RequeueException();
}
$account_switcher->switchBack(); $this->accountSwitcher->switchBack();
}
} }
} }

Loading…
Cancel
Save