<?php
namespace App\Service\Internal;
use Psr\Log\LoggerInterface;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class CmsService
{
const WP_API_MENUS = '/wp-json/menus/v1/menus/';
const WP_HEADER_MENU = 'menu-primaire-header';
const WP_FOOTER_MENU = 'menu-secondaire-footer';
const WP_API_URL = 'http://sim-wordpress-%NETWORK_ID%/';
/** @var string */
protected $networkId;
/** @var CoreApiService */
protected $coreApiService;
/** @var LoggerInterface */
private $logger;
/** @var string */
protected $portalUrl;
/** @var string */
private $env;
/** @var int */
private $error;
private $requestStack;
private CacheItemPoolInterface $cache;
public function __construct(
RequestStack $requestStack,
string $networkId,
CoreApiService $coreApiService,
LoggerInterface $logger,
string $portalUrl,
string $env,
CacheItemPoolInterface $cacheItemPool
) {
$this->requestStack = $requestStack;
$this->networkId = $networkId;
$this->coreApiService = $coreApiService;
$this->logger = $logger;
$this->portalUrl = $portalUrl;
$this->env = $env;
$this->error = 0;
$this->cache = $cacheItemPool;
}
/**
* Get CMS pages
*
* @return array
* @throws \Exception
*/
public function getPages()
{
$url = $this->coreApiService->getApiCoreUrl() . "/v3/networks/" . $this->networkId . '/pages';
$locale = $this->requestStack->getCurrentRequest()->get('_locale');
$response = $this->coreApiService->call(
CoreApiService::CALLER_TYPE_PUBLIC,
'GET',
$url,
$url,
['Accept' => 'application/json', 'Content-Type' => 'application/json'],
['locale' => $locale]
);
if ($response->status === 200 && isset($response->body->data)) {
return $response->body->data;
} else {
return [];
}
}
/**
* Get menu
*
* @return array
*/
public function getMenu()
{
$menuPages = [];
$footerPages = [];
$items = [];
if ($this->portalUrl) {
$items['header'] = [];
$items['footer'] = [];
$resource = self::WP_HEADER_MENU;
$locale = $this->requestStack->getCurrentRequest()->get('_locale');
if($locale !== 'fr' && $this->getHttpResponseCode($this->portalUrl.self::WP_API_MENUS.self::WP_HEADER_MENU.'-' . $locale) === '200') {
$resource .= '-' . $locale;
}
$header = $this->getWpApi($resource);
if(isset($header['items'])){
$items['header'] = $header['items'];
}
$footer = $this->getWpApi(self::WP_FOOTER_MENU);
if(isset($footer['items'])){
$items['footer'] = $footer['items'];
}
} else {
$cmsPages = $this->getPages();
foreach ($cmsPages as $page) {
if ($page->hook == 'MENU') {
$menuPages[$page->id] = $page;
}
if ($page->hook == 'FOOTER') {
$footerPages[$page->id] = $page;
}
}
$pages = $menuPages + $footerPages;
$menu['pages'] = $pages;
}
$menu['menuPages'] = $menuPages;
$menu['footerPages'] = $footerPages;
$menu['items'] = $items;
return $menu;
}
/**
* Get Wordpress API
*
* @param string $resource
* @return array
* @throws \Exception
*/
function getWpApi($resource){
if (!in_array($this->env, ['dev', 'local'])) {
$wsApiUrl = str_replace("%NETWORK_ID%", $this->networkId, self::WP_API_URL);
} else {
$wsApiUrl = $this->portalUrl;
}
$url = $wsApiUrl.self::WP_API_MENUS.$resource;
$cachedWpMenu = $this->cache->getItem('wp-'.$resource);
$result = $cachedWpMenu->get();
if (!$cachedWpMenu->isHit()) {
if ($this->getHttpResponseCode($url) != "200"){
return [];
} else {
$json = file_get_contents($url);
$result = json_decode($json, true);
}
if (!empty($result)) {
$cachedWpMenu->set($result);
$this->cache->save($cachedWpMenu);
}
}
return $result;
}
function getHttpResponseCode($url) {
try {
$headers = get_headers($url);
return substr($headers[0], 9, 3);
} catch (\Exception $e) {
return 500;
}
}
/**
* Get CMS posts
*
* @return array
* @throws \Exception
*/
public function getPosts()
{
$url = $this->coreApiService->getApiCoreUrl() . "/v3/networks/" . $this->networkId . '/posts';
$locale = $this->requestStack->getCurrentRequest()->get('_locale');
$params = ['locale' => $locale];
$response = $this->coreApiService->call(
CoreApiService::CALLER_TYPE_PUBLIC,
'GET',
$url,
$url,
['Accept' => 'application/json', 'Content-Type' => 'application/json'],
$params
);
if ($response->status === 200 && isset($response->body->data) ) {
return $response->body->data;
} else {
return [];
}
}
public function getBanners()
{
$url = $this->coreApiService->getApiCoreUrl() . "/v1/networks/" . $this->networkId . '/banners';
$locale = $this->requestStack->getCurrentRequest()->get('_locale');
$params = ['locale' => $locale];
$response = $this->coreApiService->call(
CoreApiService::CALLER_TYPE_PUBLIC,
'GET',
$url,
$url,
['Accept' => 'application/json', 'Content-Type' => 'application/json'],
$params
);
if ($response->status === 200 && isset($response->body->banners) ) {
return $response->body->banners;
} else {
return [];
}
}
public function getLaunchNews()
{
$url = $this->coreApiService->getApiCoreUrl() . "/v3/networks/" . $this->networkId . '/launchNews';
$locale = $this->requestStack->getCurrentRequest()->get('_locale');
$params = ['locale' => $locale];
$response = $this->coreApiService->call(
CoreApiService::CALLER_TYPE_PUBLIC,
'GET',
$url,
$url,
['Accept' => 'application/json', 'Content-Type' => 'application/json'],
$params
);
if ($response->status === 200 && isset($response->body->launchNews) ) {
return $response->body->launchNews;
} else {
return [];
}
}
}