src/Service/ProgramService.php line 30

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