<?php
namespace App\Service\Internal;
use App\Normalizer\DisruptionNormalizer;
use Psr\Log\LoggerInterface;
class DisruptionService
{
public const PATH_GET_DISRUPTIONS = '/v4/networks/%1/disruptions';
public const PATH_GET_LINES_DISRUPTIONS = '/v3/networks/%1/lines/disruptions';
public const PATH_GET_BOARDS_LINES_DISRUPTIONS = '/v3/networks/%1/disruptions/boards/lines';
public const PATH_GET_LINE_DISRUPTIONS = '/v3/networks/{1}/lines/{2}/disruptions';
public const PATH_GET_SUBNETWORKS_DISRUPTIONS = '/v3/networks/%1/disruptions/boards/subNetworks';
protected string $networkId;
protected CoreApiService $coreApiService;
protected LoggerInterface $logger;
protected int $error;
public function __construct(
string $networkId,
CoreApiService $coreApiService,
LoggerInterface $logger
) {
$this->networkId = $networkId;
$this->coreApiService = $coreApiService;
$this->logger = $logger;
$this->error = 0;
}
/**
* Get all the disruptions of the network
*
* @param string|null $subnetworks
* @param string|null $status
* @return array|mixed
*
* @throws \Exception
*/
public function getDisruptions(?string $subnetworks = null, ?string $status = null, ?string $atDateTime = null)
{
$route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_DISRUPTIONS;
$url = preg_replace(['/%1/'], [$this->networkId], $route);
$parameters = [];
if (null !== $subnetworks) {
$parameters['subNetworks'] = $subnetworks;
}
if (null !== $status) {
$parameters['status'] = $status;
}
if (null !== $atDateTime) {
$parameters['atDateTime'] = $atDateTime;
}
$response = $this->coreApiService->call(
CoreApiService::CALLER_TYPE_PUBLIC,
'GET',
$route,
$url,
['Accept' => 'application/json', 'Content-Type' => 'application/json'],
$parameters
);
$this->error = $response->status;
if ($response->status == 200) {
return json_decode(json_encode($response->body), true);
} else {
return [];
}
}
/**
* Get all the lines disruptions of the network
*
* @param string|null $subnetworks
* @param string|null $status
* @return array<string, mixed>
*
* @throws \Exception
*/
public function getLinesDisruptions(?string $subnetworks = null, ?string $status = null, ?string $atDateTime = null): array
{
$route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_DISRUPTIONS;
$url = preg_replace(['/%1/'], [$this->networkId], $route);
$parameters = [];
$parameters['type'] = 'LINE';
if (null !== $subnetworks) {
$parameters['subNetworks'] = $subnetworks;
}
if (null !== $status) {
$parameters['status'] = $status;
}
if (null !== $atDateTime) {
$parameters['atDateTime'] = $atDateTime;
}
$response = $this->coreApiService->call(
CoreApiService::CALLER_TYPE_PUBLIC,
'GET',
$route,
$url,
['Accept' => 'application/json', 'Content-Type' => 'application/json'],
$parameters
);
$this->error = $response->status;
if ($response->status == 200) {
return json_decode(json_encode($response->body), true);
} else {
return [];
}
}
/**
* Retrieve a given line disruptions
*
* @param string $lineId
* @return array<string, array<int, mixed>>
*/
public function retrieveLineDisruptions(string $lineId): array
{
$currentLineDisruptions = $this->getSingleLineDisruptions($lineId, DisruptionNormalizer::STATUS_ONGOING);
$futureLineDisruptions = $this->getSingleLineDisruptions($lineId, DisruptionNormalizer::STATUS_COMING);
return [
'currentDisruptions' => $currentLineDisruptions['disruptions'],
'futureDisruptions' => $futureLineDisruptions['disruptions'],
];
}
/**
* Get all disruptions for a given line
*
* @param string $lineId
* @param string|null $status
* @return array<string, mixed>
* @throws \Exception
*/
public function getSingleLineDisruptions(string $lineId, ?string $status = null): array
{
$route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_DISRUPTIONS;
$url = preg_replace(['/%1/'], [$this->networkId], $route);
$parameters = [];
$parameters['entityIds'] = $lineId;
if (null !== $status) {
$parameters['status'] = $status;
}
$response = $this->coreApiService->call(
CoreApiService::CALLER_TYPE_PUBLIC,
'GET',
$route,
$url,
['Accept' => 'application/json', 'Content-Type' => 'application/json'],
$parameters
);
$this->error = $response->status;
if ($response->status == 200) {
return json_decode(json_encode($response->body), true);
} else {
return [];
}
}
/**
* Get all boards lines disruptions of the network
*
* @param int $withDisruptionOnly
* @param string|null $subnetworks
*
* @return array|mixed
* @throws \Exception
*/
public function getBoardsLinesDisruptions(int $withDisruptionOnly = 0, ?string $subnetworks = null)
{
$route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_BOARDS_LINES_DISRUPTIONS;
$url = preg_replace(['/%1/'], [$this->networkId], $route);
$parameters['withDisruptionsOnly'] = $withDisruptionOnly;
if (null !== $subnetworks) {
$parameters['subNetworks'] = $subnetworks;
}
$response = $this->coreApiService->call(
CoreApiService::CALLER_TYPE_PUBLIC,
'GET',
$route,
$url,
['Accept' => 'application/json', 'Content-Type' => 'application/json'],
$parameters
);
$this->error = $response->status;
if ($response->status == 200 && isset($response->body)) {
return json_decode(json_encode($response->body), true);
} else {
return [];
}
}
/**
* Get line disruptions of the network
*
* @param string $lineId
* @return array<string, mixed>
*
* @throws \Exception
*/
public function getLineDisruptions(string $lineId): array
{
$parameters['lineIds'] = $lineId;
$route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_LINES_DISRUPTIONS;
$url = preg_replace(['/%1/'], [$this->networkId], $route);
$response = $this->coreApiService->call(
CoreApiService::CALLER_TYPE_PUBLIC,
'GET',
$route,
$url,
['Accept' => 'application/json', 'Content-Type' => 'application/json'],
$parameters
);
$this->error = $response->status;
if ($response->status == 200 && isset($response->body->lines)) {
return json_decode(json_encode($response->body->lines), true);
} else {
return [];
}
}
}