Browse Source

Avoid circular dependency.

pull/24/head
Adam Vessey 7 months ago
parent
commit
ef540161f5
No known key found for this signature in database
GPG Key ID: 89B39535BF6D0D39
  1. 10
      dgi_fixity.module
  2. 7
      dgi_fixity.services.yml
  3. 44
      src/FixityCheckService.php
  4. 5
      src/FixityCheckServiceInterface.php

10
dgi_fixity.module

@ -17,6 +17,7 @@ use Drupal\Core\Mail\MailFormatHelper;
use Drupal\Core\Render\Element; use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\dgi_fixity\FixityCheckServiceInterface;
use Drupal\dgi_fixity\Form\SettingsForm; use Drupal\dgi_fixity\Form\SettingsForm;
use Drupal\file\Plugin\Field\FieldWidget\FileWidget; use Drupal\file\Plugin\Field\FieldWidget\FileWidget;
use Drupal\user\Entity\User; use Drupal\user\Entity\User;
@ -145,15 +146,16 @@ function dgi_fixity_cron() {
* Implements hook_entity_type_alter(). * Implements hook_entity_type_alter().
*/ */
function dgi_fixity_entity_type_alter(array &$entity_types) { function dgi_fixity_entity_type_alter(array &$entity_types) {
/** @var \Drupal\dgi_fixity\FixityCheckServiceInterface $fixity */ // XXX: Cannot reference dgi_fixity.fixity_check:fromEntityTypes() due to
$fixity = \Drupal::service('dgi_fixity.fixity_check'); // circular dependencies, as dgi_fixity.fixity_check makes use of the
$supported_entity_types = $fixity->fromEntityTypes(); // entity_type.manager that we are in the middle of trying to build.
foreach ($supported_entity_types as $entity_type_id) { foreach (FixityCheckServiceInterface::ENTITY_TYPES as $entity_type_id) {
$entity_type = &$entity_types[$entity_type_id]; $entity_type = &$entity_types[$entity_type_id];
$entity_type->setLinkTemplate('fixity-audit', "/fixity/$entity_type_id/{{$entity_type_id}}"); $entity_type->setLinkTemplate('fixity-audit', "/fixity/$entity_type_id/{{$entity_type_id}}");
$entity_type->setLinkTemplate('fixity-check', "/fixity/$entity_type_id/{{$entity_type_id}}/check"); $entity_type->setLinkTemplate('fixity-check', "/fixity/$entity_type_id/{{$entity_type_id}}/check");
$entity_type->setFormClass('fixity-check', 'Drupal\dgi_fixity\Form\CheckForm'); $entity_type->setFormClass('fixity-check', 'Drupal\dgi_fixity\Form\CheckForm');
} }
unset($entity_type);
} }
/** /**

7
dgi_fixity.services.yml

@ -1,9 +1,7 @@
services: services:
dgi_fixity.fixity_check: dgi_fixity.fixity_check:
class: Drupal\dgi_fixity\FixityCheckService class: Drupal\dgi_fixity\FixityCheckService
arguments: ['@string_translation', '@config.factory', '@entity_type.manager', '@datetime.time', '@filehash'] arguments: ['@string_translation', '@config.factory', '@entity_type.manager', '@datetime.time', '@filehash', '@logger.channel.dgi_fixity']
calls:
- [setLoggerFactory, ['@logger.factory']]
dgi_fixity.route_subscriber: dgi_fixity.route_subscriber:
class: Drupal\dgi_fixity\Routing\FixityCheckRouteSubscriber class: Drupal\dgi_fixity\Routing\FixityCheckRouteSubscriber
arguments: ['@entity_type.manager', '@dgi_fixity.fixity_check'] arguments: ['@entity_type.manager', '@dgi_fixity.fixity_check']
@ -15,6 +13,5 @@ services:
tags: tags:
- { name: paramconverter } - { name: paramconverter }
logger.channel.dgi_fixity: logger.channel.dgi_fixity:
class: Drupal\Core\Logger\LoggerChannel parent: logger.channel_base
factory: logger.factory:get
arguments: ['dgi_fixity'] arguments: ['dgi_fixity']

44
src/FixityCheckService.php

@ -18,6 +18,7 @@ use Drupal\filehash\FileHash;
use Drupal\media\MediaInterface; use Drupal\media\MediaInterface;
use Drupal\views\ViewExecutable; use Drupal\views\ViewExecutable;
use Drupal\views\Views; use Drupal\views\Views;
use Psr\Log\LoggerInterface;
/** /**
* Decorates the FileHash services adding additional functionality. * Decorates the FileHash services adding additional functionality.
@ -61,13 +62,6 @@ class FixityCheckService implements FixityCheckServiceInterface {
*/ */
protected $filehash; protected $filehash;
/**
* The logger factory.
*
* @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
*/
protected LoggerChannelFactoryInterface $loggerFactory;
/** /**
* Constructor. * Constructor.
*/ */
@ -76,46 +70,22 @@ class FixityCheckService implements FixityCheckServiceInterface {
ConfigFactoryInterface $config, ConfigFactoryInterface $config,
EntityTypeManagerInterface $entity_type_manager, EntityTypeManagerInterface $entity_type_manager,
TimeInterface $time, TimeInterface $time,
FileHash $filehash FileHash $filehash,
LoggerInterface $logger,
) { ) {
$this->stringTranslation = $string_translation; $this->stringTranslation = $string_translation;
$this->config = $config; $this->config = $config;
$this->entityTypeManager = $entity_type_manager; $this->entityTypeManager = $entity_type_manager;
$this->time = $time; $this->time = $time;
$this->filehash = $filehash; $this->filehash = $filehash;
} $this->logger = $logger;
/**
* Sets the logger factory.
*
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $loggerFactory
* The logger factory.
*/
public function setLoggerFactory(LoggerChannelFactoryInterface $loggerFactory) {
$this->loggerFactory = $loggerFactory;
}
/**
* Gets the logger channel.
*
* @return \Psr\Log\LoggerInterface
* The logger channel.
*/
protected function getLogger() {
if (!isset($this->logger)) {
$this->logger = $this->loggerFactory->get('dgi_fixity');
}
return $this->logger;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function fromEntityTypes(): array { public function fromEntityTypes(): array {
return [ return static::ENTITY_TYPES;
'media',
'file',
];
} }
/** /**
@ -298,10 +268,10 @@ class FixityCheckService implements FixityCheckServiceInterface {
)->toString(), )->toString(),
]; ];
if ($check->passed()) { if ($check->passed()) {
$this->getLogger()->info($message, $args); $this->logger->info($message, $args);
} }
else { else {
$this->getLogger()->error($message, $args); $this->logger->error($message, $args);
} }
return $check; return $check;

5
src/FixityCheckServiceInterface.php

@ -12,6 +12,11 @@ use Drupal\views\ViewExecutable;
*/ */
interface FixityCheckServiceInterface { interface FixityCheckServiceInterface {
const ENTITY_TYPES = [
'media',
'file',
];
/** /**
* A list of entity types which be converted into a fixity_check entity. * A list of entity types which be converted into a fixity_check entity.
* *

Loading…
Cancel
Save