Extract business logic from DB connection

Signed-off-by: Christian Wolf <github@christianwolf.email>
This commit is contained in:
Christian Wolf 2022-07-06 13:49:59 +02:00
Родитель 6c6f1465a0
Коммит 2d4841e032
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 9FC3120E932F73F1
4 изменённых файлов: 115 добавлений и 8 удалений

Просмотреть файл

@ -473,9 +473,6 @@ class RecipeDb {
$qb->setParameter('name', $recipe['name'], $this->types->STRING());
$dateCreated = $this->parseDate($recipe['dateCreated']);
if ($dateCreated === null) {
$dateCreated = $this->parseDate('now');
}
$qb->setParameter('dateCreated', $dateCreated, $this->types->DATE());
$dateModified = $this->parseDate($recipe['dateModified']);
@ -497,8 +494,8 @@ class RecipeDb {
$qb->set('name', $qb->createNamedParameter($recipe['name'], IQueryBuilder::PARAM_STR));
$dateCreated = $this->parseDate($recipe['dateCreated'] ?? 'now');
$dateModified = $this->parseDate($recipe['dateModified']) ?? $dateCreated;
$dateCreated = $this->parseDate($recipe['dateCreated']);
$dateModified = $this->parseDate($recipe['dateModified']);
$qb->set('dateCreated', $qb->createNamedParameter($dateCreated, IQueryBuilder::PARAM_DATE));
$qb->set('dateModified', $qb->createNamedParameter($dateModified, IQueryBuilder::PARAM_DATE));

Просмотреть файл

@ -0,0 +1,45 @@
<?php
namespace OCA\Cookbook\Helper\Filter;
use OCA\Cookbook\Helper\Filter\DB\RecipeDatesFilter;
use OCP\Files\File;
class NormalizeRecipeFileFilter {
/** @var array */
private $filters;
public function __construct(
RecipeDatesFilter $datesFilter
) {
$this->filters = [
$datesFilter,
];
}
/**
* Normalize a recipe file according to the installed filters
*
* If the recipe needs updating, the file gets overwritten in the storage.
*
* @param array $json The content of the recipe
* @param File $recipeFile The file containing the recipe
* @param bool $updateFiles true, if the file on storage should be updated with the modified version
* @return array The corrected recipe object
*/
public function filter(array $json, File $recipeFile, bool $updateFiles = false): array {
$changed = false;
foreach ($this->filters as $filter) {
/** @var AbstractRecipeFilter $filter */
$changed |= $filter->apply($json, $recipeFile);
}
if ($changed && $updateFiles) {
$recipeFile->putContent(json_encode($json));
$recipeFile->touch();
}
return $json;
}
}

Просмотреть файл

@ -6,6 +6,7 @@ use OCA\Cookbook\Db\RecipeDb;
use OCP\Files\File;
use OCP\AppFramework\Db\DoesNotExistException;
use OCA\Cookbook\Exception\InvalidJSONFileException;
use OCA\Cookbook\Helper\Filter\NormalizeRecipeFileFilter;
use OCA\Cookbook\Helper\UserConfigHelper;
use OCP\IL10N;
@ -33,6 +34,9 @@ class DbCacheService {
*/
private $l;
/** @var NormalizeRecipeFileFilter */
private $normalizeFileFilter;
private $jsonFiles;
private $dbReceipeFiles;
private $dbKeywords;
@ -47,12 +51,14 @@ class DbCacheService {
RecipeDb $db,
RecipeService $recipeService,
UserConfigHelper $userConfigHelper,
NormalizeRecipeFileFilter $normalizeRecipeFileFilter,
IL10N $l
) {
$this->userId = $UserId;
$this->db = $db;
$this->recipeService = $recipeService;
$this->userConfigHelper = $userConfigHelper;
$this->normalizeFileFilter = $normalizeRecipeFileFilter;
$this->l = $l;
}
@ -142,9 +148,7 @@ class DbCacheService {
$id = (int) $jsonFile->getParent()->getId();
$json['id'] = $id;
if (!isset($json['dateModified'])) {
$json['dateModified'] = null;
}
$json = $this->normalizeFileFilter->filter($json, $jsonFile);
return $json;
}

Просмотреть файл

@ -0,0 +1,61 @@
<?php
namespace OCA\Cookbook\tests\Unit\Helper\Filter;
use OCP\Files\File;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject;
use OCA\Cookbook\Helper\Filter\DB\RecipeDatesFilter;
use OCA\Cookbook\Helper\Filter\NormalizeRecipeFileFilter;
/**
* @covers OCA\Cookbook\Helper\Filter\NormalizeRecipeFileFilter
*/
class NormalizeRecipeFileFilterTest extends TestCase {
/** @var MockObject|RecipeDatesFilter */
private $datesFilter;
/** @var NormalizeRecipeFileFilter */
private $dut;
protected function setUp(): void {
$this->datesFilter = $this->createMock(RecipeDatesFilter::class);
$this->dut = new NormalizeRecipeFileFilter($this->datesFilter);
}
public function dp() {
yield [false, false, false];
yield [false, true, false];
yield [false, false, false];
yield [true, true, true];
}
/** @dataProvider dp */
public function testFilter($updateRequested, $changedDates, $shouldWrite) {
$recipe = ['name' => 'The recipe'];
$recipeA = $recipe;
$recipeA['version'] = 'from RecipeDatesFilter';
/** @var MockObject|File $recipeFile */
$recipeFile = $this->createMock(File::class);
if ($shouldWrite) {
$recipeFile->expects($this->once())->method('putContent')->with(json_encode($recipeA));
$recipeFile->expects($this->once())->method('touch');
} else {
$recipeFile->expects($this->never())->method('putContent');
$recipeFile->expects($this->never())->method('touch');
}
$this->datesFilter->method('apply')->with($recipe, $recipeFile)
->willReturnCallback(function (&$json, $file) use ($changedDates, $recipeA) {
$json = $recipeA;
return $changedDates;
});
$ret = $this->dut->filter($recipe, $recipeFile, $updateRequested);
$this->assertEquals($recipeA, $ret);
}
}