Commit 947ac4f0 authored by Daniel Hürtgen's avatar Daniel Hürtgen

[FEATURE] LockImplemenationBuilder added to Configuration

parent b00b56cf
......@@ -5,6 +5,7 @@ namespace Higidi\Lock\Configuration;
use Higidi\Lock\Configuration\Exception\InvalidLockImplementationException;
use Higidi\Lock\Configuration\Exception\InvalidMutexException;
use Higidi\Lock\Configuration\Exception\InvalidStrategyException;
use Higidi\Lock\Configuration\Exception\NotCallableLockImplemenationBuilderException;
use Higidi\Lock\Strategy\MutexAdapterStrategy;
use NinjaMutex\Lock\LockInterface;
use NinjaMutex\Mutex;
......@@ -37,6 +38,11 @@ class Configuration implements SingletonInterface
*/
protected $lockImplementation;
/**
* @var array
*/
protected $lockImplementationBuilder = [];
/**
* @var array
*/
......@@ -182,6 +188,53 @@ class Configuration implements SingletonInterface
return $this;
}
/**
* @param null|string $lockImplemenation
*
* @return array|callable|null
*/
public function getLockImplementationBuilder($lockImplemenation = null)
{
if (empty($lockImplemenation)) {
return $this->lockImplementationBuilder;
}
$lockImplemenationBuilder = isset($this->lockImplementationBuilder[$lockImplemenation])
? $this->lockImplementationBuilder[$lockImplemenation]
: null;
return $lockImplemenationBuilder;
}
/**
* @param array $builder
*
* @return $this
* @throws InvalidLockImplementationException
* @throws NotCallableLockImplemenationBuilderException
*/
protected function setLockImplementationBuilder($builder)
{
if (! is_array($builder)) {
$builder = (array)$builder;
}
foreach ($builder as $lockImplemenation => $lockImplemenationBuilder) {
if (! $this->isValidLockImplementation($lockImplemenation)) {
throw new InvalidLockImplementationException('', 1510436775);
}
if (! is_callable($lockImplemenationBuilder)) {
throw new NotCallableLockImplemenationBuilderException(
'Lock implemenation builder needs to be callable',
1510438594
);
}
$this->lockImplementationBuilder[$lockImplemenation] = $lockImplemenationBuilder;
}
return $this;
}
/**
* @param null|string $lockImplementation
*
......
......@@ -195,6 +195,48 @@ class ConfigurationTest extends UnitTestCase
$this->assertSame($className, $sut->getLockImplementation());
}
/**
* @test
*/
public function itIsPossibleToSetLockImplementationBuilderViaGlobalsConfigurationArray()
{
$lockImplementation = $this->prophesize(LockInterface::class)->reveal();
$className = get_class($lockImplementation);
$lockImplementationBuilder = [
$className => function (array $configuration) {
return $configuration;
},
];
$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking'] = [
'lockImplementationBuilder' => $lockImplementationBuilder,
];
$sut = new Configuration();
$this->assertSame($lockImplementationBuilder, $sut->getLockImplementationBuilder());
}
/**
* @test
*/
public function itIsPossibleToSetLockImplementationBuilderViaConfigurationArray()
{
$lockImplementation = $this->prophesize(LockInterface::class)->reveal();
$className = get_class($lockImplementation);
$lockImplementationBuilder = [
$className => function (array $configuration) {
return $configuration;
},
];
$configuration = [
'lockImplementationBuilder' => $lockImplementationBuilder,
];
$sut = new Configuration($configuration);
$this->assertSame($lockImplementationBuilder, $sut->getLockImplementationBuilder());
}
/**
* @test
*/
......@@ -333,6 +375,48 @@ class ConfigurationTest extends UnitTestCase
$this->assertNull($sut->getLockImplementation());
}
/**
* @test
*/
public function itHasAnArrayAsDefaultLockImplemenationBuilder()
{
$sut = new Configuration();
$this->assertSame([], $sut->getLockImplementationBuilder());
}
/**
* @test
*/
public function itReturnsALockImplemenationBuilderByLockImplemenation()
{
$lockImplementation = $this->prophesize(LockInterface::class)->reveal();
$className = get_class($lockImplementation);
$callable = function (array $configuration) {
return $configuration;
};
$lockImplementationBuilder = [
$className => $callable,
];
$configuration = [
'lockImplementationBuilder' => $lockImplementationBuilder,
];
$sut = new Configuration($configuration);
$this->assertSame($callable, $sut->getLockImplementationBuilder($className));
}
/**
* @test
*/
public function itReturnsPerDefaultNullAsLockImplemenationBuilderByLockImplemenationIfNotExists()
{
$sut = new Configuration();
$this->assertNull($sut->getLockImplementationBuilder(\stdClass::class));
}
/**
* @test
*/
......@@ -387,6 +471,42 @@ class ConfigurationTest extends UnitTestCase
new Configuration($configuration);
}
/**
* @test
* @expectedException \Higidi\Lock\Configuration\Exception\InvalidLockImplementationException
* @expectedExceptionCode 1510436775
*/
public function itThrowsAnInvalidLockImplemenationExceptionIfLockImplemenationForBuilderIsNotValid()
{
$configuration = [
'lockImplementationBuilder' => [
\stdClass::class => function (array $configuration) {
return $configuration;
},
],
];
new Configuration($configuration);
}
/**
* @test
* @expectedException \Higidi\Lock\Configuration\Exception\NotCallableLockImplemenationBuilderException
* @expectedExceptionCode 1510438594
*/
public function itThrowsANotCallableLockImplementationBuilderExceptionIfBuilderIsNotCallable()
{
$lockImplementation = $this->prophesize(LockInterface::class)->reveal();
$className = get_class($lockImplementation);
$configuration = [
'lockImplementationBuilder' => [
$className => 'not_callable',
],
];
new Configuration($configuration);
}
/**
* @test
* @expectedException \Higidi\Lock\Configuration\Exception\InvalidLockImplementationException
......
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