/**
  * @param PDO $db
  * @param string $station
  * @param string $scanDate
  */
 public function calculateTransferPatternsForStation(PDO $db, $station, $scanDate)
 {
     $db->beginTransaction();
     $insertPattern = $db->prepare("INSERT INTO transfer_pattern VALUES (null, ?, ?, ?, ?, ?)");
     $insertLegSQL = $db->prepare("INSERT INTO transfer_pattern_leg VALUES (null, ?, ?, ?)");
     $existingPatterns = $this->getExistingPatterns($db, $station);
     $treeBuilder = new ConnectionScanner($this->timetable, $this->nonTimetableConnections, $this->interchange);
     $tree = $treeBuilder->getShortestPathTree($station, 18000);
     /** @var Journey $pattern */
     foreach ($tree as $destination => $patterns) {
         foreach ($patterns as $pattern) {
             $hash = $pattern->getHash($station, $destination);
             $legs = $pattern->getTimetableLegs();
             if (isset($existingPatterns[$hash]) || count($legs) > 7 || count($legs) === 0) {
                 continue;
             }
             //                error_log("Found {$stoppingPattern}");
             $duration = $pattern->getDuration();
             $insertPattern->execute([$station, $destination, $duration, $scanDate . ' ' . gmdate("H:i", $pattern->getDepartureTime()), $scanDate]);
             $patternId = $db->lastInsertId();
             $existingPatterns[$hash] = $duration;
             foreach ($pattern->getTimetableLegs() as $leg) {
                 $insertLegSQL->execute([$patternId, $leg->getOrigin(), $leg->getDestination()]);
             }
         }
     }
     $db->commit();
 }
 /**
  * This scenario tests that the tree generator selects the journey with the latest departure time when given a choice
  * between two journeys that arrive at the same time.
  */
 public function testPatternsWithLatestDeparture()
 {
     $timetable = [new TimetableConnection("ORP", "WAE", 1000, 1020, "SE1000", "LN"), new TimetableConnection("ORP", "WAE", 1030, 1040, "SE2000", "LN"), new TimetableConnection("WAE", "CHX", 1040, 1045, "SE2000", "LN"), new TimetableConnection("ORP", "WAE", 1100, 1120, "SE3000", "LN"), new TimetableConnection("ORP", "WAE", 1130, 1140, "SE4000", "LN"), new TimetableConnection("WAE", "CHX", 1140, 1145, "SE4000", "LN")];
     $scanner = new ConnectionScanner($timetable, [], []);
     $tree = $scanner->getShortestPathTree("ORP", 900);
     $expectedTree = ["WAE" => [1020 => new Journey([new Leg([new TimetableConnection("ORP", "WAE", 1000, 1020, "SE1000", "LN")])]), 1040 => new Journey([new Leg([new TimetableConnection("ORP", "WAE", 1030, 1040, "SE2000", "LN")])]), 1120 => new Journey([new Leg([new TimetableConnection("ORP", "WAE", 1100, 1120, "SE3000", "LN")])]), 1140 => new Journey([new Leg([new TimetableConnection("ORP", "WAE", 1130, 1140, "SE4000", "LN")])])], "CHX" => [1045 => new Journey([new Leg([new TimetableConnection("ORP", "WAE", 1030, 1040, "SE2000", "LN"), new TimetableConnection("WAE", "CHX", 1040, 1045, "SE2000", "LN")])]), 1145 => new Journey([new Leg([new TimetableConnection("ORP", "WAE", 1130, 1140, "SE4000", "LN"), new TimetableConnection("WAE", "CHX", 1140, 1145, "SE4000", "LN")])])]];
     $this->assertEquals($expectedTree, $tree);
 }