class AnonymizeRecords in Library 8
Anonymize records helper.
Hierarchy
- class \Drupal\library\AnonymizeRecords
Expanded class hierarchy of AnonymizeRecords
1 string reference to 'AnonymizeRecords'
1 service uses AnonymizeRecords
File
- src/
AnonymizeRecords.php, line 13
Namespace
Drupal\libraryView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AnonymizeRecords:: |
protected | property | Config. | |
AnonymizeRecords:: |
protected | property | Entity type manager. | |
AnonymizeRecords:: |
protected | property | Logger. | |
AnonymizeRecords:: |
protected | property | State. | |
AnonymizeRecords:: |
protected | property | Library transaction storage. | |
AnonymizeRecords:: |
public | function | Anonymize entry. | |
AnonymizeRecords:: |
private | function | Process batch. | |
AnonymizeRecords:: |
public | function | Constructor. |