src/Service/ProgramService.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Exception\BadRequestException;
  4. use App\Repository\ProgramRepository;
  5. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  6. use Symfony\Contracts\Translation\TranslatorInterface;
  7. /**
  8.  * Class ProgramService
  9.  * @package App\Service
  10.  */
  11. class ProgramService extends BaseService
  12. {
  13.     /**
  14.      * ProgramService constructor.
  15.      * @param ProgramRepository $repository
  16.      */
  17.     public function __construct(
  18.         ProgramRepository $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 getProgramLevel($level)
  33.     {
  34.         $programs $this->repository->findBy(['level' => $level'status' => 1], ['ordering' => 'ASC']);
  35.         return $this->autoMapper->mapMultiple($programs'App\DTO\Program\ProgramOutput');
  36.     }
  37.     public function getBySlug($slug$request)
  38.     {
  39.         return $this->repository->getBySlug($slug$request);
  40.     }
  41.     public function getById($id)
  42.     {
  43.         return $this->autoMapper->map($this->repository->find($id), 'App\DTO\Program\ProgramOutput');
  44.     }
  45.     public function getListLasted($id null)
  46.     {
  47.         return $this->repository->getListLasted($id);
  48.     }
  49.     public function getUrl($program$review null)
  50.     {
  51.         $url $this->urlGenerator->generate('program.detail', [
  52.             'slug' => $program->getSlug(),
  53.             'id'   => $program->getId()
  54.         ]);
  55.         if ($review) {
  56.             $url .= '?review=1';
  57.         }
  58.         return $this->urlHelper->getAbsoluteUrl($url);
  59.     }
  60.     public function addEntity($request)
  61.     {
  62.         $input = [];
  63.         $metadata $request->request->get('metadata');
  64.         if (isset($metadata['videos'])) {
  65.             $metadata['videos']['link'] = $this->getYoutubeUrl($metadata['videos']['link']);
  66.             $request->request->set('metadata'$metadata);
  67.         }
  68.         if ($request->request->get('level')) {
  69.             $getLastOrderingLevel $this->repository->getLastOrderingLevel($request->request->get('level'));
  70.             if ($getLastOrderingLevel) {
  71.                 $request->request->set('ordering', (int) $getLastOrderingLevel->getOrdering() + 1000);
  72.             }
  73.         }
  74.         $entity $this->add($request$inputnull'ENTITY'false, ['name'], true);
  75.         return $this->autoMapper->map($entity'App\DTO\Program\ProgramOutput');
  76.     }
  77.     public function updateEntity($request)
  78.     {
  79.         $entity $this->get($request->get('id'));
  80.         $input = [];
  81.         // Check Slug Exist
  82.         if ($request->get('name')) {
  83.             $programResult $this->repository->checkExistSlug($entity$this->commonService->slugify($request->get('name')));
  84.             if ($programResult) {
  85.                 throw new BadRequestException($this->translator->trans('message.name_exits'));
  86.             }
  87.         }
  88.         if ($request->get('isRemove')) {
  89.             $entity->setMedia(null);
  90.             $this->repository->save($entity);
  91.         }
  92.         $metadata $request->request->get('metadata');
  93.         if (isset($metadata['videos'])) {
  94.             $metadata['videos']['link'] = $this->getYoutubeUrl($metadata['videos']['link']);
  95.             $request->request->set('metadata'$metadata);
  96.         }
  97.         $entity $this->update($entity$request$inputnull'ENTITY'falsetrue);
  98.         return $this->autoMapper->map($entity'App\DTO\Program\ProgramOutput');
  99.     }
  100.     public function updateField($request)
  101.     {
  102.         $entity $this->get($request->get('id'));
  103.         $entity $this->update($entity$requestnull'App\DTO\Program\UpdateFieldProgramInput''ENTITY'falsetrue);
  104.         return $this->autoMapper->map($entity'App\DTO\Program\ProgramOutput');
  105.     }
  106.     public function deleteEntity($id)
  107.     {
  108.         $entity $this->get($id);
  109.         $path $entity->getMedia() ? ($entity->getMedia())->getPath() : null;
  110.         $this->delete($entity);
  111.         if ($path) {
  112.             $this->mediaService->deleteFile($path);
  113.         }
  114.         return true;
  115.     }
  116.     public function getActiveProgramGroupByLevel()
  117.     {
  118.         $programs $this->repository->getAll(['filter_isRegister' => 1'sort_asc' => 'ordering'], 'ENTITY');
  119.         $levels = [
  120.             $this->LEVEL_BTS => [
  121.                 'id' => $this->LEVEL_BTS,
  122.                 'name' => $this->translator->trans('BTS'),
  123.                 'programs' => []
  124.             ],
  125.             $this->LEVEL_BACHELOR => [
  126.                 'id' => $this->LEVEL_BACHELOR,
  127.                 'name' => $this->translator->trans('Bachelor (Bac +3)'),
  128.                 'programs' => []
  129.             ],
  130.             $this->LEVEL_MBA => [
  131.                 'id' => $this->LEVEL_MBA,
  132.                 'name' => $this->translator->trans('MSc & MBA (Bac +4)'),
  133.                 'programs' => []
  134.             ]
  135.         ];
  136.         foreach ($programs as $program) {
  137.             $levels[$program->getLevel()]['programs'][] = $program;
  138.         }
  139.         foreach ($levels as &$level) {
  140.             usort($level['programs'], function ($a$b) {
  141.                 if (is_null($b->getYear())) return -1;
  142.                 return $a->getYear() - $b->getYear();
  143.             });
  144.         }
  145.         return array_values($levels);
  146.     }
  147. }