mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-06 23:33:13 +08:00
Created value object to wrap the renaming of a tag
This commit is contained in:
@@ -7,6 +7,7 @@ namespace Shlinkio\Shlink\Core\Exception;
|
||||
use Fig\Http\Message\StatusCodeInterface;
|
||||
use Mezzio\ProblemDetails\Exception\CommonProblemDetailsExceptionTrait;
|
||||
use Mezzio\ProblemDetails\Exception\ProblemDetailsExceptionInterface;
|
||||
use Shlinkio\Shlink\Core\Tag\Model\TagRenaming;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
@@ -17,18 +18,15 @@ class TagConflictException extends RuntimeException implements ProblemDetailsExc
|
||||
private const TITLE = 'Tag conflict';
|
||||
private const TYPE = 'TAG_CONFLICT';
|
||||
|
||||
public static function fromExistingTag(string $oldName, string $newName): self
|
||||
public static function forExistingTag(TagRenaming $renaming): self
|
||||
{
|
||||
$e = new self(sprintf('You cannot rename tag %s to %s, because it already exists', $oldName, $newName));
|
||||
$e = new self(sprintf('You cannot rename tag %s, because it already exists', $renaming->toString()));
|
||||
|
||||
$e->detail = $e->getMessage();
|
||||
$e->title = self::TITLE;
|
||||
$e->type = self::TYPE;
|
||||
$e->status = StatusCodeInterface::STATUS_CONFLICT;
|
||||
$e->additional = [
|
||||
'oldName' => $oldName,
|
||||
'newName' => $newName,
|
||||
];
|
||||
$e->additional = $renaming->toArray();
|
||||
|
||||
return $e;
|
||||
}
|
||||
|
||||
68
module/Core/src/Tag/Model/TagRenaming.php
Normal file
68
module/Core/src/Tag/Model/TagRenaming.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Tag\Model;
|
||||
|
||||
use Shlinkio\Shlink\Core\Exception\ValidationException;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
final class TagRenaming
|
||||
{
|
||||
private string $oldName;
|
||||
private string $newName;
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public static function fromNames(string $oldName, string $newName): self
|
||||
{
|
||||
$o = new self();
|
||||
$o->oldName = $oldName;
|
||||
$o->newName = $newName;
|
||||
|
||||
return $o;
|
||||
}
|
||||
|
||||
public static function fromArray(array $payload): self
|
||||
{
|
||||
if (! isset($payload['oldName'], $payload['newName'])) {
|
||||
throw ValidationException::fromArray([
|
||||
'oldName' => 'oldName is required',
|
||||
'newName' => 'newName is required',
|
||||
]);
|
||||
}
|
||||
|
||||
return self::fromNames($payload['oldName'], $payload['newName']);
|
||||
}
|
||||
|
||||
public function oldName(): string
|
||||
{
|
||||
return $this->oldName;
|
||||
}
|
||||
|
||||
public function newName(): string
|
||||
{
|
||||
return $this->newName;
|
||||
}
|
||||
|
||||
public function nameChanged(): bool
|
||||
{
|
||||
return $this->oldName !== $this->newName;
|
||||
}
|
||||
|
||||
public function toString(): string
|
||||
{
|
||||
return sprintf('%s to %s', $this->oldName, $this->newName);
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'oldName' => $this->oldName,
|
||||
'newName' => $this->newName,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ use Shlinkio\Shlink\Core\Exception\TagNotFoundException;
|
||||
use Shlinkio\Shlink\Core\Repository\TagRepository;
|
||||
use Shlinkio\Shlink\Core\Repository\TagRepositoryInterface;
|
||||
use Shlinkio\Shlink\Core\Tag\Model\TagInfo;
|
||||
use Shlinkio\Shlink\Core\Tag\Model\TagRenaming;
|
||||
use Shlinkio\Shlink\Core\Util\TagManagerTrait;
|
||||
use Shlinkio\Shlink\Rest\ApiKey\Spec\WithApiKeySpecsEnsuringJoin;
|
||||
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||
@@ -82,23 +83,23 @@ class TagService implements TagServiceInterface
|
||||
* @throws TagNotFoundException
|
||||
* @throws TagConflictException
|
||||
*/
|
||||
public function renameTag(string $oldName, string $newName): Tag
|
||||
public function renameTag(TagRenaming $renaming): Tag
|
||||
{
|
||||
/** @var TagRepository $repo */
|
||||
$repo = $this->em->getRepository(Tag::class);
|
||||
|
||||
/** @var Tag|null $tag */
|
||||
$tag = $repo->findOneBy(['name' => $oldName]);
|
||||
$tag = $repo->findOneBy(['name' => $renaming->oldName()]);
|
||||
if ($tag === null) {
|
||||
throw TagNotFoundException::fromTag($oldName);
|
||||
throw TagNotFoundException::fromTag($renaming->oldName());
|
||||
}
|
||||
|
||||
$newNameExists = $newName !== $oldName && $repo->count(['name' => $newName]) > 0;
|
||||
$newNameExists = $renaming->nameChanged() && $repo->count(['name' => $renaming->newName()]) > 0;
|
||||
if ($newNameExists) {
|
||||
throw TagConflictException::fromExistingTag($oldName, $newName);
|
||||
throw TagConflictException::forExistingTag($renaming);
|
||||
}
|
||||
|
||||
$tag->rename($newName);
|
||||
$tag->rename($renaming->newName());
|
||||
$this->em->flush();
|
||||
|
||||
return $tag;
|
||||
|
||||
@@ -9,6 +9,7 @@ use Shlinkio\Shlink\Core\Entity\Tag;
|
||||
use Shlinkio\Shlink\Core\Exception\TagConflictException;
|
||||
use Shlinkio\Shlink\Core\Exception\TagNotFoundException;
|
||||
use Shlinkio\Shlink\Core\Tag\Model\TagInfo;
|
||||
use Shlinkio\Shlink\Core\Tag\Model\TagRenaming;
|
||||
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||
|
||||
interface TagServiceInterface
|
||||
@@ -39,5 +40,5 @@ interface TagServiceInterface
|
||||
* @throws TagNotFoundException
|
||||
* @throws TagConflictException
|
||||
*/
|
||||
public function renameTag(string $oldName, string $newName): Tag;
|
||||
public function renameTag(TagRenaming $renaming): Tag;
|
||||
}
|
||||
|
||||
@@ -2,22 +2,23 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\Rest\Exception;
|
||||
namespace ShlinkioTest\Shlink\Core\Exception;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Shlinkio\Shlink\Core\Exception\TagConflictException;
|
||||
use Shlinkio\Shlink\Core\Tag\Model\TagRenaming;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
class TagConflictExceptionTest extends TestCase
|
||||
{
|
||||
/** @test */
|
||||
public function properlyCreatesExceptionFromNotFoundTag(): void
|
||||
public function properlyCreatesExceptionForExistingTag(): void
|
||||
{
|
||||
$oldName = 'foo';
|
||||
$newName = 'bar';
|
||||
$expectedMessage = sprintf('You cannot rename tag %s to %s, because it already exists', $oldName, $newName);
|
||||
$e = TagConflictException::fromExistingTag($oldName, $newName);
|
||||
$e = TagConflictException::forExistingTag(TagRenaming::fromNames($oldName, $newName));
|
||||
|
||||
self::assertEquals($expectedMessage, $e->getMessage());
|
||||
self::assertEquals($expectedMessage, $e->getDetail());
|
||||
|
||||
@@ -14,6 +14,7 @@ use Shlinkio\Shlink\Core\Exception\TagConflictException;
|
||||
use Shlinkio\Shlink\Core\Exception\TagNotFoundException;
|
||||
use Shlinkio\Shlink\Core\Repository\TagRepository;
|
||||
use Shlinkio\Shlink\Core\Tag\Model\TagInfo;
|
||||
use Shlinkio\Shlink\Core\Tag\Model\TagRenaming;
|
||||
use Shlinkio\Shlink\Core\Tag\TagService;
|
||||
|
||||
class TagServiceTest extends TestCase
|
||||
@@ -92,7 +93,7 @@ class TagServiceTest extends TestCase
|
||||
$find->shouldBeCalled();
|
||||
$this->expectException(TagNotFoundException::class);
|
||||
|
||||
$this->service->renameTag('foo', 'bar');
|
||||
$this->service->renameTag(TagRenaming::fromNames('foo', 'bar'));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -107,7 +108,7 @@ class TagServiceTest extends TestCase
|
||||
$countTags = $this->repo->count(Argument::cetera())->willReturn($count);
|
||||
$flush = $this->em->flush()->willReturn(null);
|
||||
|
||||
$tag = $this->service->renameTag($oldName, $newName);
|
||||
$tag = $this->service->renameTag(TagRenaming::fromNames($oldName, $newName));
|
||||
|
||||
self::assertSame($expected, $tag);
|
||||
self::assertEquals($newName, (string) $tag);
|
||||
@@ -134,6 +135,6 @@ class TagServiceTest extends TestCase
|
||||
$flush->shouldNotBeCalled();
|
||||
$this->expectException(TagConflictException::class);
|
||||
|
||||
$this->service->renameTag('foo', 'bar');
|
||||
$this->service->renameTag(TagRenaming::fromNames('foo', 'bar'));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user