<?php
namespace App\Service;
use App\Exception\BadRequestException;
use App\Repository\NewsRepository;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Class NewsService
* @package App\Service
*/
class NewsService extends BaseService
{
/**
* NewsService constructor.
* @param NewsRepository $repository
*/
public function __construct(
NewsRepository $repository,
BaseService $baseService,
UrlGeneratorInterface $urlGenerator,
TranslatorInterface $translator
) {
$this->reflectFromParent($baseService);
$this->repository = $repository;
$this->urlGenerator = $urlGenerator;
$this->translator = $translator;
}
public function getListAll($requestData)
{
return $this->repository->getListAll($requestData);
}
public function getBySlug($slug, $request)
{
return $this->repository->getBySlug($slug, $request);
}
public function getById($id)
{
return $this->repository->getById($id);
}
public function getListLasted($id = null)
{
return $this->repository->getListLasted($id);
}
public function getUrl($news, $review = null)
{
$url = $this->urlGenerator->generate('news.detail', [
'slug' => $news->getSlug(),
'id' => $news->getId()
], UrlGeneratorInterface::ABSOLUTE_URL);
if ($review) {
$url .= '?review=1';
}
return $this->urlHelper->getAbsoluteUrl($url);
}
public function addEntity($request)
{
$input = [];
// $input['status'] = 0;
$input['priority'] = 1;
$entity = $this->add($request, $input, null, 'ENTITY', false, ['title'], true);
return $this->autoMapper->map($entity, 'App\DTO\News\NewsOutputList');
}
public function updateEntity($request)
{
$entity = $this->get($request->get('id'));
$input = [];
// Check Slug Exist
if ($request->get('title')) {
$newsResult = $this->repository->checkExistSlug($entity, $this->commonService->slugify($request->get('title')));
if ($newsResult) {
throw new BadRequestException($this->translator->trans('message.name_exits'));
}
}
if ($request->get('isRemove')) {
$entity->setMedia(null);
$this->repository->save($entity);
}
$entity = $this->update($entity, $request, $input, null, 'ENTITY', false, true);
return $this->autoMapper->map($entity, 'App\DTO\News\NewsOutputList');
}
public function updateStatus($request)
{
$entity = $this->get($request->get('id'));
$status = $request->get('status');
$entity->setStatus($status);
$this->repository->save($entity);
return $this->autoMapper->map($entity, 'App\DTO\News\NewsOutput');
}
public function deleteEntity($id)
{
$entity = $this->get($id);
$path = $entity->getMedia() ? ($entity->getMedia())->getPath() : null;
$this->delete($entity);
if ($path) {
$this->mediaService->deleteFile($path);
}
return true;
}
}