Skip to content

Commit 8a2de3d

Browse files
jderussenicolas-grekas
authored andcommitted
[Lock][Semaphore] Add union types
1 parent 864915d commit 8a2de3d

File tree

5 files changed

+16
-41
lines changed

5 files changed

+16
-41
lines changed

Store/MongoDbStore.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@
4747
*/
4848
class MongoDbStore implements PersistingStoreInterface
4949
{
50+
use ExpiringStoreTrait;
51+
5052
private $collection;
5153
private $client;
5254
private $uri;
5355
private $options;
5456
private $initialTtl;
5557

56-
use ExpiringStoreTrait;
57-
5858
/**
5959
* @param Collection|Client|string $mongo An instance of a Collection or Client or URI @see https://docs.mongodb.com/manual/reference/connection-string/
6060
* @param array $options See below
@@ -88,7 +88,7 @@ class MongoDbStore implements PersistingStoreInterface
8888
* readPreference is primary for all queries.
8989
* @see https://docs.mongodb.com/manual/applications/replication/
9090
*/
91-
public function __construct($mongo, array $options = [], float $initialTtl = 300.0)
91+
public function __construct(Collection|Client|string $mongo, array $options = [], float $initialTtl = 300.0)
9292
{
9393
$this->options = array_merge([
9494
'gcProbablity' => 0.001,
@@ -104,10 +104,8 @@ public function __construct($mongo, array $options = [], float $initialTtl = 300
104104
$this->collection = $mongo;
105105
} elseif ($mongo instanceof Client) {
106106
$this->client = $mongo;
107-
} elseif (\is_string($mongo)) {
108-
$this->uri = $this->skimUri($mongo);
109107
} else {
110-
throw new InvalidArgumentException(sprintf('"%s()" requires "%s" or "%s" or URI as first argument, "%s" given.', __METHOD__, Collection::class, Client::class, get_debug_type($mongo)));
108+
$this->uri = $this->skimUri($mongo);
111109
}
112110

113111
if (!($mongo instanceof Collection)) {

Store/PdoStore.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,15 @@ class PdoStore implements PersistingStoreInterface
6767
* * db_password: The password when lazy-connect [default: '']
6868
* * db_connection_options: An array of driver-specific connection options [default: []]
6969
*
70-
* @param \PDO|Connection|string $connOrDsn A \PDO or Connection instance or DSN string or null
71-
* @param array $options An associative array of options
72-
* @param float $gcProbability Probability expressed as floating number between 0 and 1 to clean old locks
73-
* @param int $initialTtl The expiration delay of locks in seconds
70+
* @param array $options An associative array of options
71+
* @param float $gcProbability Probability expressed as floating number between 0 and 1 to clean old locks
72+
* @param int $initialTtl The expiration delay of locks in seconds
7473
*
7574
* @throws InvalidArgumentException When first argument is not PDO nor Connection nor string
7675
* @throws InvalidArgumentException When PDO error mode is not PDO::ERRMODE_EXCEPTION
7776
* @throws InvalidArgumentException When the initial ttl is not valid
7877
*/
79-
public function __construct($connOrDsn, array $options = [], float $gcProbability = 0.01, int $initialTtl = 300)
78+
public function __construct(\PDO|Connection|string $connOrDsn, array $options = [], float $gcProbability = 0.01, int $initialTtl = 300)
8079
{
8180
if ($gcProbability < 0 || $gcProbability > 1) {
8281
throw new InvalidArgumentException(sprintf('"%s" requires gcProbability between 0 and 1, "%f" given.', __METHOD__, $gcProbability));
@@ -93,10 +92,8 @@ public function __construct($connOrDsn, array $options = [], float $gcProbabilit
9392
$this->conn = $connOrDsn;
9493
} elseif ($connOrDsn instanceof Connection) {
9594
$this->conn = $connOrDsn;
96-
} elseif (\is_string($connOrDsn)) {
97-
$this->dsn = $connOrDsn;
9895
} else {
99-
throw new InvalidArgumentException(sprintf('"%s" requires PDO or Doctrine\DBAL\Connection instance or DSN string as first argument, "%s" given.', __CLASS__, get_debug_type($connOrDsn)));
96+
$this->dsn = $connOrDsn;
10097
}
10198

10299
$this->table = $options['db_table'] ?? $this->table;

Store/PostgreSqlStore.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,13 @@ class PostgreSqlStore implements BlockingSharedLockStoreInterface, BlockingStore
4545
* * db_password: The password when lazy-connect [default: '']
4646
* * db_connection_options: An array of driver-specific connection options [default: []]
4747
*
48-
* @param \PDO|Connection|string $connOrDsn A \PDO or Connection instance or DSN string or null
49-
* @param array $options An associative array of options
48+
* @param array $options An associative array of options
5049
*
5150
* @throws InvalidArgumentException When first argument is not PDO nor Connection nor string
5251
* @throws InvalidArgumentException When PDO error mode is not PDO::ERRMODE_EXCEPTION
5352
* @throws InvalidArgumentException When namespace contains invalid characters
5453
*/
55-
public function __construct($connOrDsn, array $options = [])
54+
public function __construct(\PDO|Connection|string $connOrDsn, array $options = [])
5655
{
5756
if ($connOrDsn instanceof \PDO) {
5857
if (\PDO::ERRMODE_EXCEPTION !== $connOrDsn->getAttribute(\PDO::ATTR_ERRMODE)) {
@@ -64,10 +63,8 @@ public function __construct($connOrDsn, array $options = [])
6463
} elseif ($connOrDsn instanceof Connection) {
6564
$this->conn = $connOrDsn;
6665
$this->checkDriver();
67-
} elseif (\is_string($connOrDsn)) {
68-
$this->dsn = $connOrDsn;
6966
} else {
70-
throw new InvalidArgumentException(sprintf('"%s" requires PDO or Doctrine\DBAL\Connection instance or DSN string as first argument, "%s" given.', __CLASS__, get_debug_type($connOrDsn)));
67+
$this->dsn = $connOrDsn;
7168
}
7269

7370
$this->username = $options['db_username'] ?? $this->username;
@@ -235,10 +232,7 @@ private function unlockShared(Key $key): void
235232
}
236233
}
237234

238-
/**
239-
* @return \PDO|Connection
240-
*/
241-
private function getConnection(): object
235+
private function getConnection(): \PDO|Connection
242236
{
243237
if (null === $this->conn) {
244238
if (strpos($this->dsn, '://')) {

Store/RedisStore.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Predis\Response\ServerException;
1515
use Symfony\Component\Cache\Traits\RedisClusterProxy;
1616
use Symfony\Component\Cache\Traits\RedisProxy;
17-
use Symfony\Component\Lock\Exception\InvalidArgumentException;
1817
use Symfony\Component\Lock\Exception\InvalidTtlException;
1918
use Symfony\Component\Lock\Exception\LockConflictedException;
2019
use Symfony\Component\Lock\Exception\LockStorageException;
@@ -37,15 +36,10 @@ class RedisStore implements SharedLockStoreInterface
3736
private $supportTime;
3837

3938
/**
40-
* @param \Redis|\RedisArray|\RedisCluster|RedisProxy|RedisClusterProxy|\Predis\ClientInterface $redisClient
41-
* @param float $initialTtl the expiration delay of locks in seconds
39+
* @param float $initialTtl the expiration delay of locks in seconds
4240
*/
43-
public function __construct($redisClient, float $initialTtl = 300.0)
41+
public function __construct(\Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|RedisProxy|RedisClusterProxy $redisClient, float $initialTtl = 300.0)
4442
{
45-
if (!$redisClient instanceof \Redis && !$redisClient instanceof \RedisArray && !$redisClient instanceof \RedisCluster && !$redisClient instanceof \Predis\ClientInterface && !$redisClient instanceof RedisProxy && !$redisClient instanceof RedisClusterProxy) {
46-
throw new InvalidArgumentException(sprintf('"%s()" expects parameter 1 to be Redis, RedisArray, RedisCluster, RedisProxy, RedisClusterProxy or Predis\ClientInterface, "%s" given.', __METHOD__, get_debug_type($redisClient)));
47-
}
48-
4943
if ($initialTtl <= 0) {
5044
throw new InvalidTtlException(sprintf('"%s()" expects a strictly positive TTL. Got %d.', __METHOD__, $initialTtl));
5145
}
@@ -292,8 +286,6 @@ private function evaluate(string $script, string $resource, array $args)
292286
throw new LockStorageException($e->getMessage(), $e->getCode(), $e);
293287
}
294288
}
295-
296-
throw new InvalidArgumentException(sprintf('"%s()" expects being initialized with a Redis, RedisArray, RedisCluster or Predis\ClientInterface, "%s" given.', __METHOD__, get_debug_type($this->redis)));
297289
}
298290

299291
private function getUniqueToken(Key $key): string

Store/StoreFactory.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,10 @@
2626
class StoreFactory
2727
{
2828
/**
29-
* @param \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|RedisProxy|RedisClusterProxy|\Memcached|\MongoDB\Collection|\PDO|Connection|\Zookeeper|string $connection Connection or DSN or Store short name
30-
*
3129
* @return PersistingStoreInterface
3230
*/
33-
public static function createStore($connection)
31+
public static function createStore(\Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface|RedisProxy|RedisClusterProxy|\Memcached|\MongoDB\Collection|\PDO|Connection|\Zookeeper|string $connection)
3432
{
35-
if (!\is_string($connection) && !\is_object($connection)) {
36-
throw new \TypeError(sprintf('Argument 1 passed to "%s()" must be a string or a connection object, "%s" given.', __METHOD__, get_debug_type($connection)));
37-
}
38-
3933
switch (true) {
4034
case $connection instanceof \Redis:
4135
case $connection instanceof \RedisArray:

0 commit comments

Comments
 (0)