Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,95 @@

namespace Jetty\BackgroundProcessing\BackgroundProcess\Adapter\Out\MySql;

use Jetty\BackgroundProcessing\BackgroundProcess\Application\Port\Out\BatchTable;
use Jetty\BackgroundProcessing\BackgroundProcess\Application\Port\Out\QueueBatchRepository;
use Jetty\BackgroundProcessing\BackgroundProcess\Domain\BatchItem;
use Jetty\BackgroundProcessing\BackgroundProcess\Exception\RepositoryException;
use mysqli;
use mysqli_sql_exception;
use Psr\Log\LoggerInterface;

final class OptionsTable implements BatchTable
/**
* Batch repository implemented using mysqli.
*/
final class MySqlOptionsQueueRepository implements QueueBatchRepository
{
/**
* @var mysqli
*/
private $mysqli;

/**
* @var string
*/
private $tableName;

/**
* @var string
*/
/** @var string The queued data prefix */
private $batchPrefix;

/**
* @var string
*/
/** @var string The meta key for locking */
private $lockMetaKey;

/**
* @var LoggerInterface Logs messages
*/
/** @var LoggerInterface Implement to log errors */
private $logger;

public function __construct(LoggerInterface $logger, mysqli $mysqli, string $prefix, string $actionName)
{
$this->logger = $logger;
$this->mysqli = $mysqli;
$this->tableName = "${prefix}options";
/** @var \mysqli Connection for MySQL database */
private $mysqli;

/** @var string The table name */
private $tableName;


/**
* MySqlBatchItemRepository constructor.
*
* @param LoggerInterface $logger Implementation to log errors
* @param \mysqli $mysqli Connection for MySQL database
* @param string $tablePrefix The database prefix
* @param string $actionName The background job definition name
*/
public function __construct(
LoggerInterface $logger,
\mysqli $mysqli,
string $tablePrefix,
string $actionName
) {
$this->batchPrefix = $actionName . '_batch_';
$this->lockMetaKey = "lock_{$actionName}";
$this->logger = $logger;
$this->mysqli = $mysqli;
$this->tableName = "${tablePrefix}options";
}


public function insert(BatchItem $item): void
/**
* {@inheritdoc}
*/
public function createBatchItem($value): QueueBatchRepository
{
$data = serialize($item->value());

$data = $this->mysqli->escape_string($data);
// Prepare key and value for insertion
$key = $this->generateKey();
$data = $this->mysqli->escape_string(
serialize($value)
);

// Execute query
$query = "
INSERT INTO {$this->tableName} (option_name, option_value, autoload)
VALUES ('{$item->key()}', \"${data}\", 'no');";

INSERT INTO {$this->tableName} (option_name, option_value, autoload)
VALUES ('${key}', '${data}', 'no');
";
$result = $this->mysqli->query($query);

if ($result === false)
{
throw new mysqli_sql_exception($this->mysqli->error);
throw new RepositoryException(
'Could not insert a background job item into the wp_options queue.',
0,
new \mysqli_sql_exception($this->mysqli->error)
);
}

return $this;
}


public function readAll(): array
public function batchItemsExist(): bool
{
return count($this->readBatchItems()) > 0;
}


/**
* {@inheritdoc}
*/
public function readBatchItems(): array
{
$query = "
SELECT *
Expand All @@ -77,11 +102,14 @@ public function readAll(): array
$results = $this->mysqli->query($query);
if ($results === false)
{
throw new mysqli_sql_exception('Cannot read batch items');
throw new RepositoryException(
'Cannot read batch items',
0,
new \mysqli_sql_exception($this->mysqli->error)
);
}

$batchItems = [];

foreach ($results as $result)
{
$batchItems[] = new BatchItem($result['option_name'], maybe_unserialize($result['option_value']));
Expand All @@ -91,27 +119,38 @@ public function readAll(): array
}


public function delete(BatchItem $item): void
public function deleteBatchItem(BatchItem $item): QueueBatchRepository
{
$query = "
DELETE FROM {$this->tableName} WHERE option_name='{$item->key()}'";
$query = "DELETE FROM {$this->tableName} WHERE option_name='{$item->key()}'";

$result = $this->mysqli->query($query);

if ($result === false)
{
throw new mysqli_sql_exception('Could not delete item');
throw new RepositoryException(
'Could not delete item.',
0,
new \mysqli_sql_exception($this->mysqli->error)
);
}

return $this;
}


public function persist(): void
public function persist(): QueueBatchRepository
{
$result = $this->mysqli->commit();
if ($result === false)
{
throw new mysqli_sql_exception('Could not commit changes');
throw new RepositoryException(
'Could not commit changes for the background process.',
0,
new \mysqli_sql_exception($this->mysqli->error)
);
}

return $this;
}


Expand Down Expand Up @@ -147,12 +186,6 @@ public function tryGetLock(): bool
}


public function hasItems(): bool
{
return count($this->readAll()) > 0;
}


/**
* Attempt to create a single row for the process so that a lock can be created
*
Expand Down Expand Up @@ -181,4 +214,17 @@ private function tryCreateLockRow(): void
);
}
}


/**
* Generates a unique key based on microtime. BatchItems are
* given a unique key to save to the database.
*/
private function generateKey(): string
{
$length = 32;
$unique = md5(microtime() . rand());
$unique = substr($unique, 0, $length);
return $this->batchPrefix . $unique;
}
}
67 changes: 0 additions & 67 deletions src/BackgroundProcess/Application/Port/Out/BatchTable.php

This file was deleted.