<?php
namespace App\Service\Internal;
use App\Exception\RedirectException;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class NetworkService
{
public const PATH_GET_NETWORK = '/v1/GetNetwork';
public const PATH_GET_SUBNETWORKS = '/v3/networks/%1/subNetworks';
public const PATH_GET_SUBNETWORK_LINES = '/v3/networks/%1/subNetworks/%2/lines';
public const PATH_GET_SUBNETWORK = '/v3/networks/%1/subNetworks/%2/';
public const PATH_GET_STOPAREAS = '/v3/networks/%1/stopAreas';
public const PATH_GET_LOCALITIES = '/v3/networks/%1/localities';
public const DEFAULT_NB_DAY_FUTURE = 31;
public const LAYOUT_SCHEDULES_LINES = 'LINES';
public const CONTEXT_PT_SCHEDULES_LINES_V1 = 'PT_SCHEDULES_LINES_V1';
/** @var string */
protected $networkId;
/** @var CoreApiService */
protected $coreApiService;
/** @var HttpService */
private $httpService;
/** @var int */
private $error;
/** @var array */
private $networkData;
/**
* @var TranslatorInterface
*/
protected $translator;
/** @var RequestStack */
private $requestStack;
private CacheItemPoolInterface $cache;
public function __construct(
string $networkId,
CoreApiService $coreApiService,
HttpService $httpService,
TranslatorInterface $translator,
RequestStack $requestStack,
CacheItemPoolInterface $cacheItemPool
) {
$this->networkId = $networkId;
$this->coreApiService = $coreApiService;
$this->error = 0;
$this->httpService = $httpService;
$this->translator = $translator;
$this->requestStack = $requestStack;
$this->cache = $cacheItemPool;
}
/**
* get Network Informations
*
* @throws \Exception
*/
public function getNetwork()
{
$cachedNetworkInfos = $this->cache->getItem('networkInfos');
$networkInfos = $cachedNetworkInfos->get();
if (!$cachedNetworkInfos->isHit()) {
$route = $url = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_NETWORK;
$response = $this->coreApiService->call(
CoreApiService::CALLER_TYPE_PUBLIC,
'GET',
$route,
$url,
['Accept' => 'application/json', 'Content-Type' => 'application/json'],
['key' => $this->networkId]
);
$networkInfos = $this->httpService->handleResponse($response, $response->body);
if (!empty($networkInfos)) {
$cachedNetworkInfos->set($networkInfos);
$this->cache->save($cachedNetworkInfos);
}
}
$this->networkData = !empty($networkInfos) && isset($networkInfos['networks']) ? $networkInfos['networks'][0] : [];
return $networkInfos;
}
/**
* Get the itinerary options
*
* @return array
* @throws \Exception
*/
public function getItineraryOptions() {
return $this->networkData['layouts']['itinerary_options'];
}
/**
* Get the latitude of the network center
*
* @return string
* @throws \Exception
*/
public function getLat() {
return isset($this->networkData['lat']) ? $this->networkData['lat'] : '';
}
/**
* Get the longitude of the network center
*
* @return string
* @throws \Exception
*/
public function getLon() {
return isset($this->networkData['lon']) ? $this->networkData['lon'] : '';
}
/**
* Get the LayoutScheduleLine of the network center
*
* @return Boolean
* @throws \Exception
*/
public function getLayoutScheduleLine(): bool {
if (isset($this->networkData['layouts']['schedule'])) {
foreach ($this->networkData['layouts']['schedule'] as $schedule) {
if (self::LAYOUT_SCHEDULES_LINES === $schedule) {
return true;
}
}
}
return false;
}
/**
* Get the [latitude, longitude] of the network center
*
* @return string
* @throws \Exception
*/
public function getLatLon() {
if (!empty($this->getLat() && !empty($this->getLon()))) {
return '[' . $this->getLat() . ', ' . $this->getLon() . ']';
} else {
return '';
}
}
/**
* @param string|null $operatorId
*
* @return array
* @throws \Exception
*/
public function getOperatorInfos(?string $operatorId = null): array
{
if (empty($this->networkData)) {
$this->getNetwork();
}
$operatorsList = [];
if (isset($this->networkData['operators'])) {
foreach ($this->networkData['operators'] as $operator) {
if(null !== $operatorId && $operatorId === $operator['id']) {
return $operator;
}
if (null === $operatorId) {
$operatorsList[$operator['id']] = [
'name' => $operator['name'] ?? '',
'logo' => !empty($operator['logo']) ? $operator['logo'] : $operator['whiteLogo'] ?? '',
];
}
}
}
return $operatorsList;
}
/**
* @return bool
*/
public function isExternalNetwork()
{
$isExternal = false;
if (isset($this->networkData['options'])) {
foreach ($this->networkData['options'] as $option) {
if (isset($option['id']) && $option['id'] === 'NETWORK_EXTERNAL') {
$isExternal = true;
}
}
}
return $isExternal;
}
/**
* @return bool
*/
public function getNbDayFuture()
{
$nbDayFuture = self::DEFAULT_NB_DAY_FUTURE;
if (isset($this->networkData['options'])) {
foreach ($this->networkData['options'] as $option) {
if (isset($option['id']) && $option['id'] === 'TPJOURNEY_NBDAYFUTUR') {
$nbDayFuture = $option['valueint1'];
}
}
}
return $nbDayFuture;
}
public function getDisplayScholarLines(): int
{
$displayScholarLines = 0;
$this->getNetwork();
if (isset($this->networkData['options'])) {
foreach ($this->networkData['options'] as $option) {
if (isset($option['id']) && $option['id'] === 'DISPLAY_SCHOLARLINES') {
$displayScholarLines = $option['valueint1'];
}
}
}
return $displayScholarLines;
}
/**
* Get subNetworks
*
* @param string|null $context
*
* @return array
* @throws \Exception
*/
public function getSubNetworks(?string $context = null): array
{
$route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_SUBNETWORKS;
$url = preg_replace(['/%1/'], [$this->networkId], $route);
$filters = [];
if (!empty($context)) {
$filters['context'] = $context;
}
$response = $this->coreApiService->call(
CoreApiService::CALLER_TYPE_PUBLIC,
'GET',
$route,
$url,
['Accept' => 'application/json', 'Content-Type' => 'application/json'],
$filters
);
$this->error = $response->status;
if ($response->status == 200) {
return json_decode(json_encode($response->body->subNetworks), true);
} else {
return [];
}
}
/**
* get subNetwork
*
* @throws \Exception
*/
public function getSubNetwork($id)
{
$filters['context'] = 'PT_SCHEDULES_LINES_V1';
$route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_SUBNETWORK;
$url = preg_replace(['/%1/', '/%2/'], [$this->networkId, $id], $route);
$response = $this->coreApiService->call(
CoreApiService::CALLER_TYPE_PUBLIC,
'GET',
$route,
$url,
['Accept' => 'application/json', 'Content-Type' => 'application/json'],
$filters
);
$this->error = $response->status;
if ($response->status == 200) {
return json_decode(json_encode($response->body->subNetworks), true);
} else {
return [];
}
}
/**
* Get subNetwork Lines
*
* @param string $id
* @param string|null $context
* @return array|mixed
* @throws \Exception
*/
public function getSubNetworkLines(string $id, ?string $context = self::CONTEXT_PT_SCHEDULES_LINES_V1, int $scholar = 0): array
{
$filters = [];
if (!empty($context)) {
$filters['context'] = $context;
}
$filters['scholar'] = $scholar;
$route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_SUBNETWORK_LINES;
$url = preg_replace(['/%1/', '/%2/'], [$this->networkId, $id], $route);
$response = $this->coreApiService->call(
CoreApiService::CALLER_TYPE_PUBLIC,
'GET',
$route,
$url,
['Accept' => 'application/json', 'Content-Type' => 'application/json'],
$filters
);
$this->error = $response->status;
if ($response->status == 200) {
return json_decode(json_encode($response->body->lines), true);
} else {
return [];
}
}
/**
* get StopAreas
*
* @throws \Exception
*/
public function getStopAreas()
{
$filters['context'] = 'PT_SCHEDULES_LINES_V1';
$route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_STOPAREAS;
$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'],
$filters
);
$this->error = $response->status;
if ($response->status == 200) {
return json_decode(json_encode($response->body->stopAreas), true);
} else {
return [];
}
}
/**
* get Localities
*
* @throws \Exception
*/
public function getLocalities()
{
$filters['context'] = 'PT_SCHEDULES_LINES_V1';
$route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_LOCALITIES;
$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'],
$filters
);
$this->error = $response->status;
if ($response->status == 200) {
return json_decode(json_encode($response->body->localities), true);
} else {
return [];
}
}
/**
* Get the disruption layout
*
* @return array
* @throws \Exception
*/
public function getDisruptionLayout() {
$this->getNetwork();
return $this->networkData['layouts']['disruption'];
}
public function getUseSectionsInPlacesSearch () : bool
{
$this->getNetwork();
return $this->networkData['layouts']['useSectionsInPlacesSearch'] ?? true;
}
public function getUseLatencyInPlacesSearch () : bool
{
$this->getNetwork();
return $this->networkData['layouts']['useLatencyInPlacesSearch'] ?? true;
}
/**
* Get proximities layout
*
* @return array
* @throws \Exception
*/
public function getProximitiesLayout() {
$this->getNetwork();
return $this->networkData['layouts']['proximities'];
}
/**
* Get webDynamicMap layout
*
* @return array
* @throws \Exception
*/
public function getWebDynamicMapLayout() {
$this->getNetwork();
if (!isset($this->networkData['layouts']['webDynamicMap'])) {
return [];
}
return $this->networkData['layouts']['webDynamicMap'];
}
}