/**
* {@inheritdoc}
*/
public function save($data, $userId, $id = null)
{
$name = $data['name'];
try {
// load existing tag if id is given and create a new one otherwise
if ($id) {
$tag = $this->tagRepository->findTagById($id);
if (!$tag) {
throw new TagNotFoundException($id);
}
} else {
$tag = new Tag();
}
$user = $this->userRepository->findUserById($userId);
// update data
$tag->setName($name);
$tag->setChanger($user);
if (!$id) {
$tag->setCreator($user);
$this->em->persist($tag);
}
$this->em->flush();
return $tag;
} catch (UniqueConstraintViolationException $exc) {
throw new TagAlreadyExistsException($name);
}
}