src/Service/Internal/CmsService.php line 68

Open in your IDE?
  1. <?php
  2. namespace App\Service\Internal;
  3. use Psr\Log\LoggerInterface;
  4. use Psr\Cache\CacheItemPoolInterface;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. class CmsService
  7. {
  8. const WP_API_MENUS = '/wp-json/menus/v1/menus/';
  9. const WP_HEADER_MENU = 'menu-primaire-header';
  10. const WP_FOOTER_MENU = 'menu-secondaire-footer';
  11. const WP_API_URL = 'http://sim-wordpress-%NETWORK_ID%/';
  12. /** @var string */
  13. protected $networkId;
  14. /** @var CoreApiService */
  15. protected $coreApiService;
  16. /** @var LoggerInterface */
  17. private $logger;
  18. /** @var string */
  19. protected $portalUrl;
  20. /** @var string */
  21. private $env;
  22. /** @var int */
  23. private $error;
  24. private $requestStack;
  25. private CacheItemPoolInterface $cache;
  26. public function __construct(
  27. RequestStack $requestStack,
  28. string $networkId,
  29. CoreApiService $coreApiService,
  30. LoggerInterface $logger,
  31. string $portalUrl,
  32. string $env,
  33. CacheItemPoolInterface $cacheItemPool
  34. ) {
  35. $this->requestStack = $requestStack;
  36. $this->networkId = $networkId;
  37. $this->coreApiService = $coreApiService;
  38. $this->logger = $logger;
  39. $this->portalUrl = $portalUrl;
  40. $this->env = $env;
  41. $this->error = 0;
  42. $this->cache = $cacheItemPool;
  43. }
  44. /**
  45. * Get CMS pages
  46. *
  47. * @return array
  48. * @throws \Exception
  49. */
  50. public function getPages()
  51. {
  52. $url = $this->coreApiService->getApiCoreUrl() . "/v3/networks/" . $this->networkId . '/pages';
  53. $locale = $this->requestStack->getCurrentRequest()->get('_locale');
  54. $response = $this->coreApiService->call(
  55. CoreApiService::CALLER_TYPE_PUBLIC,
  56. 'GET',
  57. $url,
  58. $url,
  59. ['Accept' => 'application/json', 'Content-Type' => 'application/json'],
  60. ['locale' => $locale]
  61. );
  62. if ($response->status === 200 && isset($response->body->data)) {
  63. return $response->body->data;
  64. } else {
  65. return [];
  66. }
  67. }
  68. /**
  69. * Get menu
  70. *
  71. * @return array
  72. */
  73. public function getMenu()
  74. {
  75. $menuPages = [];
  76. $footerPages = [];
  77. $items = [];
  78. if ($this->portalUrl) {
  79. $items['header'] = [];
  80. $items['footer'] = [];
  81. $resource = self::WP_HEADER_MENU;
  82. $locale = $this->requestStack->getCurrentRequest()->get('_locale');
  83. if($locale !== 'fr' && $this->getHttpResponseCode($this->portalUrl.self::WP_API_MENUS.self::WP_HEADER_MENU.'-' . $locale) === '200') {
  84. $resource .= '-' . $locale;
  85. }
  86. $header = $this->getWpApi($resource);
  87. if(isset($header['items'])){
  88. $items['header'] = $header['items'];
  89. }
  90. $footer = $this->getWpApi(self::WP_FOOTER_MENU);
  91. if(isset($footer['items'])){
  92. $items['footer'] = $footer['items'];
  93. }
  94. } else {
  95. $cmsPages = $this->getPages();
  96. foreach ($cmsPages as $page) {
  97. if ($page->hook == 'MENU') {
  98. $menuPages[$page->id] = $page;
  99. }
  100. if ($page->hook == 'FOOTER') {
  101. $footerPages[$page->id] = $page;
  102. }
  103. }
  104. $pages = $menuPages + $footerPages;
  105. $menu['pages'] = $pages;
  106. }
  107. $menu['menuPages'] = $menuPages;
  108. $menu['footerPages'] = $footerPages;
  109. $menu['items'] = $items;
  110. return $menu;
  111. }
  112. /**
  113. * Get Wordpress API
  114. *
  115. * @param string $resource
  116. * @return array
  117. * @throws \Exception
  118. */
  119. function getWpApi($resource){
  120. if (!in_array($this->env, ['dev', 'local'])) {
  121. $wsApiUrl = str_replace("%NETWORK_ID%", $this->networkId, self::WP_API_URL);
  122. } else {
  123. $wsApiUrl = $this->portalUrl;
  124. }
  125. $url = $wsApiUrl.self::WP_API_MENUS.$resource;
  126. $cachedWpMenu = $this->cache->getItem('wp-'.$resource);
  127. $result = $cachedWpMenu->get();
  128. if (!$cachedWpMenu->isHit()) {
  129. if ($this->getHttpResponseCode($url) != "200"){
  130. return [];
  131. } else {
  132. $json = file_get_contents($url);
  133. $result = json_decode($json, true);
  134. }
  135. if (!empty($result)) {
  136. $cachedWpMenu->set($result);
  137. $this->cache->save($cachedWpMenu);
  138. }
  139. }
  140. return $result;
  141. }
  142. function getHttpResponseCode($url) {
  143. try {
  144. $headers = get_headers($url);
  145. return substr($headers[0], 9, 3);
  146. } catch (\Exception $e) {
  147. return 500;
  148. }
  149. }
  150. /**
  151. * Get CMS posts
  152. *
  153. * @return array
  154. * @throws \Exception
  155. */
  156. public function getPosts()
  157. {
  158. $url = $this->coreApiService->getApiCoreUrl() . "/v3/networks/" . $this->networkId . '/posts';
  159. $locale = $this->requestStack->getCurrentRequest()->get('_locale');
  160. $params = ['locale' => $locale];
  161. $response = $this->coreApiService->call(
  162. CoreApiService::CALLER_TYPE_PUBLIC,
  163. 'GET',
  164. $url,
  165. $url,
  166. ['Accept' => 'application/json', 'Content-Type' => 'application/json'],
  167. $params
  168. );
  169. if ($response->status === 200 && isset($response->body->data) ) {
  170. return $response->body->data;
  171. } else {
  172. return [];
  173. }
  174. }
  175. public function getBanners()
  176. {
  177. $url = $this->coreApiService->getApiCoreUrl() . "/v1/networks/" . $this->networkId . '/banners';
  178. $locale = $this->requestStack->getCurrentRequest()->get('_locale');
  179. $params = ['locale' => $locale];
  180. $response = $this->coreApiService->call(
  181. CoreApiService::CALLER_TYPE_PUBLIC,
  182. 'GET',
  183. $url,
  184. $url,
  185. ['Accept' => 'application/json', 'Content-Type' => 'application/json'],
  186. $params
  187. );
  188. if ($response->status === 200 && isset($response->body->banners) ) {
  189. return $response->body->banners;
  190. } else {
  191. return [];
  192. }
  193. }
  194. public function getLaunchNews()
  195. {
  196. $url = $this->coreApiService->getApiCoreUrl() . "/v3/networks/" . $this->networkId . '/launchNews';
  197. $locale = $this->requestStack->getCurrentRequest()->get('_locale');
  198. $params = ['locale' => $locale];
  199. $response = $this->coreApiService->call(
  200. CoreApiService::CALLER_TYPE_PUBLIC,
  201. 'GET',
  202. $url,
  203. $url,
  204. ['Accept' => 'application/json', 'Content-Type' => 'application/json'],
  205. $params
  206. );
  207. if ($response->status === 200 && isset($response->body->launchNews) ) {
  208. return $response->body->launchNews;
  209. } else {
  210. return [];
  211. }
  212. }
  213. }