src/Service/NewsService.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Exception\BadRequestException;
  4. use App\Repository\NewsRepository;
  5. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  6. use Symfony\Contracts\Translation\TranslatorInterface;
  7. /**
  8.  * Class NewsService
  9.  * @package App\Service
  10.  */
  11. class NewsService extends BaseService
  12. {
  13.     /**
  14.      * NewsService constructor.
  15.      * @param NewsRepository $repository
  16.      */
  17.     public function __construct(
  18.         NewsRepository $repository,
  19.         BaseService $baseService,
  20.         UrlGeneratorInterface $urlGenerator,
  21.         TranslatorInterface $translator
  22.     ) {
  23.         $this->reflectFromParent($baseService);
  24.         $this->repository $repository;
  25.         $this->urlGenerator $urlGenerator;
  26.         $this->translator $translator;
  27.     }
  28.     public function getListAll($requestData)
  29.     {
  30.         return $this->repository->getListAll($requestData);
  31.     }
  32.     public function getBySlug($slug$request)
  33.     {
  34.         return $this->repository->getBySlug($slug$request);
  35.     }
  36.     public function getById($id)
  37.     {
  38.         return $this->repository->getById($id);
  39.     }
  40.     public function getListLasted($id null)
  41.     {
  42.         return $this->repository->getListLasted($id);
  43.     }
  44.     public function getUrl($news$review null)
  45.     {
  46.         $url $this->urlGenerator->generate('news.detail', [
  47.             'slug' => $news->getSlug(),
  48.             'id'   => $news->getId()
  49.         ], UrlGeneratorInterface::ABSOLUTE_URL);
  50.         if ($review) {
  51.             $url .= '?review=1';
  52.         }
  53.         return $this->urlHelper->getAbsoluteUrl($url);
  54.     }
  55.     public function addEntity($request)
  56.     {
  57.         $input = [];
  58.         // $input['status'] = 0;
  59.         $input['priority'] = 1;
  60.         $entity $this->add($request$inputnull'ENTITY'false, ['title'], true);
  61.         return $this->autoMapper->map($entity'App\DTO\News\NewsOutputList');
  62.     }
  63.     public function updateEntity($request)
  64.     {
  65.         $entity $this->get($request->get('id'));
  66.         $input = [];
  67.         // Check Slug Exist
  68.         if ($request->get('title')) {
  69.             $newsResult $this->repository->checkExistSlug($entity$this->commonService->slugify($request->get('title')));
  70.             if ($newsResult) {
  71.                 throw new BadRequestException($this->translator->trans('message.name_exits'));
  72.             }
  73.         }
  74.         if ($request->get('isRemove')) {
  75.             $entity->setMedia(null);
  76.             $this->repository->save($entity);
  77.         }
  78.         $entity $this->update($entity$request$inputnull'ENTITY'falsetrue);
  79.         return $this->autoMapper->map($entity'App\DTO\News\NewsOutputList');
  80.     }
  81.     public function updateStatus($request)
  82.     {
  83.         $entity $this->get($request->get('id'));
  84.         $status $request->get('status');
  85.         $entity->setStatus($status);
  86.         $this->repository->save($entity);
  87.         return $this->autoMapper->map($entity'App\DTO\News\NewsOutput');
  88.     }
  89.     public function deleteEntity($id)
  90.     {
  91.         $entity $this->get($id);
  92.         $path $entity->getMedia() ? ($entity->getMedia())->getPath() : null;
  93.         $this->delete($entity);
  94.         if ($path) {
  95.             $this->mediaService->deleteFile($path);
  96.         }
  97.         return true;
  98.     }
  99. }