<?php
namespace App\Manager;
use App\Denormalizer\Place\FavoriteDenormalizer as PlaceFavoriteDenormalizer;
use App\Normalizer\DisruptionNormalizer;
use App\Normalizer\Line\FavoriteNormalizer as LineFavoriteNormalizer;
use App\Normalizer\Place\FavoriteNormalizer as PlaceFavoriteNormalizer;
use App\Normalizer\Schedule\FavoriteNormalizer as ScheduleFavoriteNormalizer;
use App\Service\Internal\DisruptionService;
use App\Service\Internal\FavoriteService;
use App\Service\Internal\Line;
use App\Service\Internal\Place;
use App\Service\Internal\Schedule;
class FavoritesManager
{
public const CONTEXT_MULTIPLE_LINES = 'MULTIPLE_LINES';
public const CONTEXT_LINE = 'LINE';
public const CONTEXT_STOPPOINT = 'STOPPOINT';
public const CONTEXT_TRAFFIC = 'TRAFFIC';
public const ACTION_ADD = 'add';
public const ACTION_DELETE = 'delete';
public const FAVORITES_TOKEN_KEY = [
self::ACTION_ADD => [
self::CONTEXT_MULTIPLE_LINES => 'favorite_add_multiple_lines',
self::CONTEXT_LINE => 'favorite_add_line',
self::CONTEXT_STOPPOINT => 'favorite_add_schedule',
self::CONTEXT_TRAFFIC => 'favorite_add_traffic'
],
self::ACTION_DELETE => [
self::CONTEXT_LINE => 'favorite_delete_line',
self::CONTEXT_STOPPOINT => 'favorite_delete_schedule',
self::CONTEXT_TRAFFIC => 'favorite_delete_traffic'
]
];
/**
* @var Line\FavoriteService
*/
private $lineFavoriteService;
/**
* @var Schedule\FavoriteService
*/
private $scheduleFavoriteService;
/**
* @var Place\FavoriteService
*/
private $placeFavoriteService;
/**
* @var DisruptionService
*/
private $disruptionService;
/**
* @var PlaceFavoriteDenormalizer
*/
private $placeFavoriteDenormalizer;
/**
* @var PlaceFavoriteNormalizer
*/
private $placeFavoriteNormalizer;
/**
* @var LineFavoriteNormalizer
*/
private $lineFavoriteNormalizer;
/**
* @var ScheduleFavoriteNormalizer
*/
private $scheduleFavoriteNormalizer;
/**
* @var DisruptionNormalizer
*/
private $disruptionNormalizer;
public function __construct(
Line\FavoriteService $lineFavoriteService,
Schedule\FavoriteService $scheduleFavoriteService,
Place\FavoriteService $placeFavoriteService,
DisruptionService $disruptionService,
FavoriteService $favoriteService,
PlaceFavoriteDenormalizer $placeFavoriteDenormalizer,
PlaceFavoriteNormalizer $placeFavoriteNormalizer,
LineFavoriteNormalizer $lineFavoriteNormalizer,
ScheduleFavoriteNormalizer $scheduleFavoriteNormalizer,
DisruptionNormalizer $disruptionNormalizer
) {
$this->lineFavoriteService = $lineFavoriteService;
$this->scheduleFavoriteService = $scheduleFavoriteService;
$this->placeFavoriteService = $placeFavoriteService;
$this->disruptionService = $disruptionService;
$this->favoriteService = $favoriteService;
$this->placeFavoriteDenormalizer = $placeFavoriteDenormalizer;
$this->placeFavoriteNormalizer = $placeFavoriteNormalizer;
$this->lineFavoriteNormalizer = $lineFavoriteNormalizer;
$this->scheduleFavoriteNormalizer = $scheduleFavoriteNormalizer;
$this->disruptionNormalizer = $disruptionNormalizer;
}
/*
* Get all favorites management
*/
public function getAll(array $params = [], bool $normalize = false): array
{
$allFavorites = $this->favoriteService->getAllFavorites($params);
if ($normalize) {
foreach ($allFavorites as $type => &$favorites) {
switch ($type) {
case 'lines':
$disruptions = $this->disruptionNormalizer->normalize(
$this->disruptionService->getLinesDisruptions()
);
$favorites = $this->lineFavoriteNormalizer->normalize($favorites, $disruptions);
break;
case 'places':
$favorites = $this->placeFavoriteNormalizer->normalize($favorites);
break;
case 'schedules':
$favorites = $this->scheduleFavoriteNormalizer->normalize($favorites);
break;
}
}
}
return $allFavorites;
}
public function placesHasByType(string $type, array $places): bool
{
foreach ($places as $place) {
if (isset($place['picto']) && $place['picto'] === $type) {
return true;
}
}
return false;
}
/*
* Place Favorites management
*/
public function getPlaces(array $params = [], bool $normalize = false): array
{
$favorites = $this->placeFavoriteService->getFavoritePlaces($params);
if ($normalize) {
$favorites = $this->placeFavoriteNormalizer->normalize($favorites);
}
return $favorites;
}
public function postPlace(array $body): array
{
return $this->placeFavoriteService->postFavorite($this->placeFavoriteDenormalizer->denormalize($body));
}
public function updatePlace(int $id, array $body): array
{
return $this->placeFavoriteService->updateFavorite($id, $this->placeFavoriteDenormalizer->denormalize($body));
}
public function deletePlace(int $id): array
{
return $this->placeFavoriteService->deleteFavorite($id);
}
/**
* Lines Favorites Management
*/
public function getLines(array $params = [], bool $normalize = false): array
{
$lines = $this->lineFavoriteService->getFavoriteLines($params);
if ($normalize) {
$disruptions = $this->disruptionNormalizer->normalize($this->disruptionService->getLinesDisruptions());
$lines = $this->lineFavoriteNormalizer->normalize($lines, $disruptions);
}
return $lines;
}
public function postLine(string $body): array
{
return $this->lineFavoriteService->postLine($body);
}
public function deleteLine(int $id): array
{
return $this->lineFavoriteService->deleteFavoriteLine($id);
}
/*
* Schedules Favorites Management
*/
public function getSchedules(array $params = [], bool $normalize = false): array
{
$schedules = $this->scheduleFavoriteService->getScheduleFavorites($params);
if ($normalize) {
$schedules = $this->scheduleFavoriteNormalizer->normalize($schedules);
}
return $schedules;
}
public function deleteSchedule(int $id): array
{
return $this->scheduleFavoriteService->deleteFavorite($id);
}
public function postScheduleLineStoppointDirection(string $body): array
{
return $this->scheduleFavoriteService->postLineStoppointDirection($body);
}
public function getTokenKeyFromContext(string $context, string $action): string
{
return self::FAVORITES_TOKEN_KEY[$action][$context] ?? '';
}
}