UniqueDatabaseQueue.php in Queue Unique 8.2
File
src/UniqueDatabaseQueue.php
View source
<?php
namespace Drupal\queue_unique;
use Drupal\Core\Database\IntegrityConstraintViolationException;
use Drupal\Core\Queue\DatabaseQueue;
class UniqueDatabaseQueue extends DatabaseQueue {
public const TABLE_NAME = 'queue_unique';
public const HASH_COL_LENGTH = 48;
public const HASH_ID = 'sha2';
public function doCreateItem($data) {
try {
$serialized_data = serialize($data);
$query = $this->connection
->insert(static::TABLE_NAME)
->fields([
'name' => $this->name,
'data' => $serialized_data,
'created' => time(),
'hash' => static::hash($this->name, $serialized_data),
]);
return $query
->execute();
} catch (IntegrityConstraintViolationException $e) {
return FALSE;
}
}
public static function hash($name, $serialized_data) {
$substr_length = static::HASH_COL_LENGTH - strlen(static::HASH_ID);
return static::HASH_ID . substr(base64_encode(hash('sha512', $name . $serialized_data, TRUE)), 0, $substr_length);
}
public function schemaDefinition() {
return array_merge_recursive(parent::schemaDefinition(), [
'fields' => [
'hash' => [
'type' => 'char',
'length' => static::HASH_COL_LENGTH,
'not null' => TRUE,
],
],
'unique keys' => [
'unique' => [
'hash',
],
],
]);
}
}