View source
<?php
namespace Drupal\linkchecker\Plugin;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Entity\TranslatableInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\QueueFactory;
use Drupal\Core\Session\AccountSwitcherInterface;
use Drupal\Core\Session\UserSession;
use Drupal\linkchecker\LinkCheckerLinkInterface;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class LinkStatusHandlerBase extends PluginBase implements LinkStatusHandlerInterface, ContainerFactoryPluginInterface {
protected $queue;
protected $entityTypeManager;
protected $accountSwitcher;
protected $linkcheckerSetting;
protected $itemsPerBatch;
public function __construct(array $configuration, $plugin_id, $plugin_definition, QueueFactory $queueFactory, EntityTypeManagerInterface $entityTypeManager, AccountSwitcherInterface $accountSwitcher, ImmutableConfig $linkcheckerSetting) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->queue = $queueFactory
->get('linkchecker_status_handle');
$this->entityTypeManager = $entityTypeManager;
$this->accountSwitcher = $accountSwitcher;
$this->linkcheckerSetting = $linkcheckerSetting;
$this->itemsPerBatch = 10;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('queue'), $container
->get('entity_type.manager'), $container
->get('account_switcher'), $container
->get('config.factory')
->get('linkchecker.settings'));
}
public function queueItems(LinkCheckerLinkInterface $link, ResponseInterface $response) {
$items = $this
->getItems($link, $response);
if (empty($this->queue
->numberOfItems())) {
$this->queue
->createQueue();
}
foreach ($items as $item) {
$data = [];
$data['links'] = $item;
$data['response'] = $response;
$data['handler'] = $this
->getPluginId();
$this->queue
->createItem($data);
}
}
public function handle(LinkCheckerLinkInterface $link, ResponseInterface $response) {
$this
->switchSession();
$entity = $link
->getParentEntity();
if ($entity instanceof TranslatableInterface) {
if ($entity
->hasTranslation($link
->getParentEntityLangcode())) {
$entity = $entity
->getTranslation($link
->getParentEntityLangcode());
$this
->doHandle($link, $response, $entity);
}
}
else {
$this
->doHandle($link, $response, $entity);
}
$this
->switchSessionBack();
}
protected abstract function doHandle(LinkCheckerLinkInterface $link, ResponseInterface $response, FieldableEntityInterface $entity);
protected function getItems(LinkCheckerLinkInterface $link, ResponseInterface $response) {
$linkStorage = $this->entityTypeManager
->getStorage($link
->getEntityTypeId());
$query = $linkStorage
->getQuery();
$query
->condition('urlhash', $link
->getHash());
$linkIds = $query
->execute();
return array_chunk($linkIds, $this->itemsPerBatch, TRUE);
}
protected function switchSession() {
$this->accountSwitcher
->switchTo(new UserSession([
'uid' => user_load_by_name($this->linkcheckerSetting
->get('error.impersonate_account')),
]));
}
protected function switchSessionBack() {
$this->accountSwitcher
->switchBack();
}
}