src/Service/PartnerService.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Repository\PartnerRepository;
  4. use Symfony\Component\HttpFoundation\File\UploadedFile;
  5. class PartnerService extends BaseService
  6. {
  7.     /**
  8.      * NewsService constructor.
  9.      * @param PartnerRepository $repository
  10.      */
  11.     public function __construct(
  12.         PartnerRepository $repository,
  13.         BaseService $baseService
  14.     ) {
  15.         $this->reflectFromParent($baseService);
  16.         $this->repository = $repository;
  17.     }
  18.     public function getListAll($requestData = null)
  19.     {
  20.         return $this->repository->getListAll($requestData);
  21.     }
  22.     public function getById($id)
  23.     {
  24.         return $this->repository->getById($id);
  25.     }
  26.     public function uploadPhoto(UploadedFile $photo)
  27.     {
  28.         $folder = $this->container->getParameter('dir.partner');
  29.         $newMedia = $this->mediaService->uploadFile($photo, $folder);
  30.         return $newMedia['path'];
  31.     }
  32.     public function removePhoto($entity)
  33.     {
  34.         $entity->setPhoto(null);
  35.         $this->entityManager->flush();
  36.         return;
  37.     }
  38.     public function addEntity($request)
  39.     {
  40.         $input = [];
  41.         // Upload photo
  42.         $photo = $request->files->get('photo');
  43.         if ($photo) {
  44.             $input['photo'] = $this->uploadPhoto($photo);
  45.             $request->files->remove('photo');
  46.         }
  47.         return $this->add($request, $input, null, 'App\DTO\Partner\PartnerOutput', false, [], true);
  48.     }
  49.     public function updateEntity($request)
  50.     {
  51.         $input = [];
  52.         $entity = $this->get($request->get('id'));
  53.         // Remove photo
  54.         if ($request->get('removePhoto')) {
  55.             $this->removePhoto($entity);
  56.         }
  57.         // Upload photo
  58.         $photo = $request->files->get('photo');
  59.         if ($photo) {
  60.             $input['photo'] = $this->uploadPhoto($photo);
  61.             $request->files->remove('photo');
  62.         }
  63.         return  $this->update($entity, $request, $input, null, 'App\DTO\Partner\PartnerOutput', false, true);
  64.     }
  65.     public function updateStatus($request)
  66.     {
  67.         $entity = $this->get($request->get('id'));
  68.         $status = $request->get('status');
  69.         $entity->setStatus($status);
  70.         $this->repository->save($entity);
  71.         return $this->autoMapper->map($entity, 'App\DTO\Partner\PartnerOutput');
  72.     }
  73.     public function deleteEntity($id)
  74.     {
  75.         $entity = $this->get($id);
  76.         $this->delete($entity);
  77.         if ($entity->getPhoto()) {
  78.             $this->mediaService->deleteFile($entity->getPhoto());
  79.         }
  80.         return true;
  81.     }
  82. }