From f049ab0ece7b0e6790e6baf02266f841f6d2532e Mon Sep 17 00:00:00 2001 From: Dragan Gjorgjiev Date: Thu, 27 Jun 2024 10:15:05 +0200 Subject: [PATCH] PJAS-466 Minor refactorings --- src/Driver/DriverInterface.php | 6 +++--- src/Driver/PHPArrayDriver.php | 6 +++--- src/Driver/RedisDriver.php | 33 +++++++++++++++++---------------- src/Job.php | 6 +----- src/Locker.php | 10 +--------- src/Mutex.php | 20 ++------------------ src/ProcessManager.php | 34 +++++----------------------------- src/Queue.php | 8 +------- 8 files changed, 33 insertions(+), 90 deletions(-) diff --git a/src/Driver/DriverInterface.php b/src/Driver/DriverInterface.php index 39116de..3de984e 100644 --- a/src/Driver/DriverInterface.php +++ b/src/Driver/DriverInterface.php @@ -27,7 +27,7 @@ public function add(Job $job, int $maxRetries = 3) : void; * * @param string $id The Job identifier. * - * @return Job + * @return Job|null */ public function get(string $id) : ?Job; @@ -121,7 +121,7 @@ public function storeStatsValue(string $key, $value) : void; * * @return mixed */ - public function getStatsValue(string $key); + public function getStatsValue(string $key) : mixed; /** * Locking System Methods @@ -143,7 +143,7 @@ public function isLocked(string $key) : bool; * * @param string $key * - * @return string + * @return string|null */ public function getLockToken(string $key) : ?string; diff --git a/src/Driver/PHPArrayDriver.php b/src/Driver/PHPArrayDriver.php index 91a7daa..1c0e8f6 100644 --- a/src/Driver/PHPArrayDriver.php +++ b/src/Driver/PHPArrayDriver.php @@ -59,7 +59,7 @@ public function add(Job $job, int $maxRetries = 3) : void * * @param string $id The Job identifier. * - * @return Job + * @return Job|null */ public function get(string $id) : ?Job { @@ -185,7 +185,7 @@ public function storeStatsValue(string $key, $value) : void * * @return mixed */ - public function getStatsValue(string $key) + public function getStatsValue(string $key) : mixed { return $this->stats[$key] ?? null; } @@ -207,7 +207,7 @@ public function isLocked(string $key) : bool * * @param string $key * - * @return string + * @return string|null */ public function getLockToken(string $key) : ?string { diff --git a/src/Driver/RedisDriver.php b/src/Driver/RedisDriver.php index 7931dfc..51f93c0 100644 --- a/src/Driver/RedisDriver.php +++ b/src/Driver/RedisDriver.php @@ -18,11 +18,6 @@ class RedisDriver implements DriverInterface public const REDIS_KEY_DATA = 'data.'; public const REDIS_KEY_STATS = 'stats.'; - /** - * Get the redis connection - */ - protected Redis $redis; - /** * The redis key prefix */ @@ -35,10 +30,11 @@ class RedisDriver implements DriverInterface /** * Constructor + * + * @param Redis $redis The redis instance. */ - public function __construct(Redis $redis) + public function __construct(protected Redis $redis) { - $this->redis = $redis; } /** @@ -50,20 +46,24 @@ public function __construct(Redis $redis) /** * Sets the queues key prefix * - * @param string $prefix + * @param string $prefix + * * @return void */ - public function setQueueKeyPrefix(string $prefix) + public function setQueueKeyPrefix(string $prefix) : void { $this->queuePrefix = $prefix; } /** + * Sets the key prefix + * * @deprecated - * @param string $prefix + * @param string $prefix + * * @return void */ - public function setKeyPrefix(string $prefix) + public function setKeyPrefix(string $prefix) : void { trigger_error('Method ' . __METHOD__ . ' is deprecated, use setQueueKeyPrefix instead.', E_USER_DEPRECATED); $this->setQueueKeyPrefix($prefix); @@ -72,10 +72,11 @@ public function setKeyPrefix(string $prefix) /** * Sets the lock key prefix * - * @param string $prefix + * @param string $prefix + * * @return void */ - public function setLockKeyPrefix(string $prefix) + public function setLockKeyPrefix(string $prefix) : void { $this->lockPrefix = $prefix; } @@ -110,7 +111,7 @@ public function add(Job $job, int $maxRetries = 3) : void * * @param string $id The Job identifier. * - * @return Job + * @return Job|null */ public function get(string $id) : ?Job { @@ -258,7 +259,7 @@ public function storeStatsValue(string $key, $value) : void * * @return mixed */ - public function getStatsValue(string $key) + public function getStatsValue(string $key) : mixed { if (($raw = $this->redis->get($this->queuePrefix . static::REDIS_KEY_STATS . $key)) === false) { throw new InvalidDataException("Could not read stats value from redis."); @@ -290,7 +291,7 @@ public function isLocked(string $key) : bool * * @param string $key * - * @return string + * @return string|null */ public function getLockToken(string $key) : ?string { diff --git a/src/Job.php b/src/Job.php index b1bdf74..bdac001 100644 --- a/src/Job.php +++ b/src/Job.php @@ -8,15 +8,11 @@ class Job { /** * The jobs id - * - * @var string */ private string $id; /** * The jobs action - * - * @var string */ private string $action; @@ -105,7 +101,7 @@ public function serialize() : string * * @param string $data * - * @return Job + * @return Job|null */ public static function unserialize(string $data) : ?Job { diff --git a/src/Locker.php b/src/Locker.php index 66a9b9d..cabd3f7 100644 --- a/src/Locker.php +++ b/src/Locker.php @@ -6,13 +6,6 @@ class Locker { - /** - * Driver instance - * - * @var DriverInterface - */ - private DriverInterface $driver; - /** * Constructor * @@ -20,9 +13,8 @@ class Locker * * @return void */ - public function __construct(DriverInterface $driver) + public function __construct(private DriverInterface $driver) { - $this->driver = $driver; } /** diff --git a/src/Mutex.php b/src/Mutex.php index 36b9d7c..c4e3a3d 100644 --- a/src/Mutex.php +++ b/src/Mutex.php @@ -12,21 +12,7 @@ class Mutex */ public const ERROR_ALREADY_LOCKED = 5; public const ERROR_UNLOCK_FAILURE = 10; - - /** - * Driver instance - * - * @var DriverInterface - */ - private DriverInterface $driver; - - /** - * Mutex key - * - * @var string - */ - private string $lockkey; - + /** * The mutex current token that ensures that only * this instance can unlock a lock. @@ -43,10 +29,8 @@ class Mutex * * @return void */ - public function __construct(DriverInterface $driver, string $lockkey) + public function __construct(private DriverInterface $driver, private string $lockkey) { - $this->driver = $driver; - $this->lockkey = $lockkey; } /** diff --git a/src/ProcessManager.php b/src/ProcessManager.php index 2f5f70f..0f7e476 100644 --- a/src/ProcessManager.php +++ b/src/ProcessManager.php @@ -6,47 +6,25 @@ class ProcessManager { - /** - * A queue instance - * - * @var Queue - */ - protected $queue; - /** * An array of workers - * - * @var array */ - protected $workers = []; + protected array $workers = []; /** * Maximum number of workers - * - * @var int */ - protected $maxWorkers = 32; + protected int $maxWorkers = 32; /** * Should we exit the main loop - * - * @var bool */ - protected $shouldExit = false; + protected bool $shouldExit = false; /** * Idle wait in microseconds - * - * @var int */ - protected $idleWait = 10000; - - /** - * The process pattern - * - * @var string - */ - protected $processPattern; + protected int $idleWait = 10000; /** * Construct @@ -54,10 +32,8 @@ class ProcessManager * @param Queue $queue * @param string $processPattern */ - public function __construct(Queue $queue, string $processPattern) + public function __construct(protected Queue $queue, protected string $processPattern) { - $this->queue = $queue; - $this->processPattern = $processPattern; } /** diff --git a/src/Queue.php b/src/Queue.php index 2bbfb9c..6aac139 100644 --- a/src/Queue.php +++ b/src/Queue.php @@ -6,19 +6,13 @@ class Queue { - /** - * The driver to read the queue - */ - protected DriverInterface $driver; - /** * Constructor * * @param DriverInterface $driver The beryllium driver. */ - public function __construct(DriverInterface $driver) + public function __construct(protected DriverInterface $driver) { - $this->driver = $driver; } /**