src/Service/Internal/NetworkService.php line 408

Open in your IDE?
  1. <?php
  2. namespace App\Service\Internal;
  3. use App\Exception\RedirectException;
  4. use Psr\Cache\CacheItemPoolInterface;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\RouterInterface;
  9. use Symfony\Contracts\Translation\TranslatorInterface;
  10. class NetworkService
  11. {
  12. public const PATH_GET_NETWORK = '/v1/GetNetwork';
  13. public const PATH_GET_SUBNETWORKS = '/v3/networks/%1/subNetworks';
  14. public const PATH_GET_SUBNETWORK_LINES = '/v3/networks/%1/subNetworks/%2/lines';
  15. public const PATH_GET_SUBNETWORK = '/v3/networks/%1/subNetworks/%2/';
  16. public const PATH_GET_STOPAREAS = '/v3/networks/%1/stopAreas';
  17. public const PATH_GET_LOCALITIES = '/v3/networks/%1/localities';
  18. public const DEFAULT_NB_DAY_FUTURE = 31;
  19. public const LAYOUT_SCHEDULES_LINES = 'LINES';
  20. public const CONTEXT_PT_SCHEDULES_LINES_V1 = 'PT_SCHEDULES_LINES_V1';
  21. /** @var string */
  22. protected $networkId;
  23. /** @var CoreApiService */
  24. protected $coreApiService;
  25. /** @var HttpService */
  26. private $httpService;
  27. /** @var int */
  28. private $error;
  29. /** @var array */
  30. private $networkData;
  31. /**
  32. * @var TranslatorInterface
  33. */
  34. protected $translator;
  35. /** @var RequestStack */
  36. private $requestStack;
  37. private CacheItemPoolInterface $cache;
  38. public function __construct(
  39. string $networkId,
  40. CoreApiService $coreApiService,
  41. HttpService $httpService,
  42. TranslatorInterface $translator,
  43. RequestStack $requestStack,
  44. CacheItemPoolInterface $cacheItemPool
  45. ) {
  46. $this->networkId = $networkId;
  47. $this->coreApiService = $coreApiService;
  48. $this->error = 0;
  49. $this->httpService = $httpService;
  50. $this->translator = $translator;
  51. $this->requestStack = $requestStack;
  52. $this->cache = $cacheItemPool;
  53. }
  54. /**
  55. * get Network Informations
  56. *
  57. * @throws \Exception
  58. */
  59. public function getNetwork()
  60. {
  61. $cachedNetworkInfos = $this->cache->getItem('networkInfos');
  62. $networkInfos = $cachedNetworkInfos->get();
  63. if (!$cachedNetworkInfos->isHit()) {
  64. $route = $url = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_NETWORK;
  65. $response = $this->coreApiService->call(
  66. CoreApiService::CALLER_TYPE_PUBLIC,
  67. 'GET',
  68. $route,
  69. $url,
  70. ['Accept' => 'application/json', 'Content-Type' => 'application/json'],
  71. ['key' => $this->networkId]
  72. );
  73. $networkInfos = $this->httpService->handleResponse($response, $response->body);
  74. if (!empty($networkInfos)) {
  75. $cachedNetworkInfos->set($networkInfos);
  76. $this->cache->save($cachedNetworkInfos);
  77. }
  78. }
  79. $this->networkData = !empty($networkInfos) && isset($networkInfos['networks']) ? $networkInfos['networks'][0] : [];
  80. return $networkInfos;
  81. }
  82. /**
  83. * Get the itinerary options
  84. *
  85. * @return array
  86. * @throws \Exception
  87. */
  88. public function getItineraryOptions() {
  89. return $this->networkData['layouts']['itinerary_options'];
  90. }
  91. /**
  92. * Get the latitude of the network center
  93. *
  94. * @return string
  95. * @throws \Exception
  96. */
  97. public function getLat() {
  98. return isset($this->networkData['lat']) ? $this->networkData['lat'] : '';
  99. }
  100. /**
  101. * Get the longitude of the network center
  102. *
  103. * @return string
  104. * @throws \Exception
  105. */
  106. public function getLon() {
  107. return isset($this->networkData['lon']) ? $this->networkData['lon'] : '';
  108. }
  109. /**
  110. * Get the LayoutScheduleLine of the network center
  111. *
  112. * @return Boolean
  113. * @throws \Exception
  114. */
  115. public function getLayoutScheduleLine(): bool {
  116. if (isset($this->networkData['layouts']['schedule'])) {
  117. foreach ($this->networkData['layouts']['schedule'] as $schedule) {
  118. if (self::LAYOUT_SCHEDULES_LINES === $schedule) {
  119. return true;
  120. }
  121. }
  122. }
  123. return false;
  124. }
  125. /**
  126. * Get the [latitude, longitude] of the network center
  127. *
  128. * @return string
  129. * @throws \Exception
  130. */
  131. public function getLatLon() {
  132. if (!empty($this->getLat() && !empty($this->getLon()))) {
  133. return '[' . $this->getLat() . ', ' . $this->getLon() . ']';
  134. } else {
  135. return '';
  136. }
  137. }
  138. /**
  139. * @param string|null $operatorId
  140. *
  141. * @return array
  142. * @throws \Exception
  143. */
  144. public function getOperatorInfos(?string $operatorId = null): array
  145. {
  146. if (empty($this->networkData)) {
  147. $this->getNetwork();
  148. }
  149. $operatorsList = [];
  150. if (isset($this->networkData['operators'])) {
  151. foreach ($this->networkData['operators'] as $operator) {
  152. if(null !== $operatorId && $operatorId === $operator['id']) {
  153. return $operator;
  154. }
  155. if (null === $operatorId) {
  156. $operatorsList[$operator['id']] = [
  157. 'name' => $operator['name'] ?? '',
  158. 'logo' => !empty($operator['logo']) ? $operator['logo'] : $operator['whiteLogo'] ?? '',
  159. ];
  160. }
  161. }
  162. }
  163. return $operatorsList;
  164. }
  165. /**
  166. * @return bool
  167. */
  168. public function isExternalNetwork()
  169. {
  170. $isExternal = false;
  171. if (isset($this->networkData['options'])) {
  172. foreach ($this->networkData['options'] as $option) {
  173. if (isset($option['id']) && $option['id'] === 'NETWORK_EXTERNAL') {
  174. $isExternal = true;
  175. }
  176. }
  177. }
  178. return $isExternal;
  179. }
  180. /**
  181. * @return bool
  182. */
  183. public function getNbDayFuture()
  184. {
  185. $nbDayFuture = self::DEFAULT_NB_DAY_FUTURE;
  186. if (isset($this->networkData['options'])) {
  187. foreach ($this->networkData['options'] as $option) {
  188. if (isset($option['id']) && $option['id'] === 'TPJOURNEY_NBDAYFUTUR') {
  189. $nbDayFuture = $option['valueint1'];
  190. }
  191. }
  192. }
  193. return $nbDayFuture;
  194. }
  195. public function getDisplayScholarLines(): int
  196. {
  197. $displayScholarLines = 0;
  198. $this->getNetwork();
  199. if (isset($this->networkData['options'])) {
  200. foreach ($this->networkData['options'] as $option) {
  201. if (isset($option['id']) && $option['id'] === 'DISPLAY_SCHOLARLINES') {
  202. $displayScholarLines = $option['valueint1'];
  203. }
  204. }
  205. }
  206. return $displayScholarLines;
  207. }
  208. /**
  209. * Get subNetworks
  210. *
  211. * @param string|null $context
  212. *
  213. * @return array
  214. * @throws \Exception
  215. */
  216. public function getSubNetworks(?string $context = null): array
  217. {
  218. $route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_SUBNETWORKS;
  219. $url = preg_replace(['/%1/'], [$this->networkId], $route);
  220. $filters = [];
  221. if (!empty($context)) {
  222. $filters['context'] = $context;
  223. }
  224. $response = $this->coreApiService->call(
  225. CoreApiService::CALLER_TYPE_PUBLIC,
  226. 'GET',
  227. $route,
  228. $url,
  229. ['Accept' => 'application/json', 'Content-Type' => 'application/json'],
  230. $filters
  231. );
  232. $this->error = $response->status;
  233. if ($response->status == 200) {
  234. return json_decode(json_encode($response->body->subNetworks), true);
  235. } else {
  236. return [];
  237. }
  238. }
  239. /**
  240. * get subNetwork
  241. *
  242. * @throws \Exception
  243. */
  244. public function getSubNetwork($id)
  245. {
  246. $filters['context'] = 'PT_SCHEDULES_LINES_V1';
  247. $route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_SUBNETWORK;
  248. $url = preg_replace(['/%1/', '/%2/'], [$this->networkId, $id], $route);
  249. $response = $this->coreApiService->call(
  250. CoreApiService::CALLER_TYPE_PUBLIC,
  251. 'GET',
  252. $route,
  253. $url,
  254. ['Accept' => 'application/json', 'Content-Type' => 'application/json'],
  255. $filters
  256. );
  257. $this->error = $response->status;
  258. if ($response->status == 200) {
  259. return json_decode(json_encode($response->body->subNetworks), true);
  260. } else {
  261. return [];
  262. }
  263. }
  264. /**
  265. * Get subNetwork Lines
  266. *
  267. * @param string $id
  268. * @param string|null $context
  269. * @return array|mixed
  270. * @throws \Exception
  271. */
  272. public function getSubNetworkLines(string $id, ?string $context = self::CONTEXT_PT_SCHEDULES_LINES_V1, int $scholar = 0): array
  273. {
  274. $filters = [];
  275. if (!empty($context)) {
  276. $filters['context'] = $context;
  277. }
  278. $filters['scholar'] = $scholar;
  279. $route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_SUBNETWORK_LINES;
  280. $url = preg_replace(['/%1/', '/%2/'], [$this->networkId, $id], $route);
  281. $response = $this->coreApiService->call(
  282. CoreApiService::CALLER_TYPE_PUBLIC,
  283. 'GET',
  284. $route,
  285. $url,
  286. ['Accept' => 'application/json', 'Content-Type' => 'application/json'],
  287. $filters
  288. );
  289. $this->error = $response->status;
  290. if ($response->status == 200) {
  291. return json_decode(json_encode($response->body->lines), true);
  292. } else {
  293. return [];
  294. }
  295. }
  296. /**
  297. * get StopAreas
  298. *
  299. * @throws \Exception
  300. */
  301. public function getStopAreas()
  302. {
  303. $filters['context'] = 'PT_SCHEDULES_LINES_V1';
  304. $route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_STOPAREAS;
  305. $url = preg_replace(['/%1/'], [$this->networkId], $route);
  306. $response = $this->coreApiService->call(
  307. CoreApiService::CALLER_TYPE_PUBLIC,
  308. 'GET',
  309. $route,
  310. $url,
  311. ['Accept' => 'application/json', 'Content-Type' => 'application/json'],
  312. $filters
  313. );
  314. $this->error = $response->status;
  315. if ($response->status == 200) {
  316. return json_decode(json_encode($response->body->stopAreas), true);
  317. } else {
  318. return [];
  319. }
  320. }
  321. /**
  322. * get Localities
  323. *
  324. * @throws \Exception
  325. */
  326. public function getLocalities()
  327. {
  328. $filters['context'] = 'PT_SCHEDULES_LINES_V1';
  329. $route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_LOCALITIES;
  330. $url = preg_replace(['/%1/'], [$this->networkId], $route);
  331. $response = $this->coreApiService->call(
  332. CoreApiService::CALLER_TYPE_PUBLIC,
  333. 'GET',
  334. $route,
  335. $url,
  336. ['Accept' => 'application/json', 'Content-Type' => 'application/json'],
  337. $filters
  338. );
  339. $this->error = $response->status;
  340. if ($response->status == 200) {
  341. return json_decode(json_encode($response->body->localities), true);
  342. } else {
  343. return [];
  344. }
  345. }
  346. /**
  347. * Get the disruption layout
  348. *
  349. * @return array
  350. * @throws \Exception
  351. */
  352. public function getDisruptionLayout() {
  353. $this->getNetwork();
  354. return $this->networkData['layouts']['disruption'];
  355. }
  356. public function getUseSectionsInPlacesSearch () : bool
  357. {
  358. $this->getNetwork();
  359. return $this->networkData['layouts']['useSectionsInPlacesSearch'] ?? true;
  360. }
  361. public function getUseLatencyInPlacesSearch () : bool
  362. {
  363. $this->getNetwork();
  364. return $this->networkData['layouts']['useLatencyInPlacesSearch'] ?? true;
  365. }
  366. /**
  367. * Get proximities layout
  368. *
  369. * @return array
  370. * @throws \Exception
  371. */
  372. public function getProximitiesLayout() {
  373. $this->getNetwork();
  374. return $this->networkData['layouts']['proximities'];
  375. }
  376. /**
  377. * Get webDynamicMap layout
  378. *
  379. * @return array
  380. * @throws \Exception
  381. */
  382. public function getWebDynamicMapLayout() {
  383. $this->getNetwork();
  384. if (!isset($this->networkData['layouts']['webDynamicMap'])) {
  385. return [];
  386. }
  387. return $this->networkData['layouts']['webDynamicMap'];
  388. }
  389. }