<?php
namespace App\MDS\WebInOutTravelBundle\Service;
use App\Constants\NewsConstants;
use App\DTO\NewsDTO;
use App\Entity\Configuration;
use App\Entity\News;
use App\Entity\SettingsCompany;
use App\Helper\NewsHelper;
use Doctrine\Common\Persistence\ObjectManager;
use App\MDS\WebInOutTravelBundle\Constants\ApiNewsWebConstants;
class ApiNewsWebService
{
public static function isRequestAllowed($onlyHost, $token, $categoryList) {
return
(
self::isAllowedWebsiteReferer($onlyHost) &&
self::isAllowedCategory($categoryList) &&
self::isTokenValid($token)
)
;
}
/**
* @param $onlyHost
* @return bool
*/
public static function isAllowedWebsiteReferer ($onlyHost) {
return in_array($onlyHost, ApiNewsWebConstants::$allowedWebsitesReferer);
}
/**
* @param $token
* @return bool
*/
public static function isTokenValid ($token) {
return !is_null($token) && !empty($token);
}
/**
* @param $data
* @return bool
*/
public static function isNotNullAndNotEmpty ($data) {
return !is_null($data) && !empty($data);
}
/**
* @param $category
* @return bool
*/
public static function isAllowedCategory ($category) {
return
!is_null($category) &&
!empty($category) &&
in_array($category, ApiNewsWebConstants::$allowedCategories);
}
/**
* @param ObjectManager $em
* @param $token
* @return News | null
*/
private static function getNewsByToken($em, $token) {
$news = $em->getRepository(News::class)->findOneBy(
array(
"token" => $token,
)
)
;
return $news;
}
/**
* @param ObjectManager $em
* @param $newsId
* @return News | null
*/
private static function getNewsByNewsId($em, $newsSlug) {
$idnews = $em->getRepository(News::class)->findOneBy(
array(
"slug" => $newsSlug,
)
);
return $idnews;
}
public static function getNewsData($em, $websiteReferer, $tokenList, $categoryList, $language) {
$newsList = array();
// Compruebo:
// 1. que la página web que solicita este recurso está entre las websites permitidas
$isAllowedWeb = ApiNewsWebService::isAllowedWebsiteReferer($websiteReferer);
// 2. que la lista de categorías NO esté vacía, que SÍ la hayan enviado y que tenga categorías permitidas.
$isAllowedCategory = ApiNewsWebService::isAllowedCategory($categoryList);
// 3. que la lista de tokens NO esté vacía y que SÍ la hayan enviado.
$isTokenValid = ApiNewsWebService::isTokenValid($tokenList);
// dd($isAllowedWeb, $websiteReferer);
if ($isAllowedWeb && $isAllowedCategory && $isTokenValid) {
$tokens = $tokenList;
if (is_array($tokenList)) {
$tokens = explode(",", $tokenList);
}
$settingCompany = $em->getRepository(SettingsCompany::class)->findOneBy(
array(
'token' => $tokens
)
);
if(!empty($settingCompany) && !is_null($settingCompany)) {
// Generamos el listado
$helper = new NewsHelper($em);
$newsDTOList = $helper->getListNewsBySettingCompany($settingCompany->getId(), NewsConstants::ENABLED, $language);
if (!is_null($newsDTOList)) {
foreach($newsDTOList as $newsDTO) {
/**
* @var NewsDTO $newsDTO
*/
$newsDTOCategory = $newsDTO->getCategory();
$myCategories = $categoryList;
if (!is_array($categoryList)) {
$myCategories = str_split($categoryList, strlen($categoryList) + 1);
}
if (in_array($newsDTOCategory, $myCategories)){
$newsList[] = $newsDTO;
}
}
}
}
} else {
$newsList[] = ["dataNotAllowed"];
}
$response_base = array(
'data' => $newsList,
);
return $response_base;
}
/**
* @param ObjectManager $em
* @param $token
* @param $baseURL
* @return array
*/
public static function getNewsDataExtended($em, $token, $baseURL) {
$news = self::getNewsByToken($em, $token);
if (!is_null($news) && !empty($news)) {
$newsData = [
"id" => $news->getId(),
"image1" => $news->getDocument1(),
"headLine" => $news->getHeadline(),
"header" => $news->getHeader(),
"observation" => $news->getObservations(),
"releasedDate" => $news->getPublicationDate(),
"image2" => $news->getDocument2(),
"category" => $news->getCategory(),
"url" => $baseURL . self::slugify($news->getHeadline())
]
;
$response = "OK";
}else{
$newsData = ["Not allowed"];
$response = "KO";
}
$response_base = array(
'data' => $newsData,
'response' => $response
);
return $response_base;
}
/**
* @param ObjectManager $em
* @param $newsSlug
* @param $baseURL
* @return array
*/
public static function getNewsDataExtendedId($em, $newsSlug, $baseURL) {
$news = self::getNewsByNewsId($em, $newsSlug);
if (!is_null($news) && !empty($news)) {
$newsData = [
"id" => $news->getId(),
"image1" => $news->getDocument1(),
"headLine" => $news->getHeadline(),
"header" => $news->getHeader(),
"observation" => $news->getObservations(),
"releasedDate" => $news->getPublicationDate(),
"image2" => $news->getDocument2(),
"category" => $news->getCategory(),
"url" => $baseURL . self::slugify($news->getHeadline())
]
;
$response = "OK";
}else{
$newsData = ["Not allowed"];
$response = "KO";
}
$response_base = array(
'data' => $newsData,
'response' => $response
);
return $response_base;
}
private static function slugify($text) {
return str_replace(" ", "-", $text);
}
}