mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Converted EntityDoesNotExistException into a problem details exception renamed as TagNotFoundException
This commit is contained in:
@@ -1,30 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Exception;
|
||||
|
||||
use function implode;
|
||||
use function sprintf;
|
||||
|
||||
class EntityDoesNotExistException extends RuntimeException
|
||||
{
|
||||
public static function createFromEntityAndConditions($entityName, array $conditions)
|
||||
{
|
||||
return new self(sprintf(
|
||||
'Entity of type %s with params [%s] does not exist',
|
||||
$entityName,
|
||||
static::serializeParams($conditions)
|
||||
));
|
||||
}
|
||||
|
||||
private static function serializeParams(array $params)
|
||||
{
|
||||
$result = [];
|
||||
foreach ($params as $key => $value) {
|
||||
$result[] = sprintf('"%s" => "%s"', $key, $value);
|
||||
}
|
||||
|
||||
return implode(', ', $result);
|
||||
}
|
||||
}
|
||||
31
module/Core/src/Exception/TagNotFoundException.php
Normal file
31
module/Core/src/Exception/TagNotFoundException.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Exception;
|
||||
|
||||
use Fig\Http\Message\StatusCodeInterface;
|
||||
use Zend\ProblemDetails\Exception\CommonProblemDetailsExceptionTrait;
|
||||
use Zend\ProblemDetails\Exception\ProblemDetailsExceptionInterface;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
class TagNotFoundException extends DomainException implements ProblemDetailsExceptionInterface
|
||||
{
|
||||
use CommonProblemDetailsExceptionTrait;
|
||||
|
||||
private const TITLE = 'Tag not found';
|
||||
public const TYPE = 'TAG_NOT_FOUND';
|
||||
|
||||
public static function fromTag(string $tag): self
|
||||
{
|
||||
$e = new self(sprintf('Tag with name "%s" could not be found', $tag));
|
||||
|
||||
$e->detail = $e->getMessage();
|
||||
$e->title = self::TITLE;
|
||||
$e->type = self::TYPE;
|
||||
$e->status = StatusCodeInterface::STATUS_NOT_FOUND;
|
||||
|
||||
return $e;
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ namespace Shlinkio\Shlink\Core\Service\Tag;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\ORM;
|
||||
use Shlinkio\Shlink\Core\Entity\Tag;
|
||||
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
|
||||
use Shlinkio\Shlink\Core\Exception\TagNotFoundException;
|
||||
use Shlinkio\Shlink\Core\Repository\TagRepository;
|
||||
use Shlinkio\Shlink\Core\Util\TagManagerTrait;
|
||||
|
||||
@@ -35,8 +35,7 @@ class TagService implements TagServiceInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $tagNames
|
||||
* @return void
|
||||
* @param string[] $tagNames
|
||||
*/
|
||||
public function deleteTags(array $tagNames): void
|
||||
{
|
||||
@@ -60,23 +59,17 @@ class TagService implements TagServiceInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $oldName
|
||||
* @param string $newName
|
||||
* @return Tag
|
||||
* @throws EntityDoesNotExistException
|
||||
* @throws ORM\OptimisticLockException
|
||||
* @throws TagNotFoundException
|
||||
*/
|
||||
public function renameTag($oldName, $newName): Tag
|
||||
public function renameTag(string $oldName, string $newName): Tag
|
||||
{
|
||||
$criteria = ['name' => $oldName];
|
||||
/** @var Tag|null $tag */
|
||||
$tag = $this->em->getRepository(Tag::class)->findOneBy($criteria);
|
||||
$tag = $this->em->getRepository(Tag::class)->findOneBy(['name' => $oldName]);
|
||||
if ($tag === null) {
|
||||
throw EntityDoesNotExistException::createFromEntityAndConditions(Tag::class, $criteria);
|
||||
throw TagNotFoundException::fromTag($oldName);
|
||||
}
|
||||
|
||||
$tag->rename($newName);
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
return $tag;
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace Shlinkio\Shlink\Core\Service\Tag;
|
||||
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Shlinkio\Shlink\Core\Entity\Tag;
|
||||
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
|
||||
use Shlinkio\Shlink\Core\Exception\TagNotFoundException;
|
||||
|
||||
interface TagServiceInterface
|
||||
{
|
||||
@@ -17,23 +17,17 @@ interface TagServiceInterface
|
||||
|
||||
/**
|
||||
* @param string[] $tagNames
|
||||
* @return void
|
||||
*/
|
||||
public function deleteTags(array $tagNames): void;
|
||||
|
||||
/**
|
||||
* Provided a list of tag names, creates all that do not exist yet
|
||||
*
|
||||
* @param string[] $tagNames
|
||||
* @return Collection|Tag[]
|
||||
*/
|
||||
public function createTags(array $tagNames): Collection;
|
||||
|
||||
/**
|
||||
* @param string $oldName
|
||||
* @param string $newName
|
||||
* @return Tag
|
||||
* @throws EntityDoesNotExistException
|
||||
* @throws TagNotFoundException
|
||||
*/
|
||||
public function renameTag($oldName, $newName): Tag;
|
||||
public function renameTag(string $oldName, string $newName): Tag;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user