src/Service/Internal/DisruptionService.php line 60

Open in your IDE?
  1. <?php
  2. namespace App\Service\Internal;
  3. use App\Normalizer\DisruptionNormalizer;
  4. use Psr\Log\LoggerInterface;
  5. class DisruptionService
  6. {
  7. public const PATH_GET_DISRUPTIONS = '/v4/networks/%1/disruptions';
  8. public const PATH_GET_LINES_DISRUPTIONS = '/v3/networks/%1/lines/disruptions';
  9. public const PATH_GET_BOARDS_LINES_DISRUPTIONS = '/v3/networks/%1/disruptions/boards/lines';
  10. public const PATH_GET_LINE_DISRUPTIONS = '/v3/networks/{1}/lines/{2}/disruptions';
  11. public const PATH_GET_SUBNETWORKS_DISRUPTIONS = '/v3/networks/%1/disruptions/boards/subNetworks';
  12. protected string $networkId;
  13. protected CoreApiService $coreApiService;
  14. protected LoggerInterface $logger;
  15. protected int $error;
  16. public function __construct(
  17. string $networkId,
  18. CoreApiService $coreApiService,
  19. LoggerInterface $logger
  20. ) {
  21. $this->networkId = $networkId;
  22. $this->coreApiService = $coreApiService;
  23. $this->logger = $logger;
  24. $this->error = 0;
  25. }
  26. /**
  27. * Get all the disruptions of the network
  28. *
  29. * @param string|null $subnetworks
  30. * @param string|null $status
  31. * @return array|mixed
  32. *
  33. * @throws \Exception
  34. */
  35. public function getDisruptions(?string $subnetworks = null, ?string $status = null, ?string $atDateTime = null)
  36. {
  37. $route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_DISRUPTIONS;
  38. $url = preg_replace(['/%1/'], [$this->networkId], $route);
  39. $parameters = [];
  40. if (null !== $subnetworks) {
  41. $parameters['subNetworks'] = $subnetworks;
  42. }
  43. if (null !== $status) {
  44. $parameters['status'] = $status;
  45. }
  46. if (null !== $atDateTime) {
  47. $parameters['atDateTime'] = $atDateTime;
  48. }
  49. $response = $this->coreApiService->call(
  50. CoreApiService::CALLER_TYPE_PUBLIC,
  51. 'GET',
  52. $route,
  53. $url,
  54. ['Accept' => 'application/json', 'Content-Type' => 'application/json'],
  55. $parameters
  56. );
  57. $this->error = $response->status;
  58. if ($response->status == 200) {
  59. return json_decode(json_encode($response->body), true);
  60. } else {
  61. return [];
  62. }
  63. }
  64. /**
  65. * Get all the lines disruptions of the network
  66. *
  67. * @param string|null $subnetworks
  68. * @param string|null $status
  69. * @return array<string, mixed>
  70. *
  71. * @throws \Exception
  72. */
  73. public function getLinesDisruptions(?string $subnetworks = null, ?string $status = null, ?string $atDateTime = null): array
  74. {
  75. $route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_DISRUPTIONS;
  76. $url = preg_replace(['/%1/'], [$this->networkId], $route);
  77. $parameters = [];
  78. $parameters['type'] = 'LINE';
  79. if (null !== $subnetworks) {
  80. $parameters['subNetworks'] = $subnetworks;
  81. }
  82. if (null !== $status) {
  83. $parameters['status'] = $status;
  84. }
  85. if (null !== $atDateTime) {
  86. $parameters['atDateTime'] = $atDateTime;
  87. }
  88. $response = $this->coreApiService->call(
  89. CoreApiService::CALLER_TYPE_PUBLIC,
  90. 'GET',
  91. $route,
  92. $url,
  93. ['Accept' => 'application/json', 'Content-Type' => 'application/json'],
  94. $parameters
  95. );
  96. $this->error = $response->status;
  97. if ($response->status == 200) {
  98. return json_decode(json_encode($response->body), true);
  99. } else {
  100. return [];
  101. }
  102. }
  103. /**
  104. * Retrieve a given line disruptions
  105. *
  106. * @param string $lineId
  107. * @return array<string, array<int, mixed>>
  108. */
  109. public function retrieveLineDisruptions(string $lineId): array
  110. {
  111. $currentLineDisruptions = $this->getSingleLineDisruptions($lineId, DisruptionNormalizer::STATUS_ONGOING);
  112. $futureLineDisruptions = $this->getSingleLineDisruptions($lineId, DisruptionNormalizer::STATUS_COMING);
  113. return [
  114. 'currentDisruptions' => $currentLineDisruptions['disruptions'],
  115. 'futureDisruptions' => $futureLineDisruptions['disruptions'],
  116. ];
  117. }
  118. /**
  119. * Get all disruptions for a given line
  120. *
  121. * @param string $lineId
  122. * @param string|null $status
  123. * @return array<string, mixed>
  124. * @throws \Exception
  125. */
  126. public function getSingleLineDisruptions(string $lineId, ?string $status = null): array
  127. {
  128. $route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_DISRUPTIONS;
  129. $url = preg_replace(['/%1/'], [$this->networkId], $route);
  130. $parameters = [];
  131. $parameters['entityIds'] = $lineId;
  132. if (null !== $status) {
  133. $parameters['status'] = $status;
  134. }
  135. $response = $this->coreApiService->call(
  136. CoreApiService::CALLER_TYPE_PUBLIC,
  137. 'GET',
  138. $route,
  139. $url,
  140. ['Accept' => 'application/json', 'Content-Type' => 'application/json'],
  141. $parameters
  142. );
  143. $this->error = $response->status;
  144. if ($response->status == 200) {
  145. return json_decode(json_encode($response->body), true);
  146. } else {
  147. return [];
  148. }
  149. }
  150. /**
  151. * Get all boards lines disruptions of the network
  152. *
  153. * @param int $withDisruptionOnly
  154. * @param string|null $subnetworks
  155. *
  156. * @return array|mixed
  157. * @throws \Exception
  158. */
  159. public function getBoardsLinesDisruptions(int $withDisruptionOnly = 0, ?string $subnetworks = null)
  160. {
  161. $route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_BOARDS_LINES_DISRUPTIONS;
  162. $url = preg_replace(['/%1/'], [$this->networkId], $route);
  163. $parameters['withDisruptionsOnly'] = $withDisruptionOnly;
  164. if (null !== $subnetworks) {
  165. $parameters['subNetworks'] = $subnetworks;
  166. }
  167. $response = $this->coreApiService->call(
  168. CoreApiService::CALLER_TYPE_PUBLIC,
  169. 'GET',
  170. $route,
  171. $url,
  172. ['Accept' => 'application/json', 'Content-Type' => 'application/json'],
  173. $parameters
  174. );
  175. $this->error = $response->status;
  176. if ($response->status == 200 && isset($response->body)) {
  177. return json_decode(json_encode($response->body), true);
  178. } else {
  179. return [];
  180. }
  181. }
  182. /**
  183. * Get line disruptions of the network
  184. *
  185. * @param string $lineId
  186. * @return array<string, mixed>
  187. *
  188. * @throws \Exception
  189. */
  190. public function getLineDisruptions(string $lineId): array
  191. {
  192. $parameters['lineIds'] = $lineId;
  193. $route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_LINES_DISRUPTIONS;
  194. $url = preg_replace(['/%1/'], [$this->networkId], $route);
  195. $response = $this->coreApiService->call(
  196. CoreApiService::CALLER_TYPE_PUBLIC,
  197. 'GET',
  198. $route,
  199. $url,
  200. ['Accept' => 'application/json', 'Content-Type' => 'application/json'],
  201. $parameters
  202. );
  203. $this->error = $response->status;
  204. if ($response->status == 200 && isset($response->body->lines)) {
  205. return json_decode(json_encode($response->body->lines), true);
  206. } else {
  207. return [];
  208. }
  209. }
  210. }