Add tested booker class

This commit is contained in:
Benoît Burnichon
2016-02-05 14:10:43 +01:00
parent 09ce98e21a
commit a539919cf8
4 changed files with 146 additions and 55 deletions

View File

@@ -24,21 +24,22 @@ class LazaretFilesystemService
*/
private $filesystem;
/**
* @var string
*/
private $tmpPath;
/**
* @var Alchemyst
*/
private $alchemyst;
/**
* @var LazaretPathBooker
*/
private $booker;
public function __construct(Filesystem $filesystem, $tmpPath, Alchemyst $alchemyst)
{
$this->filesystem = $filesystem;
$this->tmpPath = $tmpPath;
$this->alchemyst = $alchemyst;
$this->booker = new LazaretPathBooker($filesystem, $tmpPath);
}
/**
@@ -48,10 +49,10 @@ class LazaretFilesystemService
*/
public function writeLazaret(File $file)
{
$lazaretPathname = $this->bookLazaretPathfile($file->getOriginalName());
$lazaretPathname = $this->booker->bookFile($file->getOriginalName());
$this->filesystem->copy($file->getFile()->getRealPath(), $lazaretPathname, true);
$lazaretPathnameThumb = $this->bookLazaretPathfile($file->getOriginalName(), 'thumb');
$lazaretPathnameThumb = $this->booker->bookFile($file->getOriginalName(), 'thumb');
try {
$this->alchemyst->turnInto($file->getFile()->getPathname(), $lazaretPathnameThumb, $this->createThumbnailSpecification());
} catch (ExceptionInterface $e) {
@@ -61,28 +62,6 @@ class LazaretFilesystemService
return new PersistedLazaretInformation($lazaretPathname, $lazaretPathnameThumb);
}
private function bookLazaretPathfile($filename, $suffix = '')
{
$output = $this->tmpPath .'/lzrt_' . substr($filename, 0, 3) . '_' . $suffix . '.' . pathinfo($filename, PATHINFO_EXTENSION);
$infos = pathinfo($output);
$n = 0;
while (true) {
$output = sprintf('%s/%s-%d%s', $infos['dirname'], $infos['filename'], ++ $n, (isset($infos['extension']) ? '.' . $infos['extension'] : ''));
try {
if (! $this->filesystem->exists($output)) {
$this->filesystem->touch($output);
break;
}
} catch (IOException $e) {
}
}
return realpath($output);
}
/**
* @return ImageSpecification
*/

View File

@@ -0,0 +1,87 @@
<?php
/**
* This file is part of Phraseanet
*
* (c) 2005-2016 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Filesystem;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
class LazaretPathBooker
{
/**
* @var Filesystem
*/
private $filesystem;
/**
* @var string
*/
private $tmpPath;
/**
* @var callable
*/
private $pathResolver;
/**
* @param Filesystem $filesystem
* @param string $tmpPath
* @param callable|string $pathResolver
*/
public function __construct(Filesystem $filesystem, $tmpPath, $pathResolver = 'realpath')
{
$this->filesystem = $filesystem;
$this->tmpPath = $tmpPath;
if (!is_callable($pathResolver)) {
throw new \LogicException('pathResolver should be callable');
}
$this->pathResolver = $pathResolver;
}
/**
* @param string $filename
* @param string $suffix
* @return string
*/
public function bookFile($filename, $suffix = '')
{
$output = $this->tmpPath .'/lzrt_' . substr($filename, 0, 3) . '_' . $suffix . '.' . pathinfo($filename, PATHINFO_EXTENSION);
$infos = pathinfo($output);
$n = 0;
while (true) {
$output = sprintf('%s/%s-%d%s', $infos['dirname'], $infos['filename'], ++ $n, (isset($infos['extension']) ? '.' . $infos['extension'] : ''));
try {
if (! $this->filesystem->exists($output)) {
$this->filesystem->touch($output);
break;
}
} catch (IOException $e) {
}
}
return $this->resolvePath($output);
}
/**
* @param string $path
* @return string
*/
private function resolvePath($path)
{
$callable = $this->pathResolver;
return $callable($path);
}
}

View File

@@ -639,35 +639,10 @@ class ManagerTest extends \PhraseanetAuthenticatedWebTestCase
$this->object->unregisterChecker($uuidChecker);
$this->assertEquals([$shaChecker, $filenameChecker], $this->object->getCheckers());
}
/**
* @covers Alchemy\Phrasea\Border\Manager::bookLazaretPathfile
*/
public function testBookLazaretPathfile()
{
$manager = new ManagerTester(self::$DI['app']);
$file1 = $manager->bookLazaretPathfileTester('babebibobu.txt');
$file2 = $manager->bookLazaretPathfileTester('babebibobu.txt');
$this->assertNotEquals($file2, $file1);
$this->assertTrue(file_exists($file1));
$this->assertTrue(file_exists($file2));
unlink($file1);
unlink($file2);
}
}
class ManagerTester extends Manager
{
public function bookLazaretPathfileTester($filename)
{
return parent::bookLazaretPathfile($filename);
}
public function addMediaAttributesTester($file)
{
return parent::addMediaAttributes($file);

View File

@@ -0,0 +1,50 @@
<?php
/**
* This file is part of Phraseanet
*
* (c) 2005-2016 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Tests\Phrasea\Filesystem;
use Alchemy\Phrasea\Filesystem\LazaretPathBooker;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use Symfony\Component\Filesystem\Filesystem;
class LazaretPathBookerTest extends \PHPUnit_Framework_TestCase
{
/**
* @var vfsStreamDirectory
*/
private $root;
protected function setUp()
{
$this->root = vfsStream::setup();
}
/**
* @covers Alchemy\Phrasea\Border\Manager::bookLazaretPathfile
*/
public function testBookFile()
{
$mockedPathResolver = function ($path) {
return $path;
};
$sut = new LazaretPathBooker(new Filesystem(), $this->root->url(), $mockedPathResolver);
$file1 = $sut->bookFile('babebibobu.txt');
$file2 = $sut->bookFile('babebibobu.txt');
$this->assertNotEquals($file2, $file1);
$this->assertCount(2, $this->root->getChildren());
$this->assertTrue($this->root->hasChild(vfsStream::path($file1)));
$this->assertTrue($this->root->hasChild(vfsStream::path($file2)));
}
}