Compare commits

..

16 Commits

  1. 4
      composer.json
  2. 10
      dgi_fixity.module
  3. 5
      dgi_fixity.services.yml
  4. 27
      src/FixityCheckService.php
  5. 5
      src/FixityCheckServiceInterface.php

4
composer.json

@ -1,8 +1,8 @@
{ {
"name": "discoverygarden/dgi_fixity", "name": "roblib/dgi_fixity",
"type": "drupal-module", "type": "drupal-module",
"license": "GPL-2.0-or-later", "license": "GPL-2.0-or-later",
"require": { "require": {
"drupal/filehash": "^2.0" "drupal/filehash": "^3.0"
} }
} }

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);
} }
/** /**

5
dgi_fixity.services.yml

@ -1,11 +1,10 @@
services: services:
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']
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', '@plugin.manager.mail', '@logger.channel.dgi_fixity', '@filehash'] arguments: ['@string_translation', '@config.factory', '@entity_type.manager', '@datetime.time', '@logger.channel.dgi_fixity', '@filehash']
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']

27
src/FixityCheckService.php

@ -7,7 +7,6 @@ use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Link; use Drupal\Core\Link;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\dgi_fixity\Entity\FixityCheck; use Drupal\dgi_fixity\Entity\FixityCheck;
@ -48,17 +47,10 @@ class FixityCheckService implements FixityCheckServiceInterface {
*/ */
protected $time; protected $time;
/**
* The mail manager service.
*
* @var \Drupal\Core\Mail\MailManagerInterface
*/
protected $mailManager;
/** /**
* The logger for this service. * The logger for this service.
* *
* @var Psr\Log\LoggerInterface * @var \Psr\Log\LoggerInterface
*/ */
protected $logger; protected $logger;
@ -72,12 +64,18 @@ class FixityCheckService implements FixityCheckServiceInterface {
/** /**
* Constructor. * Constructor.
*/ */
public function __construct(TranslationInterface $string_translation, ConfigFactoryInterface $config, EntityTypeManagerInterface $entity_type_manager, TimeInterface $time, MailManagerInterface $mail_manager, LoggerInterface $logger, FileHash $filehash) { public function __construct(
TranslationInterface $string_translation,
ConfigFactoryInterface $config,
EntityTypeManagerInterface $entity_type_manager,
TimeInterface $time,
LoggerInterface $logger,
FileHash $filehash,
) {
$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->mailManager = $mail_manager;
$this->logger = $logger; $this->logger = $logger;
$this->filehash = $filehash; $this->filehash = $filehash;
} }
@ -86,10 +84,7 @@ class FixityCheckService implements FixityCheckServiceInterface {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function fromEntityTypes(): array { public function fromEntityTypes(): array {
return [ return static::ENTITY_TYPES;
'media',
'file',
];
} }
/** /**
@ -223,7 +218,7 @@ class FixityCheckService implements FixityCheckServiceInterface {
// Assume success until proven untrue. // Assume success until proven untrue.
$state = FixityCheck::STATE_MATCHES; $state = FixityCheck::STATE_MATCHES;
// If column is set, only generate that hash. // If column is set, only generate that hash.
foreach ($this->filehash->algos() as $column => $algo) { foreach ($this->filehash->getEnabledAlgorithms() as $column => $algo) {
// Nothing to do if the previous checksum value is not known. // Nothing to do if the previous checksum value is not known.
if (!isset($file->{$column})) { if (!isset($file->{$column})) {
$state = FixityCheck::STATE_NO_CHECKSUM; $state = FixityCheck::STATE_NO_CHECKSUM;

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