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

5
dgi_fixity.services.yml

@ -1,11 +1,10 @@
services:
logger.channel.dgi_fixity:
class: Drupal\Core\Logger\LoggerChannel
factory: logger.factory:get
parent: logger.channel_base
arguments: ['dgi_fixity']
dgi_fixity.fixity_check:
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:
class: Drupal\dgi_fixity\Routing\FixityCheckRouteSubscriber
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\EntityTypeManagerInterface;
use Drupal\Core\Link;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\dgi_fixity\Entity\FixityCheck;
@ -48,17 +47,10 @@ class FixityCheckService implements FixityCheckServiceInterface {
*/
protected $time;
/**
* The mail manager service.
*
* @var \Drupal\Core\Mail\MailManagerInterface
*/
protected $mailManager;
/**
* The logger for this service.
*
* @var Psr\Log\LoggerInterface
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
@ -72,12 +64,18 @@ class FixityCheckService implements FixityCheckServiceInterface {
/**
* 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->config = $config;
$this->entityTypeManager = $entity_type_manager;
$this->time = $time;
$this->mailManager = $mail_manager;
$this->logger = $logger;
$this->filehash = $filehash;
}
@ -86,10 +84,7 @@ class FixityCheckService implements FixityCheckServiceInterface {
* {@inheritdoc}
*/
public function fromEntityTypes(): array {
return [
'media',
'file',
];
return static::ENTITY_TYPES;
}
/**
@ -223,7 +218,7 @@ class FixityCheckService implements FixityCheckServiceInterface {
// Assume success until proven untrue.
$state = FixityCheck::STATE_MATCHES;
// 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.
if (!isset($file->{$column})) {
$state = FixityCheck::STATE_NO_CHECKSUM;

5
src/FixityCheckServiceInterface.php

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

Loading…
Cancel
Save