Added Create and Delete tag actions

This commit is contained in:
Alejandro Celaya
2017-07-15 09:00:53 +02:00
parent 6717102dd2
commit b2d9f2fc01
10 changed files with 286 additions and 11 deletions

View File

@@ -2,11 +2,16 @@
namespace Shlinkio\Shlink\Core\Service\Tag;
use Acelaya\ZsmAnnotatedServices\Annotation as DI;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\EntityManagerInterface;
use Shlinkio\Shlink\Core\Entity\Tag;
use Shlinkio\Shlink\Core\Repository\TagRepository;
use Shlinkio\Shlink\Core\Util\TagManagerTrait;
class TagService implements TagServiceInterface
{
use TagManagerTrait;
/**
* @var EntityManagerInterface
*/
@@ -31,4 +36,29 @@ class TagService implements TagServiceInterface
{
return $this->em->getRepository(Tag::class)->findBy([], ['name' => 'ASC']);
}
/**
* @param array $tagNames
* @return void
*/
public function deleteTags(array $tagNames)
{
/** @var TagRepository $repo */
$repo = $this->em->getRepository(Tag::class);
$repo->deleteByName($tagNames);
}
/**
* Provided a list of tag names, creates all that do not exist yet
*
* @param string[] $tagNames
* @return Collection|Tag[]
*/
public function createTags(array $tagNames)
{
$tags = $this->tagNamesToEntities($this->em, $tagNames);
$this->em->flush();
return $tags;
}
}

View File

@@ -1,6 +1,7 @@
<?php
namespace Shlinkio\Shlink\Core\Service\Tag;
use Doctrine\Common\Collections\Collection;
use Shlinkio\Shlink\Core\Entity\Tag;
interface TagServiceInterface
@@ -9,4 +10,18 @@ interface TagServiceInterface
* @return Tag[]
*/
public function listTags();
/**
* @param string[] $tagNames
* @return void
*/
public function deleteTags(array $tagNames);
/**
* Provided a list of tag names, creates all that do not exist yet
*
* @param string[] $tagNames
* @return Collection|Tag[]
*/
public function createTags(array $tagNames);
}