You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SchedulesBusiness.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: robin
  5. * Date: 10/1/16
  6. * Time: 5:32 PM
  7. */
  8. namespace App\Business;
  9. use App\DataAccess\SchedulesDataAccess;
  10. use App\Dbo\StopsLiteRoutesLiteDbo;
  11. use App\Dbo\StopsLiteRoutesLiteDboArray;
  12. use App\Dbo\StopsSchedulesDbo;
  13. use Carbon\Carbon;
  14. use GuzzleHttp\Exception\ClientException;
  15. use Luticate\Utils\Dbo\LuPaginatedDbo;
  16. class SchedulesBusiness
  17. {
  18. public static $days = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"];
  19. public static $dateFormat = "Y-m-d";
  20. public static $timeFormat = "H:i:s";
  21. protected static function getDataAccess()
  22. {
  23. return new SchedulesDataAccess();
  24. }
  25. public static function getScheduleTypeForDate(array $config, Carbon $date)
  26. {
  27. foreach ($config["scheduleTypes"] as $scheduleType) {
  28. if ($scheduleType["type"] == static::$days[$date->dayOfWeek]) {
  29. $from = Carbon::createFromFormat(static::$dateFormat, $scheduleType["dateFrom"]);
  30. $to = Carbon::createFromFormat(static::$dateFormat, $scheduleType["dateTo"]);
  31. if ($date->between($from, $to)) {
  32. return $scheduleType;
  33. }
  34. }
  35. }
  36. foreach ($config["scheduleTypes"] as $scheduleType) {
  37. if ($scheduleType["type"] == static::$days[$date->dayOfWeek]) {
  38. return $scheduleType;
  39. }
  40. }
  41. foreach ($config["scheduleTypes"] as $scheduleType) {
  42. if ($scheduleType["type"] == "week") {
  43. $from = Carbon::createFromFormat(static::$dateFormat, $scheduleType["dateFrom"]);
  44. $to = Carbon::createFromFormat(static::$dateFormat, $scheduleType["dateTo"]);
  45. if ($date->between($from, $to)) {
  46. return $scheduleType;
  47. }
  48. }
  49. }
  50. foreach ($config["scheduleTypes"] as $scheduleType) {
  51. if ($scheduleType["type"] == "week") {
  52. return $scheduleType;
  53. }
  54. }
  55. return $config["scheduleTypes"][0];
  56. }
  57. public static function getSchedulesForDate(array $schedules, Carbon $date)
  58. {
  59. foreach ($schedules as $schedule) {
  60. if (in_array($date->dayOfWeek, $schedule["week_days"])) {
  61. return $schedule["schedule"];
  62. }
  63. }
  64. return null;
  65. }
  66. public static function getSingle(array $scheduleType, StopsLiteRoutesLiteDbo $stop, Carbon $date, int $count)
  67. {
  68. $dbo = new StopsSchedulesDbo();
  69. $dbo->setRouteId($stop->getRouteId());
  70. $dbo->setStopId($stop->getStopId());
  71. try {
  72. $data = static::getDataAccess()->get($scheduleType["resourceId"], "allweek", $stop->getStopId(), $stop->getRouteId());
  73. }
  74. catch (ClientException $e) {
  75. if ($e->getCode() == 404) {
  76. $dbo->setSchedules([]);
  77. return $dbo;
  78. }
  79. else {
  80. throw $e;
  81. }
  82. }
  83. $schedulesCarbon = [];
  84. if (!is_null($data["data"]["schedules"])) {
  85. $schedules = static::getSchedulesForDate($data["data"]["schedules"], $date);
  86. for ($i = 0; $i < count($schedules) && $count > 0; ++$i) {
  87. $scheduleDate = Carbon::createFromFormat(static::$timeFormat . " " . static::$dateFormat, $schedules[$i] . " " . $date->toDateString());
  88. if ($scheduleDate->gte($date)) {
  89. --$count;
  90. $schedulesCarbon[] = $scheduleDate;
  91. }
  92. }
  93. }
  94. $dbo->setSchedules($schedulesCarbon);
  95. return $dbo;
  96. }
  97. public static function getMultiple(StopsLiteRoutesLiteDboArray $stops, Carbon $date, int $count)
  98. {
  99. $config = MiscBusiness::getConfig();
  100. $scheduleType = static::getScheduleTypeForDate($config, $date);
  101. $dbos = [];
  102. foreach ($stops->getArray() as $stop) {
  103. $dbo = static::getSingle($scheduleType, $stop, $date, $count);
  104. if (count($dbo->getSchedules()) < $count) {
  105. $count2 = $count - count($dbo->getSchedules());
  106. $date2 = $date->copy();
  107. $date2->addDay(1);
  108. $date2->setTime(0, 0, 0);
  109. $scheduleType2 = static::getScheduleTypeForDate($config, $date2);
  110. $dbo2 = static::getSingle($scheduleType2, $stop, $date2, $count2);
  111. $dbo->setSchedules(array_merge($dbo->getSchedules(), $dbo2->getSchedules()));
  112. }
  113. $dbos[] = $dbo;
  114. }
  115. return new LuPaginatedDbo(count($dbos), $dbos);
  116. }
  117. }