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

[FEATURE] MutexStrategy implemented in LockFactory

parent 947ac4f0
......@@ -4,6 +4,7 @@ namespace Higidi\Lock;
use Higidi\Lock\Builder\LockBuilder;
use Higidi\Lock\Configuration\Configuration;
use NinjaMutex\Lock\LockInterface;
use TYPO3\CMS\Core\Locking\Exception\LockCreateException;
use TYPO3\CMS\Core\Locking\LockFactory as CoreLockFactory;
use TYPO3\CMS\Core\Locking\LockingStrategyInterface;
......@@ -19,6 +20,11 @@ class LockFactory extends CoreLockFactory
*/
protected $configuration;
/**
* @var LockInterface
*/
protected $lockImplementation;
/**
* @param Configuration|null $configuration The configuration to use
*/
......@@ -55,9 +61,63 @@ class LockFactory extends CoreLockFactory
return parent::createLocker($id, $capabilities);
}
$className = $this->configuration->getStrategy();
$locker = GeneralUtility::makeInstance($className, $id);
try {
$strategyClassName = $this->configuration->getStrategy();
if ($this->configuration->isMutexStrategy()) {
$mutexClassName = $this->configuration->getMutex();
$lockImplementation = $this->getLockImplemenation();
$mutex = GeneralUtility::makeInstance($mutexClassName, $id, $lockImplementation);
$locker = GeneralUtility::makeInstance($strategyClassName, $mutex);
} else {
$locker = GeneralUtility::makeInstance($strategyClassName, $id);
}
} catch (\Exception $e) {
if ($e instanceof LockCreateException) {
throw $e;
}
throw new LockCreateException('Could not create locker', 1510432762, $e);
}
return $locker;
}
/**
* @return LockInterface
* @throws LockCreateException
*/
protected function getLockImplemenation()
{
if (! $this->lockImplementation) {
$lockImplementationClassName = $this->configuration->getLockImplementation();
if (! $lockImplementationClassName) {
throw new LockCreateException('No lock implementation configured', 1510439606);
}
$lockImplementationConfiguration = $this->configuration->getLockImplementationConfiguration(
$lockImplementationClassName
);
$lockImplementationBuilder = $this->configuration->getLockImplementationBuilder(
$lockImplementationClassName
);
if (! is_callable($lockImplementationBuilder)) {
throw new LockCreateException(
sprintf('No callable builder found for lock implementation %s', $lockImplementationClassName),
1510432679
);
}
$lockImplementation = call_user_func($lockImplementationBuilder, $lockImplementationConfiguration);
if (! $lockImplementation instanceof $lockImplementationClassName) {
throw new LockCreateException(
sprintf(
'Expected lock implementation instance of %s. Got %s',
$lockImplementationClassName,
is_object($lockImplementation) ? get_class($lockImplementation) : gettype($lockImplementation)
),
1510439540
);
}
$this->lockImplementation = $lockImplementation;
}
return $this->lockImplementation;
}
}
......@@ -3,10 +3,11 @@
namespace Higidi\Lock\Tests\Unit;
use Higidi\Lock\Configuration\Configuration;
use Higidi\Lock\Builder\LockBuilder;
use Higidi\Lock\LockFactory;
use Higidi\Lock\Strategy\MutexAdapterStrategy;
use Nimut\TestingFramework\TestCase\UnitTestCase;
use NinjaMutex\Lock\LockInterface;
use NinjaMutex\Mutex;
use TYPO3\CMS\Core\Locking as CoreLocking;
use TYPO3\CMS\Core\Locking\LockFactory as CoreLockFactory;
......@@ -102,4 +103,46 @@ class LockFactoryTest extends UnitTestCase
$this->assertInstanceOf($strategy, $locker);
}
/**
* @test
*/
public function itCreatesAMutexStrategy()
{
$lockImplemenation = $this->prophesize(LockInterface::class)->reveal();
$configuration = $this->prophesize(Configuration::class);
$configuration
->isActive()
->willReturn(true);
$configuration
->getStrategy()
->willReturn(MutexAdapterStrategy::class);
$configuration
->getMutex()
->willReturn(Mutex::class);
$configuration
->isMutexStrategy()
->willReturn(true);
$configuration
->getLockImplementation()
->willReturn(get_class($lockImplemenation));
$configuration
->getLockImplementationConfiguration(get_class($lockImplemenation))
->willReturn([]);
$configuration
->getLockImplementationBuilder(get_class($lockImplemenation))
->willReturn(
function (array $configuration) use ($lockImplemenation) {
unset($configuration);
return $lockImplemenation;
}
);
$sut = new LockFactory($configuration->reveal());
$locker = $sut->createLocker('blafoo');
$this->assertInstanceOf(MutexAdapterStrategy::class, $locker);
}
}
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