You are here

class AnonymizeRecords in Library 8

Anonymize records helper.

Hierarchy

Expanded class hierarchy of AnonymizeRecords

1 string reference to 'AnonymizeRecords'
library.services.yml in ./library.services.yml
library.services.yml
1 service uses AnonymizeRecords
library.anonmyize_records in ./library.services.yml
Drupal\library\AnonymizeRecords

File

src/AnonymizeRecords.php, line 13

Namespace

Drupal\library
View source
class AnonymizeRecords {

  /**
   * Config.
   *
   * @var \Drupal\Core\Config\Config|\Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * State.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * Logger.
   *
   * @var \Drupal\Core\Logger\LoggerChannelInterface
   */
  protected $logger;

  /**
   * Entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Library transaction storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $transactionStorage;

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactory $config
   *   Config for settings.
   * @param \Drupal\Core\State\StateInterface $state
   *   State.
   * @param \Drupal\Core\Logger\LoggerChannelInterface $logger
   *   Logger.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   Entity type manager.
   */
  public function __construct(ConfigFactory $config, StateInterface $state, LoggerChannelInterface $logger, EntityTypeManagerInterface $entity_type_manager) {
    $this->config = $config
      ->get('library.settings');
    $this->state = $state;
    $this->logger = $logger;
    $this->entityTypeManager = $entity_type_manager;
    $this->transactionStorage = $entity_type_manager
      ->getStorage('library_transaction');
  }

  /**
   * Anonymize entry.
   */
  public function anonymize() : void {
    switch ($this->config
      ->get('anonymize_transactions')) {
      case 'daily':
        $interval = 86400;
        break;
      case 'weekly':
        $interval = 86400 * 7;
        break;
      case 'monthly':
        $interval = 86400 * 30;
        break;
      case 'never':
      default:
        $interval = 0;
        break;
    }
    $lastCheck = $this->state
      ->get('library_last_anonymization');
    if ($interval > 0 && strtotime('today') + $interval >= $lastCheck) {
      $items = \Drupal::entityQuery('library_item')
        ->condition('library_status', LibraryItemInterface::ITEM_AVAILABLE)
        ->execute();
      $this
        ->processBatch($items);
      $this->state
        ->set('library_last_anonymization', strtotime('today'));
    }
  }

  /**
   * Process batch.
   *
   * @param array $items
   *   Items to anonymize.
   */
  private function processBatch(array $items) : void {
    $results = [];
    foreach ($items as $item) {
      $transactions = $this->transactionStorage
        ->getQuery()
        ->Exists('uid')
        ->condition('library_item', $item)
        ->execute();

      /** @var \Drupal\library\Entity\LibraryTransaction[] $transactionEntities */
      $transactionEntities = $this->transactionStorage
        ->loadMultiple($transactions);
      foreach ($transactionEntities as $transaction) {
        $transaction
          ->set('uid', NULL);
        $results[] = $transaction
          ->save();
      }
    }
    if ($results) {
      $this->logger
        ->notice('@count transactions anonymized.', [
        '@count' => count($results),
      ]);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AnonymizeRecords::$config protected property Config.
AnonymizeRecords::$entityTypeManager protected property Entity type manager.
AnonymizeRecords::$logger protected property Logger.
AnonymizeRecords::$state protected property State.
AnonymizeRecords::$transactionStorage protected property Library transaction storage.
AnonymizeRecords::anonymize public function Anonymize entry.
AnonymizeRecords::processBatch private function Process batch.
AnonymizeRecords::__construct public function Constructor.