src/Manager/FavoritesManager.php line 85

Open in your IDE?
  1. <?php
  2. namespace App\Manager;
  3. use App\Denormalizer\Place\FavoriteDenormalizer as PlaceFavoriteDenormalizer;
  4. use App\Normalizer\DisruptionNormalizer;
  5. use App\Normalizer\Line\FavoriteNormalizer as LineFavoriteNormalizer;
  6. use App\Normalizer\Place\FavoriteNormalizer as PlaceFavoriteNormalizer;
  7. use App\Normalizer\Schedule\FavoriteNormalizer as ScheduleFavoriteNormalizer;
  8. use App\Service\Internal\DisruptionService;
  9. use App\Service\Internal\FavoriteService;
  10. use App\Service\Internal\Line;
  11. use App\Service\Internal\Place;
  12. use App\Service\Internal\Schedule;
  13. class FavoritesManager
  14. {
  15. public const CONTEXT_MULTIPLE_LINES = 'MULTIPLE_LINES';
  16. public const CONTEXT_LINE = 'LINE';
  17. public const CONTEXT_STOPPOINT = 'STOPPOINT';
  18. public const CONTEXT_TRAFFIC = 'TRAFFIC';
  19. public const ACTION_ADD = 'add';
  20. public const ACTION_DELETE = 'delete';
  21. public const FAVORITES_TOKEN_KEY = [
  22. self::ACTION_ADD => [
  23. self::CONTEXT_MULTIPLE_LINES => 'favorite_add_multiple_lines',
  24. self::CONTEXT_LINE => 'favorite_add_line',
  25. self::CONTEXT_STOPPOINT => 'favorite_add_schedule',
  26. self::CONTEXT_TRAFFIC => 'favorite_add_traffic'
  27. ],
  28. self::ACTION_DELETE => [
  29. self::CONTEXT_LINE => 'favorite_delete_line',
  30. self::CONTEXT_STOPPOINT => 'favorite_delete_schedule',
  31. self::CONTEXT_TRAFFIC => 'favorite_delete_traffic'
  32. ]
  33. ];
  34. /**
  35. * @var Line\FavoriteService
  36. */
  37. private $lineFavoriteService;
  38. /**
  39. * @var Schedule\FavoriteService
  40. */
  41. private $scheduleFavoriteService;
  42. /**
  43. * @var Place\FavoriteService
  44. */
  45. private $placeFavoriteService;
  46. /**
  47. * @var DisruptionService
  48. */
  49. private $disruptionService;
  50. /**
  51. * @var PlaceFavoriteDenormalizer
  52. */
  53. private $placeFavoriteDenormalizer;
  54. /**
  55. * @var PlaceFavoriteNormalizer
  56. */
  57. private $placeFavoriteNormalizer;
  58. /**
  59. * @var LineFavoriteNormalizer
  60. */
  61. private $lineFavoriteNormalizer;
  62. /**
  63. * @var ScheduleFavoriteNormalizer
  64. */
  65. private $scheduleFavoriteNormalizer;
  66. /**
  67. * @var DisruptionNormalizer
  68. */
  69. private $disruptionNormalizer;
  70. public function __construct(
  71. Line\FavoriteService $lineFavoriteService,
  72. Schedule\FavoriteService $scheduleFavoriteService,
  73. Place\FavoriteService $placeFavoriteService,
  74. DisruptionService $disruptionService,
  75. FavoriteService $favoriteService,
  76. PlaceFavoriteDenormalizer $placeFavoriteDenormalizer,
  77. PlaceFavoriteNormalizer $placeFavoriteNormalizer,
  78. LineFavoriteNormalizer $lineFavoriteNormalizer,
  79. ScheduleFavoriteNormalizer $scheduleFavoriteNormalizer,
  80. DisruptionNormalizer $disruptionNormalizer
  81. ) {
  82. $this->lineFavoriteService = $lineFavoriteService;
  83. $this->scheduleFavoriteService = $scheduleFavoriteService;
  84. $this->placeFavoriteService = $placeFavoriteService;
  85. $this->disruptionService = $disruptionService;
  86. $this->favoriteService = $favoriteService;
  87. $this->placeFavoriteDenormalizer = $placeFavoriteDenormalizer;
  88. $this->placeFavoriteNormalizer = $placeFavoriteNormalizer;
  89. $this->lineFavoriteNormalizer = $lineFavoriteNormalizer;
  90. $this->scheduleFavoriteNormalizer = $scheduleFavoriteNormalizer;
  91. $this->disruptionNormalizer = $disruptionNormalizer;
  92. }
  93. /*
  94. * Get all favorites management
  95. */
  96. public function getAll(array $params = [], bool $normalize = false): array
  97. {
  98. $allFavorites = $this->favoriteService->getAllFavorites($params);
  99. if ($normalize) {
  100. foreach ($allFavorites as $type => &$favorites) {
  101. switch ($type) {
  102. case 'lines':
  103. $disruptions = $this->disruptionNormalizer->normalize(
  104. $this->disruptionService->getLinesDisruptions()
  105. );
  106. $favorites = $this->lineFavoriteNormalizer->normalize($favorites, $disruptions);
  107. break;
  108. case 'places':
  109. $favorites = $this->placeFavoriteNormalizer->normalize($favorites);
  110. break;
  111. case 'schedules':
  112. $favorites = $this->scheduleFavoriteNormalizer->normalize($favorites);
  113. break;
  114. }
  115. }
  116. }
  117. return $allFavorites;
  118. }
  119. public function placesHasByType(string $type, array $places): bool
  120. {
  121. foreach ($places as $place) {
  122. if (isset($place['picto']) && $place['picto'] === $type) {
  123. return true;
  124. }
  125. }
  126. return false;
  127. }
  128. /*
  129. * Place Favorites management
  130. */
  131. public function getPlaces(array $params = [], bool $normalize = false): array
  132. {
  133. $favorites = $this->placeFavoriteService->getFavoritePlaces($params);
  134. if ($normalize) {
  135. $favorites = $this->placeFavoriteNormalizer->normalize($favorites);
  136. }
  137. return $favorites;
  138. }
  139. public function postPlace(array $body): array
  140. {
  141. return $this->placeFavoriteService->postFavorite($this->placeFavoriteDenormalizer->denormalize($body));
  142. }
  143. public function updatePlace(int $id, array $body): array
  144. {
  145. return $this->placeFavoriteService->updateFavorite($id, $this->placeFavoriteDenormalizer->denormalize($body));
  146. }
  147. public function deletePlace(int $id): array
  148. {
  149. return $this->placeFavoriteService->deleteFavorite($id);
  150. }
  151. /**
  152. * Lines Favorites Management
  153. */
  154. public function getLines(array $params = [], bool $normalize = false): array
  155. {
  156. $lines = $this->lineFavoriteService->getFavoriteLines($params);
  157. if ($normalize) {
  158. $disruptions = $this->disruptionNormalizer->normalize($this->disruptionService->getLinesDisruptions());
  159. $lines = $this->lineFavoriteNormalizer->normalize($lines, $disruptions);
  160. }
  161. return $lines;
  162. }
  163. public function postLine(string $body): array
  164. {
  165. return $this->lineFavoriteService->postLine($body);
  166. }
  167. public function deleteLine(int $id): array
  168. {
  169. return $this->lineFavoriteService->deleteFavoriteLine($id);
  170. }
  171. /*
  172. * Schedules Favorites Management
  173. */
  174. public function getSchedules(array $params = [], bool $normalize = false): array
  175. {
  176. $schedules = $this->scheduleFavoriteService->getScheduleFavorites($params);
  177. if ($normalize) {
  178. $schedules = $this->scheduleFavoriteNormalizer->normalize($schedules);
  179. }
  180. return $schedules;
  181. }
  182. public function deleteSchedule(int $id): array
  183. {
  184. return $this->scheduleFavoriteService->deleteFavorite($id);
  185. }
  186. public function postScheduleLineStoppointDirection(string $body): array
  187. {
  188. return $this->scheduleFavoriteService->postLineStoppointDirection($body);
  189. }
  190. public function getTokenKeyFromContext(string $context, string $action): string
  191. {
  192. return self::FAVORITES_TOKEN_KEY[$action][$context] ?? '';
  193. }
  194. }