<?php
namespace App\Controller\Api;
use App\Services\Api\MediaGameManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile as FileUploadedFile;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class MediaGameController extends ApiController
{
/** @var MediaGameManager $mediaGameManager */
protected $mediaGameManager;
/**
* @param MediaGameManager $mediaGameManager
*/
public function __construct(EntityManagerInterface $em, ParameterBagInterface $parameterBag, MediaGameManager $mediaGameManager)
{
parent::__construct($em, $parameterBag);
$this->mediaGameManager = $mediaGameManager;
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/media_game/add", name="api_media_game_add", methods={"POST"})
*/
public function add(Request $request)
{
$request = $this->transformJsonBody($request);
$data = $this->getDataFromRequest($request);
$response = $this->mediaGameManager->add($data, $request);
return $this->response($response);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/media_game/delete/{id}", name="api_media_game_delete", methods={"POST"})
*/
public function delete(Int $id)
{
$folder = $this->getParameter("app.path.game_files");
$response = $this->mediaGameManager->delete($id, $folder);
return $this->response($response);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/media_game/{game_id}", name="api_media_game_get", methods={"POST"})
*/
public function getMediaGame(Request $request, Int $game_id)
{
$prefix = $this->getParameter("app.path.game_files");
$baseurl = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath();
$folder = $baseurl . $prefix . "/";
$response = $this->mediaGameManager->getMediaGame($request, $game_id, $folder);
return $this->response($response);
}
protected function getDataFromRequest(Request $request)
{
$data = [
"game_id" => $request->get('game_id'),
];
$image = $request->files->get('file');
if (!empty($image)) {
$data["file"] = new FileUploadedFile($image->getPathname(), $image->getClientOriginalName());
$data['file_size'] = intval($image->getSize());
}
return $data;
}
}