Moved tag export to service

This commit is contained in:
Marcin Kurczewski 2014-10-07 23:03:06 +02:00
parent 25f1e5b83d
commit 76d9e95e4b
9 changed files with 68 additions and 65 deletions

View file

@ -230,8 +230,6 @@ class PostDao extends AbstractDao implements ICrudDao
{ {
$this->fpdo->deleteFrom('postTags')->where('postId', $post->getId())->where('tagId', $tagId)->execute(); $this->fpdo->deleteFrom('postTags')->where('postId', $post->getId())->where('tagId', $tagId)->execute();
} }
$this->tagDao->exportJson();
} }
private function syncPostRelations(\Szurubooru\Entities\Post $post) private function syncPostRelations(\Szurubooru\Entities\Post $post)

View file

@ -3,18 +3,13 @@ namespace Szurubooru\Dao;
class TagDao extends AbstractDao implements ICrudDao class TagDao extends AbstractDao implements ICrudDao
{ {
private $fileService;
public function __construct( public function __construct(
\Szurubooru\DatabaseConnection $databaseConnection, \Szurubooru\DatabaseConnection $databaseConnection)
\Szurubooru\Services\FileService $fileService)
{ {
parent::__construct( parent::__construct(
$databaseConnection, $databaseConnection,
'tags', 'tags',
new \Szurubooru\Dao\EntityConverters\TagEntityConverter()); new \Szurubooru\Dao\EntityConverters\TagEntityConverter());
$this->fileService = $fileService;
} }
public function findByNames($tagNames) public function findByNames($tagNames)
@ -36,21 +31,4 @@ class TagDao extends AbstractDao implements ICrudDao
$arrayEntities = iterator_to_array($query); $arrayEntities = iterator_to_array($query);
return $this->arrayToEntities($arrayEntities); return $this->arrayToEntities($arrayEntities);
} }
public function exportJson()
{
$tags = [];
foreach ($this->findAll() as $tag)
{
$tags[$tag->getName()] = $tag->getUsages();
}
$json = json_encode($tags);
$this->fileService->save('tags.json', $json);
}
protected function afterBatchSave(array $entities)
{
if (count($entities) > 0)
$this->exportJson();
}
} }

View file

@ -271,6 +271,7 @@ class PostService
$tags[] = $tag; $tags[] = $tag;
} }
$tags = $this->tagService->createTags($tags); $tags = $this->tagService->createTags($tags);
$this->tagService->exportJson();
$post->setTags($tags); $post->setTags($tags);
} }

View file

@ -5,16 +5,19 @@ class TagService
{ {
private $transactionManager; private $transactionManager;
private $tagDao; private $tagDao;
private $fileService;
private $timeService; private $timeService;
public function __construct( public function __construct(
\Szurubooru\Dao\TransactionManager $transactionManager, \Szurubooru\Dao\TransactionManager $transactionManager,
\Szurubooru\Dao\TagDao $tagDao, \Szurubooru\Dao\TagDao $tagDao,
\Szurubooru\Services\TimeService $timeService) \Szurubooru\Services\TimeService $timeService,
\Szurubooru\Services\FileService $fileService)
{ {
$this->transactionManager = $transactionManager; $this->transactionManager = $transactionManager;
$this->tagDao = $tagDao; $this->tagDao = $tagDao;
$this->timeService = $timeService; $this->timeService = $timeService;
$this->fileService = $fileService;
} }
public function getFiltered(\Szurubooru\SearchServices\Filters\TagFilter $filter) public function getFiltered(\Szurubooru\SearchServices\Filters\TagFilter $filter)
@ -26,6 +29,17 @@ class TagService
return $this->transactionManager->rollback($transactionFunc); return $this->transactionManager->rollback($transactionFunc);
} }
public function exportJson()
{
$tags = [];
foreach ($this->tagDao->findAll() as $tag)
{
$tags[$tag->getName()] = $tag->getUsages();
}
$json = json_encode($tags);
$this->fileService->save('tags.json', $json);
}
public function createTags(array $tags) public function createTags(array $tags)
{ {
$transactionFunc = function() use ($tags) $transactionFunc = function() use ($tags)

View file

@ -3,15 +3,15 @@ namespace Szurubooru\Upgrades;
class Upgrade12 implements IUpgrade class Upgrade12 implements IUpgrade
{ {
private $tagDao; private $tagService;
public function __construct(\Szurubooru\Dao\TagDao $tagDao) public function __construct(\Szurubooru\Services\TagService $tagService)
{ {
$this->tagDao = $tagDao; $this->tagService = $tagService;
} }
public function run(\Szurubooru\DatabaseConnection $databaseConnection) public function run(\Szurubooru\DatabaseConnection $databaseConnection)
{ {
$this->tagDao->exportJson(); $this->tagService->exportJson();
} }
} }

View file

@ -13,16 +13,24 @@ abstract class AbstractDatabaseTestCase extends \Szurubooru\Tests\AbstractTestCa
$config->set('database/user', ''); $config->set('database/user', '');
$config->set('database/password', ''); $config->set('database/password', '');
$fileServiceMock = $this->mock(\Szurubooru\Services\FileService::class); $fileService = $this->prepareFileService();
$this->databaseConnection = new \Szurubooru\DatabaseConnection($config); $this->databaseConnection = new \Szurubooru\DatabaseConnection($config);
\Szurubooru\Injector::set(\Szurubooru\DatabaseConnection::class, $this->databaseConnection); \Szurubooru\Injector::set(\Szurubooru\DatabaseConnection::class, $this->databaseConnection);
\Szurubooru\Injector::set(\Szurubooru\Services\FileService::class, $fileServiceMock); \Szurubooru\Injector::set(\Szurubooru\Services\FileService::class, $fileService);
$upgradeRepository = \Szurubooru\Injector::get(\Szurubooru\Upgrades\UpgradeRepository::class); $upgradeRepository = \Szurubooru\Injector::get(\Szurubooru\Upgrades\UpgradeRepository::class);
$upgradeService = new \Szurubooru\Services\UpgradeService($config, $this->databaseConnection, $upgradeRepository); $upgradeService = new \Szurubooru\Services\UpgradeService($config, $this->databaseConnection, $upgradeRepository);
$upgradeService->runUpgradesQuiet(); $upgradeService->runUpgradesQuiet();
} }
private function prepareFileService()
{
$testDirectory = $this->createTestDirectory();
$configMock = $this->mockConfig(null, $testDirectory);
$httpHelper = \Szurubooru\Injector::get(\Szurubooru\Helpers\HttpHelper::class);
return new \Szurubooru\Services\FileService($configMock, $httpHelper);
}
public function tearDown() public function tearDown()
{ {
parent::tearDown(); parent::tearDown();

View file

@ -251,11 +251,10 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
[$post->getThumbnailSourceContentPath()]); [$post->getThumbnailSourceContentPath()]);
$this->fileServiceMock $this->fileServiceMock
->expects($this->exactly(2)) ->expects($this->exactly(1))
->method('save') ->method('save')
->withConsecutive( ->withConsecutive(
[$post->getContentPath(), 'whatever'], [$post->getContentPath(), 'whatever']);
['tags.json', '[]']);
$postDao->save($post); $postDao->save($post);
} }
@ -275,12 +274,11 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
[$post->getThumbnailSourceContentPath()]); [$post->getThumbnailSourceContentPath()]);
$this->fileServiceMock $this->fileServiceMock
->expects($this->exactly(3)) ->expects($this->exactly(2))
->method('save') ->method('save')
->withConsecutive( ->withConsecutive(
[$post->getContentPath(), 'whatever'], [$post->getContentPath(), 'whatever'],
[$post->getThumbnailSourceContentPath(), 'an image of sharks'], [$post->getThumbnailSourceContentPath(), 'an image of sharks']);
['tags.json', '[]']);
$postDao->save($post); $postDao->save($post);
} }

View file

@ -3,12 +3,9 @@ namespace Szurubooru\Tests\Dao;
final class TagDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase final class TagDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
{ {
private $fileServiceMock;
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
$this->fileServiceMock = $this->mock(\Szurubooru\Services\FileService::class);
} }
public function testFindByPostIds() public function testFindByPostIds()
@ -38,31 +35,8 @@ final class TagDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
$this->assertEntitiesEqual($expected, $actual); $this->assertEntitiesEqual($expected, $actual);
} }
public function testExportSingle()
{
$tag1 = new \Szurubooru\Entities\Tag();
$tag1->setName('test');
$tag1->setCreationTime(date('c'));
$this->fileServiceMock->expects($this->once())->method('save')->with('tags.json', '{"test":0}');
$tagDao = $this->getTagDao();
$tagDao->save($tag1);
}
public function testExportMultiple()
{
$tag1 = new \Szurubooru\Entities\Tag();
$tag1->setName('test1');
$tag1->setCreationTime(date('c'));
$tag2 = new \Szurubooru\Entities\Tag();
$tag2->setName('test2');
$tag2->setCreationTime(date('c'));
$this->fileServiceMock->expects($this->once())->method('save')->with('tags.json', '{"test1":0,"test2":0}');
$tagDao = $this->getTagDao();
$tagDao->batchSave([$tag1, $tag2]);
}
private function getTagDao() private function getTagDao()
{ {
return new \Szurubooru\Dao\TagDao($this->databaseConnection, $this->fileServiceMock); return new \Szurubooru\Dao\TagDao($this->databaseConnection);
} }
} }

View file

@ -64,6 +64,38 @@ class TagServiceTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
$this->assertEquals('test3', $result[1]->getName()); $this->assertEquals('test3', $result[1]->getName());
} }
public function testExportSingle()
{
$tag1 = new \Szurubooru\Entities\Tag();
$tag1->setName('test');
$tag1->setCreationTime(date('c'));
$fileService = $this->getFileService();
$tagService = $this->getTagService();
$tagService->createTags([$tag1]);
$tagService->exportJson();
$this->assertEquals('{"test":0}', $fileService->load('tags.json'));
}
public function testExportMultiple()
{
$tag1 = new \Szurubooru\Entities\Tag();
$tag1->setName('test1');
$tag1->setCreationTime(date('c'));
$tag2 = new \Szurubooru\Entities\Tag();
$tag2->setName('test2');
$tag2->setCreationTime(date('c'));
$fileService = $this->getFileService();
$tagService = $this->getTagService();
$tagService->createTags([$tag1, $tag2]);
$tagService->exportJson();
$this->assertEquals('{"test1":0,"test2":0}', $fileService->load('tags.json'));
}
private function getFileService()
{
return \Szurubooru\Injector::get(\Szurubooru\Services\FileService::class);
}
private function getTagService() private function getTagService()
{ {
return \Szurubooru\Injector::get(\Szurubooru\Services\TagService::class); return \Szurubooru\Injector::get(\Szurubooru\Services\TagService::class);