src/MDS/WebInOutTravelBundle/Service/ApiNewsWebService.php line 119

Open in your IDE?
  1. <?php
  2. namespace App\MDS\WebInOutTravelBundle\Service;
  3. use App\Constants\NewsConstants;
  4. use App\DTO\NewsDTO;
  5. use App\Entity\Configuration;
  6. use App\Entity\News;
  7. use App\Entity\SettingsCompany;
  8. use App\Helper\NewsHelper;
  9. use Doctrine\Common\Persistence\ObjectManager;
  10. use App\MDS\WebInOutTravelBundle\Constants\ApiNewsWebConstants;
  11. class ApiNewsWebService
  12. {
  13.     public static function isRequestAllowed($onlyHost$token$categoryList) {
  14.         return
  15.             (
  16.             self::isAllowedWebsiteReferer($onlyHost) &&
  17.             self::isAllowedCategory($categoryList) &&
  18.             self::isTokenValid($token)
  19.             )
  20.         ;
  21.     }
  22.     /**
  23.      * @param $onlyHost
  24.      * @return bool
  25.      */
  26.     public static function isAllowedWebsiteReferer ($onlyHost) {
  27.         return in_array($onlyHostApiNewsWebConstants::$allowedWebsitesReferer);
  28.     }
  29.     /**
  30.      * @param $token
  31.      * @return bool
  32.      */
  33.     public static function isTokenValid ($token) {
  34.         return !is_null($token) && !empty($token);
  35.     }
  36.     /**
  37.      * @param $data
  38.      * @return bool
  39.      */
  40.     public static function isNotNullAndNotEmpty ($data) {
  41.         return !is_null($data) && !empty($data);
  42.     }
  43.     /**
  44.      * @param $category
  45.      * @return bool
  46.      */
  47.     public static function isAllowedCategory ($category) {
  48.         return
  49.             !is_null($category) &&
  50.             !empty($category) &&
  51.             in_array($categoryApiNewsWebConstants::$allowedCategories);
  52.     }
  53.     /**
  54.      * @param ObjectManager $em
  55.      * @param $token
  56.      * @return News | null
  57.      */
  58.     private static function getNewsByToken($em$token) {
  59.         $news $em->getRepository(News::class)->findOneBy(
  60.             array(
  61.                 "token" => $token,
  62.             )
  63.         )
  64.         ;
  65.         return $news;
  66.     }
  67.     /**
  68.      * @param ObjectManager $em
  69.      * @param $newsId
  70.      * @return News | null
  71.      */
  72.     private static function getNewsByNewsId($em$newsSlug) {
  73.         $idnews $em->getRepository(News::class)->findOneBy(
  74.             array(
  75.                 "slug" => $newsSlug,
  76.             )
  77.         );
  78.         return $idnews;
  79.     }
  80.     public static function getNewsData($em$websiteReferer$tokenList$categoryList$language) {
  81.         $newsList = array();
  82.         // Compruebo:
  83.         // 1. que la página web que solicita este recurso está entre las websites permitidas
  84.         $isAllowedWeb ApiNewsWebService::isAllowedWebsiteReferer($websiteReferer);
  85.         // 2. que la lista de categorías NO esté vacía, que SÍ la hayan enviado y que tenga categorías permitidas.
  86.         $isAllowedCategory ApiNewsWebService::isAllowedCategory($categoryList);
  87.         // 3. que la lista de tokens NO esté vacía y que SÍ la hayan enviado.
  88.         $isTokenValid ApiNewsWebService::isTokenValid($tokenList);
  89.         // dd($isAllowedWeb, $websiteReferer);
  90.         if ($isAllowedWeb && $isAllowedCategory && $isTokenValid) {
  91.             $tokens $tokenList;
  92.             if (is_array($tokenList)) {
  93.                 $tokens explode(","$tokenList);
  94.             }
  95.             $settingCompany $em->getRepository(SettingsCompany::class)->findOneBy(
  96.                 array(
  97.                     'token' => $tokens
  98.                 )
  99.             );
  100.             if(!empty($settingCompany) && !is_null($settingCompany)) {
  101.                 // Generamos el listado
  102.                 $helper = new NewsHelper($em);
  103.                 $newsDTOList $helper->getListNewsBySettingCompany($settingCompany->getId(), NewsConstants::ENABLED$language);
  104.                 if (!is_null($newsDTOList)) {
  105.                     foreach($newsDTOList as $newsDTO) {
  106.                         /**
  107.                          * @var NewsDTO $newsDTO
  108.                          */
  109.                         $newsDTOCategory $newsDTO->getCategory();
  110.                         $myCategories $categoryList;
  111.                         if (!is_array($categoryList)) {
  112.                             $myCategories str_split($categoryListstrlen($categoryList) + 1);
  113.                         }
  114.                         if (in_array($newsDTOCategory$myCategories)){
  115.                             $newsList[] = $newsDTO;
  116.                         }
  117.                     }
  118.                 }
  119.             }
  120.         } else {
  121.             $newsList[] = ["dataNotAllowed"];
  122.         }
  123.         $response_base = array(
  124.             'data' => $newsList,
  125.         );
  126.         return $response_base;
  127.     }
  128.     /**
  129.      * @param ObjectManager $em
  130.      * @param $token
  131.      * @param $baseURL
  132.      * @return array
  133.      */
  134.     public static function getNewsDataExtended($em$token$baseURL) {
  135.         $news self::getNewsByToken($em$token);
  136.         if (!is_null($news) && !empty($news)) {
  137.             $newsData = [
  138.                 "id" => $news->getId(),
  139.                 "image1" => $news->getDocument1(),
  140.                 "headLine" => $news->getHeadline(),
  141.                 "header" => $news->getHeader(),
  142.                 "observation" => $news->getObservations(),
  143.                 "releasedDate" => $news->getPublicationDate(),
  144.                 "image2" => $news->getDocument2(),
  145.                 "category" => $news->getCategory(),
  146.                 "url" => $baseURL self::slugify($news->getHeadline())
  147.             ]
  148.             ;
  149.             $response "OK";
  150.         }else{
  151.             $newsData = ["Not allowed"];
  152.             $response "KO";
  153.         }
  154.         $response_base = array(
  155.             'data' => $newsData,
  156.             'response' => $response
  157.         );
  158.         return $response_base;
  159.     }
  160.     /**
  161.      * @param ObjectManager $em
  162.      * @param $newsSlug
  163.      * @param $baseURL
  164.      * @return array
  165.      */
  166.     public static function getNewsDataExtendedId($em$newsSlug$baseURL) {
  167.         $news self::getNewsByNewsId($em$newsSlug);
  168.         if (!is_null($news) && !empty($news)) {
  169.             $newsData = [
  170.                 "id" => $news->getId(),
  171.                 "image1" => $news->getDocument1(),
  172.                 "headLine" => $news->getHeadline(),
  173.                 "header" => $news->getHeader(),
  174.                 "observation" => $news->getObservations(),
  175.                 "releasedDate" => $news->getPublicationDate(),
  176.                 "image2" => $news->getDocument2(),
  177.                 "category" => $news->getCategory(),
  178.                 "url" => $baseURL self::slugify($news->getHeadline())
  179.             ]
  180.             ;
  181.             $response "OK";
  182.         }else{
  183.             $newsData = ["Not allowed"];
  184.             $response "KO";
  185.         }
  186.         $response_base = array(
  187.             'data' => $newsData,
  188.             'response' => $response
  189.         );
  190.         return $response_base;
  191.     }
  192.     private static function slugify($text) {
  193.         return str_replace(" ""-"$text);
  194.     }
  195. }