Commit b00b56cf authored by Daniel Hürtgen's avatar Daniel Hürtgen

[FEATURE] LockImplemenationConfiguration added to Configuration

parent 90a381b5
......@@ -37,6 +37,11 @@ class Configuration implements SingletonInterface
*/
protected $lockImplementation;
/**
* @var array
*/
protected $lockImplementationConfiguration = [];
/**
* @param array|null $configuration
*/
......@@ -166,7 +171,7 @@ class Configuration implements SingletonInterface
*/
protected function setLockImplementation($lockImplementation)
{
if (! is_a($lockImplementation, LockInterface::class, true)) {
if (! $this->isValidLockImplementation($lockImplementation)) {
throw new InvalidLockImplementationException(
sprintf('%s only accepts classes extending the %s class', __METHOD__, LockInterface::class),
1510268834
......@@ -176,4 +181,54 @@ class Configuration implements SingletonInterface
return $this;
}
/**
* @param null|string $lockImplementation
*
* @return array
*/
public function getLockImplementationConfiguration($lockImplementation = null)
{
if (empty($lockImplementation)) {
return $this->lockImplementationConfiguration;
}
$lockImplementationConfiguration = isset($this->lockImplementationConfiguration[$lockImplementation])
? $this->lockImplementationConfiguration[$lockImplementation]
: [];
return $lockImplementationConfiguration;
}
/**
* @param array $configuration
*
* @return $this
* @throws InvalidLockImplementationException
*/
protected function setLockImplementationConfiguration($configuration)
{
if (! is_array($configuration)) {
$configuration = (array)$configuration;
}
foreach ($configuration as $lockImplementation => $lockImplementationConfiguration) {
if (! $this->isValidLockImplementation($lockImplementation)) {
throw new InvalidLockImplementationException('', 1510436776);
}
$this->lockImplementationConfiguration[$lockImplementation] = (array)$lockImplementationConfiguration;
}
return $this;
}
/**
* @param string $className
*
* @return bool
*/
protected function isValidLockImplementation($className)
{
return is_a($className, LockInterface::class, true);
}
}
......@@ -195,6 +195,44 @@ class ConfigurationTest extends UnitTestCase
$this->assertSame($className, $sut->getLockImplementation());
}
/**
* @test
*/
public function itIsPossibleToSetLockImplementationConfigurationViaGlobalsConfigurationArray()
{
$lockImplementation = $this->prophesize(LockInterface::class)->reveal();
$className = get_class($lockImplementation);
$lockImplementationConfiguration = [
$className => [],
];
$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking'] = [
'lockImplementationConfiguration' => $lockImplementationConfiguration,
];
$sut = new Configuration();
$this->assertSame($lockImplementationConfiguration, $sut->getLockImplementationConfiguration());
}
/**
* @test
*/
public function itIsPossibleToSetLockImplementationConfigurationViaConfigurationArray()
{
$lockImplementation = $this->prophesize(LockInterface::class)->reveal();
$className = get_class($lockImplementation);
$lockImplementationConfiguration = [
$className => [],
];
$configuration = [
'lockImplementationConfiguration' => $lockImplementationConfiguration,
];
$sut = new Configuration($configuration);
$this->assertSame($lockImplementationConfiguration, $sut->getLockImplementationConfiguration());
}
/**
* @test
*/
......@@ -295,6 +333,46 @@ class ConfigurationTest extends UnitTestCase
$this->assertNull($sut->getLockImplementation());
}
/**
* @test
*/
public function itHasAnArrayAsDefaultLockImplemenationConfiguration()
{
$sut = new Configuration();
$this->assertSame([], $sut->getLockImplementationConfiguration());
}
/**
* @test
*/
public function itReturnsALockImplemenationConfigurationByLockImplemenation()
{
$lockImplementation = $this->prophesize(LockInterface::class)->reveal();
$className = get_class($lockImplementation);
$config = ['bla' => 'foo'];
$lockImplementationConfiguration = [
$className => $config,
];
$configuration = [
'lockImplementationConfiguration' => $lockImplementationConfiguration,
];
$sut = new Configuration($configuration);
$this->assertSame($config, $sut->getLockImplementationConfiguration($className));
}
/**
* @test
*/
public function itReturnsPerDefaultAnArrayAsLockImplemenationConfigurationByLockImplemenationIfNotExists()
{
$sut = new Configuration();
$this->assertSame([], $sut->getLockImplementationConfiguration(\stdClass::class));
}
/**
* @test
* @expectedException \Higidi\Lock\Configuration\Exception\InvalidLockImplementationException
......@@ -308,4 +386,20 @@ class ConfigurationTest extends UnitTestCase
new Configuration($configuration);
}
/**
* @test
* @expectedException \Higidi\Lock\Configuration\Exception\InvalidLockImplementationException
* @expectedExceptionCode 1510436776
*/
public function itThrowsAnInvalidLockImplemenationExceptionIfLockImplemenationForConfigurationIsNotValid()
{
$configuration = [
'lockImplementationConfiguration' => [
\stdClass::class => [],
],
];
new Configuration($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