<?php
namespace App\Service;
use App\Exception\BadRequestException;
use App\Repository\ProgramRepository;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Class ProgramService
* @package App\Service
*/
class ProgramService extends BaseService
{
/**
* ProgramService constructor.
* @param ProgramRepository $repository
*/
public function __construct(
ProgramRepository $repository,
BaseService $baseService,
UrlGeneratorInterface $urlGenerator,
TranslatorInterface $translator
) {
$this->reflectFromParent($baseService);
$this->repository = $repository;
$this->urlGenerator = $urlGenerator;
$this->translator = $translator;
}
public function getListAll($requestData)
{
return $this->repository->getListAll($requestData);
}
public function getProgramLevel($level)
{
$programs = $this->repository->findBy(['level' => $level, 'status' => 1], ['ordering' => 'ASC']);
return $this->autoMapper->mapMultiple($programs, 'App\DTO\Program\ProgramOutput');
}
public function getBySlug($slug, $request)
{
return $this->repository->getBySlug($slug, $request);
}
public function getById($id)
{
return $this->autoMapper->map($this->repository->find($id), 'App\DTO\Program\ProgramOutput');
}
public function getListLasted($id = null)
{
return $this->repository->getListLasted($id);
}
public function getUrl($program, $review = null)
{
$url = $this->urlGenerator->generate('program.detail', [
'slug' => $program->getSlug(),
'id' => $program->getId()
]);
if ($review) {
$url .= '?review=1';
}
return $this->urlHelper->getAbsoluteUrl($url);
}
public function addEntity($request)
{
$input = [];
$metadata = $request->request->get('metadata');
if (isset($metadata['videos'])) {
$metadata['videos']['link'] = $this->getYoutubeUrl($metadata['videos']['link']);
$request->request->set('metadata', $metadata);
}
if ($request->request->get('level')) {
$getLastOrderingLevel = $this->repository->getLastOrderingLevel($request->request->get('level'));
if ($getLastOrderingLevel) {
$request->request->set('ordering', (int) $getLastOrderingLevel->getOrdering() + 1000);
}
}
$entity = $this->add($request, $input, null, 'ENTITY', false, ['name'], true);
return $this->autoMapper->map($entity, 'App\DTO\Program\ProgramOutput');
}
public function updateEntity($request)
{
$entity = $this->get($request->get('id'));
$input = [];
// Check Slug Exist
if ($request->get('name')) {
$programResult = $this->repository->checkExistSlug($entity, $this->commonService->slugify($request->get('name')));
if ($programResult) {
throw new BadRequestException($this->translator->trans('message.name_exits'));
}
}
if ($request->get('isRemove')) {
$entity->setMedia(null);
$this->repository->save($entity);
}
$metadata = $request->request->get('metadata');
if (isset($metadata['videos'])) {
$metadata['videos']['link'] = $this->getYoutubeUrl($metadata['videos']['link']);
$request->request->set('metadata', $metadata);
}
$entity = $this->update($entity, $request, $input, null, 'ENTITY', false, true);
return $this->autoMapper->map($entity, 'App\DTO\Program\ProgramOutput');
}
public function updateField($request)
{
$entity = $this->get($request->get('id'));
$entity = $this->update($entity, $request, null, 'App\DTO\Program\UpdateFieldProgramInput', 'ENTITY', false, true);
return $this->autoMapper->map($entity, 'App\DTO\Program\ProgramOutput');
}
public function deleteEntity($id)
{
$entity = $this->get($id);
$path = $entity->getMedia() ? ($entity->getMedia())->getPath() : null;
$this->delete($entity);
if ($path) {
$this->mediaService->deleteFile($path);
}
return true;
}
public function getActiveProgramGroupByLevel()
{
$programs = $this->repository->getAll(['filter_isRegister' => 1, 'sort_asc' => 'ordering'], 'ENTITY');
$levels = [
$this->LEVEL_BTS => [
'id' => $this->LEVEL_BTS,
'name' => $this->translator->trans('BTS'),
'programs' => []
],
$this->LEVEL_BACHELOR => [
'id' => $this->LEVEL_BACHELOR,
'name' => $this->translator->trans('Bachelor (Bac +3)'),
'programs' => []
],
$this->LEVEL_MBA => [
'id' => $this->LEVEL_MBA,
'name' => $this->translator->trans('MSc & MBA (Bac +4)'),
'programs' => []
]
];
foreach ($programs as $program) {
$levels[$program->getLevel()]['programs'][] = $program;
}
foreach ($levels as &$level) {
usort($level['programs'], function ($a, $b) {
if (is_null($b->getYear())) return -1;
return $a->getYear() - $b->getYear();
});
}
return array_values($levels);
}
}