Skip to content

Commit

Permalink
PJAS-466 Minor refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
dgjorgjiev-prospective committed Jun 27, 2024
1 parent 928dae7 commit f049ab0
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 90 deletions.
6 changes: 3 additions & 3 deletions src/Driver/DriverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -143,7 +143,7 @@ public function isLocked(string $key) : bool;
*
* @param string $key
*
* @return string
* @return string|null
*/
public function getLockToken(string $key) : ?string;

Expand Down
6 changes: 3 additions & 3 deletions src/Driver/PHPArrayDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}
Expand All @@ -207,7 +207,7 @@ public function isLocked(string $key) : bool
*
* @param string $key
*
* @return string
* @return string|null
*/
public function getLockToken(string $key) : ?string
{
Expand Down
33 changes: 17 additions & 16 deletions src/Driver/RedisDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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;
}

/**
Expand All @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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.");
Expand Down Expand Up @@ -290,7 +291,7 @@ public function isLocked(string $key) : bool
*
* @param string $key
*
* @return string
* @return string|null
*/
public function getLockToken(string $key) : ?string
{
Expand Down
6 changes: 1 addition & 5 deletions src/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@ class Job
{
/**
* The jobs id
*
* @var string
*/
private string $id;

/**
* The jobs action
*
* @var string
*/
private string $action;

Expand Down Expand Up @@ -105,7 +101,7 @@ public function serialize() : string
*
* @param string $data
*
* @return Job
* @return Job|null
*/
public static function unserialize(string $data) : ?Job
{
Expand Down
10 changes: 1 addition & 9 deletions src/Locker.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,15 @@

class Locker
{
/**
* Driver instance
*
* @var DriverInterface
*/
private DriverInterface $driver;

/**
* Constructor
*
* @param DriverInterface $driver The beryllium driver.
*
* @return void
*/
public function __construct(DriverInterface $driver)
public function __construct(private DriverInterface $driver)
{
$this->driver = $driver;
}

/**
Expand Down
20 changes: 2 additions & 18 deletions src/Mutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}

/**
Expand Down
34 changes: 5 additions & 29 deletions src/ProcessManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,58 +6,34 @@

class ProcessManager
{
/**
* A queue instance
*
* @var Queue
*/
protected $queue;

/**
* An array of workers
*
* @var array<Process>
*/
protected $workers = [];
protected array $workers = [];

Check failure on line 12 in src/ProcessManager.php

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.1)

Property Beryllium\ProcessManager::$workers type has no value type specified in iterable type array.

Check failure on line 12 in src/ProcessManager.php

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.2)

Property Beryllium\ProcessManager::$workers type has no value type specified in iterable type array.

Check failure on line 12 in src/ProcessManager.php

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.3)

Property Beryllium\ProcessManager::$workers type has no value type specified in iterable type array.

/**
* 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
*
* @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;
}

/**
Expand Down
8 changes: 1 addition & 7 deletions src/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit f049ab0

Please sign in to comment.