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

[FEATURE] LockBuilder::buildDirectoryLock() implemented

parent f309cbc4
......@@ -2,11 +2,53 @@
namespace Higidi\Lock\Builder;
use Higidi\Lock\Builder\Exception\InvalidConfigurationException;
use Higidi\Lock\Builder\Exception\LockCreateException;
use NinjaMutex\Lock;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Lock implementation builder class.
*/
class LockBuilder implements SingletonInterface
{
/**
* @param array $configuration
*
* @return Lock\DirectoryLock
* @throws InvalidConfigurationException
* @throws LockCreateException
*/
public function buildDirectoryLock(array $configuration)
{
$path = isset($configuration['path']) ? (string)$configuration['path'] : null;
if (empty($path)) {
throw new InvalidConfigurationException(
$configuration,
'Missing or empty lock directory path',
1510318044
);
}
if (! $this->preparePath($path)) {
throw new LockCreateException(sprintf('Path %s is not usable (not readable/writeable)', $path), 1510318759);
}
$lock = GeneralUtility::makeInstance(Lock\DirectoryLock::class, $path);
return $lock;
}
/**
* @param string $path
*
* @return bool
*/
protected function preparePath($path)
{
if (null === $path || ! is_dir($path) && ! GeneralUtility::mkdir($path)) {
return false;
}
return true;
}
}
......@@ -5,6 +5,8 @@ namespace Higidi\Lock\Tests\Unit;
use Higidi\Lock\Builder\LockBuilder;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Tests\UnitTestCase;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use NinjaMutex\Lock;
/**
* Test case for "\Higidi\Lock\LockBuilder".
......@@ -13,6 +15,53 @@ use TYPO3\CMS\Core\Tests\UnitTestCase;
*/
class LockBuilderTest extends UnitTestCase
{
/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
parent::setUp();
$this->clearLockPath();
}
/**
* Unset all additional properties of test classes to help PHP
* garbage collection. This reduces memory footprint with lots
* of tests.
*
* If owerwriting tearDown() in test classes, please call
* parent::tearDown() at the end. Unsetting of own properties
* is not needed this way.
*
* @throws \RuntimeException
* @return void
*/
protected function tearDown()
{
parent::tearDown();
$this->clearLockPath();
}
/**
* @return void
*/
protected function clearLockPath()
{
$path = $this->getLockPath();
if (is_dir($path)) {
GeneralUtility::rmdir($path, true);
}
}
/**
* @return string
*/
protected function getLockPath()
{
return PATH_site . '/typo3temp/locks/';
}
/**
* @test
*/
......@@ -22,4 +71,32 @@ class LockBuilderTest extends UnitTestCase
$this->assertInstanceOf(SingletonInterface::class, $sut);
}
/**
* @test
*/
public function itBuildsADirectoryLock()
{
$configurtion = [
'path' => $this->getLockPath(),
];
$builder = new LockBuilder();
$lock = $builder->buildDirectoryLock($configurtion);
$this->assertInstanceOf(Lock\DirectoryLock::class, $lock);
}
/**
* @test
* @expectedException \Higidi\Lock\Builder\Exception\InvalidConfigurationException
* @expectedExceptionCode 1510318044
*/
public function itThrowsAnInvalidConfigurationExceptionOnBuildADirectoryLockWithMissingPathConfiguration()
{
$builder = new LockBuilder();
$builder->buildDirectoryLock([]);
}
}
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