<?php
namespace App\Service;
use App\Repository\OurStudentRepository;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class OurStudentService extends BaseService
{
/**
* NewsService constructor.
* @param OurStudentRepository $repository
*/
public function __construct(
OurStudentRepository $repository,
BaseService $baseService
) {
$this->reflectFromParent($baseService);
$this->repository = $repository;
}
public function getById($id)
{
return $this->repository->getById($id);
}
public function uploadImage(UploadedFile $fileUpload)
{
$folder = $this->container->getParameter('our_student.image');
$newMedia = $this->mediaService->uploadFile($fileUpload, $folder);
return $newMedia['path'];
}
public function removeImage($entity)
{
$entity->setImage(null);
$this->entityManager->flush();
return;
}
public function addEntity($request)
{
$input = [];
// Upload image
$image = $request->files->get('image');
if ($image) {
$input['image'] = $this->uploadImage($image);
$request->files->remove('image');
}
if ($request->get('video')) {
$input['video'] = $this->getYoutubeUrl($request->get('video'));
}
$entity = $this->add($request, $input, null, 'ENTITY', false, [], true);
return $this->autoMapper->map($entity, 'App\DTO\OurStudent\OurStudentOutput');
}
public function updateEntity($request)
{
$input = [];
$entity = $this->get($request->get('id'));
// Remove image
if ($request->get('removeImage')) {
$this->removeImage($entity);
}
// Upload image
$image = $request->files->get('image');
if ($image) {
$input['image'] = $this->uploadImage($image);
$request->files->remove('image');
}
if ($request->get('video')) {
$input['video'] = $this->getYoutubeUrl($request->get('video'));
}
$entity = $this->update($entity, $request, $input, null, 'ENTITY', false, true);
return $this->autoMapper->map($entity, 'App\DTO\OurStudent\OurStudentOutput');
}
public function updateStatus($request)
{
$entity = $this->get($request->get('id'));
$status = $request->get('status');
$entity->setStatus($status);
$this->repository->save($entity);
return $this->autoMapper->map($entity, 'App\DTO\OurStudent\OurStudentOutput');
}
public function deleteEntity($id)
{
$entity = $this->get($id);
$this->delete($entity);
if ($entity->getImage()) {
$this->mediaService->deleteFile($entity->getImage());
}
return true;
}
}