Commit 7a302aab authored by Daniel Hürtgen's avatar Daniel Hürtgen

[FEATURE] LockBuilder::buildPhpRedisLock() implemented

parent 0dee446b
......@@ -35,7 +35,12 @@ lint:cgl:
.test: &test
stage: test
services:
- redis:latest
before_script:
- apk add --no-cache --virtual .build-deps autoconf g++ make
- pecl install -f redis
- docker-php-ext-enable redis
- wget https://composer.github.io/installer.sig -O - -q | tr -d '\n' > installer.sig
- php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
- php -r "if (hash_file('SHA384', 'composer-setup.php') === file_get_contents('installer.sig')) { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
......
......@@ -41,6 +41,52 @@ class LockBuilder implements SingletonInterface
return $lock;
}
/**
* @param array $configuration
*
* @return Lock\PhpRedisLock
* @throws InvalidConfigurationException
* @throws LockCreateException
*/
public function buildPhpRedisLock(array $configuration)
{
if (! extension_loaded('redis')) {
throw new LockCreateException('PHP extension "redis" not loaded', 1510321193);
}
$host = isset($configuration['host']) ? (string)$configuration['host'] : null;
if (empty($host)) {
throw new InvalidConfigurationException($configuration, 'Missing or empty redis host', 1510321408);
}
$port = isset($configuration['port']) ? (int)$configuration['port'] : 6379;
$timeout = isset($configuration['timeout']) ? (float)$configuration['timeout'] : 0.0;
$password = isset($configuration['password']) ? (string)$configuration['password'] : null;
$database = isset($configuration['database']) ? (int)$configuration['database'] : 0;
/** @var \Redis $redis */
$errorReporting = error_reporting();
error_reporting(0);
$redis = GeneralUtility::makeInstance(\Redis::class);
if (! $redis->connect($host, $port, $timeout)) {
throw new LockCreateException(
sprintf('Could not connect to redis host "%s" on port %d', $host, $port),
1510321516
);
};
if (! empty($password)) {
if (! $redis->auth($password)) {
throw new LockCreateException('Authentication with redis host failed', 1510321753);
};
}
if (! $redis->select($database)) {
throw new LockCreateException('Switch redis database to %d failed', 1510321791);
};
error_reporting($errorReporting);
$lock = GeneralUtility::makeInstance(Lock\PhpRedisLock::class, $redis);
return $lock;
}
/**
* @param string $className
* @param array $configuration
......
......@@ -127,4 +127,82 @@ class LockBuilderTest extends UnitTestCase
$builder->buildFlockLock([]);
}
/**
* @test
*/
public function itBuildsAPhpRedisLock()
{
$configuration = [
'host' => 'redis',
];
$builder = new LockBuilder();
$lock = $builder->buildPhpRedisLock($configuration);
$this->assertInstanceOf(Lock\PhpRedisLock::class, $lock);
}
/**
* @test
* @expectedException \Higidi\Lock\Builder\Exception\InvalidConfigurationException
* @expectedExceptionCode 1510321408
*/
public function itThrowsAInvalidConfigurationExceptionOnBuildAPhpRedisLockWithMissingHostConfiguration()
{
$builder = new LockBuilder();
$builder->buildPhpRedisLock([]);
}
/**
* @test
* @expectedException \Higidi\Lock\Builder\Exception\LockCreateException
* @expectedExceptionCode 1510321516
*/
public function itThrowsALockCreateExceptionOnBuildPhpRedisLockIfCanNotConnectToRedis()
{
$configuration = [
'host' => '1.2.3.4',
'port' => 1,
];
$builder = new LockBuilder();
$builder->buildPhpRedisLock($configuration);
}
/**
* @test
* @expectedException \Higidi\Lock\Builder\Exception\LockCreateException
* @expectedExceptionCode 1510321753
*/
public function itThrowsALockCreateExceptionOnBuildPhpRedisLockIfCanNotAuthWithRedis()
{
$configuration = [
'host' => 'redis',
'password' => 'invalidPassword',
];
$builder = new LockBuilder();
$builder->buildPhpRedisLock($configuration);
}
/**
* @test
* @expectedException \Higidi\Lock\Builder\Exception\LockCreateException
* @expectedExceptionCode 1510321791
*/
public function itThrowsALockCreateExceptionOnBuildPhpRedisLockIfCanNotSelectRedisDatabase()
{
$configuration = [
'host' => 'redis',
'database' => -1,
];
$builder = new LockBuilder();
$builder->buildPhpRedisLock($configuration);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment