<?php
/**
* Created by PhpStorm.
* User: DEVELUP-USER3
* Date: 07/10/2019
* Time: 10:15
*/
namespace App\MDS\WebInOutTravelBundle\Controller;
use App\MDS\WebInOutTravelBundle\Service\ApiNewsWebService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\AbstractFOSRestController;
use Symfony\Component\HttpFoundation\JsonResponse;
class ApiNewsWebController extends AbstractFOSRestController
{
/**
* @Rest\Get("/api/newsweb/listGet")
* @param Request $request
* @return Response
*/
public function apiNewsWebListGetAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$newsList = array();
$language = null;
try {
// Compruebo:
// 1. que la página web que solicita este recurso está entre las websites permitidas
$websiteReferer = $request->headers->get('referer') ?? "";
// 2. que la lista de categorías NO esté vacía, que SÍ la hayan enviado y que tenga categorías permitidas.
$categoryList = $request->query->get('categoryList');
// 3. que la lista de tokens NO esté vacía y que SÍ la hayan enviado.
$tokenList = $request->headers->get('token');
$language = $request->query->get('language');
$response_base = ApiNewsWebService::getNewsData($em, $websiteReferer, $tokenList, $categoryList, $language);
$response_body = json_encode($response_base, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$response = new Response($response_body, 200);
} catch (\Throwable $th) {
$response = new Response($th, 400);
}
return $response;
}
/**
* @Rest\Post("/api/newsweb/listPost")
* @param Request $request
* @return Response
*/
public function apiNewsWebListPostAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$websiteReferer = $request->request->get('referer'); // "https://develup.solutions";
$tokenList = $request->request->get('token');
$categoryList = $request->request->get('categoryList');
$language = $request->request->get('language');
$response_base = ApiNewsWebService::getNewsData($em, $websiteReferer, $tokenList, $categoryList, $language);
$response_body = json_encode($response_base, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$response = new Response($response_body, 200);
return $response;
}
/**
* @Rest\Get("/api/newsweb/getExtendedNewsData")
* @param Request $request
* @return Response
*/
public function apiGetExtendedNewsDataAction(Request $request)
{
$newsData = array();
// Compruebo:
// 1. que la página web que solicita este recurso está entre las websites permitidas
$websiteReferer = $request->headers->get('referer');
$onlyHost = parse_url($websiteReferer, PHP_URL_HOST);
$isAllowedWeb = ApiNewsWebService::isAllowedWebsiteReferer($onlyHost);
// 2. que el token no sea vacío y no sea nulo
$token = $request->headers->get('token');
$isTokenValid = ApiNewsWebService::isNotNullAndNotEmpty($token);
if ($isAllowedWeb && $isTokenValid) {
$em = $this->getDoctrine()->getManager();
$response_base = ApiNewsWebService::getNewsDataExtended($em, $token, $websiteReferer);
} else {
$newsData[] = [
"isAllowedWeb" => $isAllowedWeb,
"isTokenValid" => $isTokenValid,
];
$response_base = array(
'data' => $newsData,
'response' => 'KO'
);
}
$response_body = json_encode($response_base, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$response = new Response($response_body, 200);
return $response;
}
/**
* @Rest\Post("/api/newsweb/getExtendedNewsDataPost")
* @param Request $request
* @return Response
*/
public function apiGetExtendedNewsDataPostAction(Request $request)
{
$newsData = array();
// Compruebo:
// 1. que la página web que solicita este recurso está entre las websites permitidas
$websiteReferer = $request->request->get('referer');
$onlyHost = parse_url($websiteReferer, PHP_URL_HOST);
$isAllowedWeb = ApiNewsWebService::isAllowedWebsiteReferer($onlyHost);
// 2. que el token no sea vacío y no sea nulo
$token = $request->request->get('token');
$newsSlug = $request->request->get('slug') ?? '';
$isTokenValid = ApiNewsWebService::isNotNullAndNotEmpty($token);
$isNewsIdValid = ApiNewsWebService::isNotNullAndNotEmpty($newsSlug);
if ($isAllowedWeb && $isTokenValid && $isNewsIdValid) {
$em = $this->getDoctrine()->getManager();
$response_base = ApiNewsWebService::getNewsDataExtendedId($em, $newsSlug, $websiteReferer);
} else {
$newsData[] = [
"isAllowedWeb" => $isAllowedWeb,
"isTokenValid" => $isTokenValid,
];
$response_base = array(
'data' => $newsData,
'response' => 'KO'
);
}
$response_body = json_encode($response_base, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$response = new Response($response_body, 200);
return $response;
}
}