src/Service/OurStudentService.php line 15

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