Skip to content

Commit 6ee2bcd

Browse files
authored
✨ Preparations for multiple data sources (#3034)
1 parent 4a80d07 commit 6ee2bcd

24 files changed

+546
-488
lines changed

app/Console/Commands/RefreshCurrentTrips.php

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace App\Console\Commands;
44

5+
use App\DataProviders\DataProviderBuilder;
6+
use App\DataProviders\DataProviderInterface;
7+
use App\DataProviders\HafasStopoverService;
58
use App\Enum\TripSource;
69
use App\Exceptions\HafasException;
7-
use App\Http\Controllers\HafasController;
8-
use App\Models\Trip;
910
use App\Models\Checkin;
11+
use App\Models\Trip;
1012
use Illuminate\Console\Command;
1113
use PDOException;
1214

@@ -15,6 +17,11 @@ class RefreshCurrentTrips extends Command
1517
protected $signature = 'trwl:refreshTrips';
1618
protected $description = 'Refresh delay data from current active trips';
1719

20+
private function getDataProvider(): DataProviderInterface {
21+
// Probably only HafasController is needed here, because this Command is very Hafas specific
22+
return (new DataProviderBuilder)->build();
23+
}
24+
1825
public function handle(): int {
1926
$this->info('Getting trips to be refreshed...');
2027

@@ -23,22 +30,22 @@ public function handle(): int {
2330
->join('train_stopovers as origin_stopovers', 'origin_stopovers.id', '=', 'train_checkins.origin_stopover_id')
2431
->join('train_stopovers as destination_stopovers', 'destination_stopovers.id', '=', 'train_checkins.destination_stopover_id')
2532
->where(function($query) {
26-
$query->where('destination_stopovers.arrival_planned', '>=', now()->subMinutes(20))
27-
->orWhere('destination_stopovers.arrival_real', '>=', now()->subMinutes(20));
28-
})
33+
$query->where('destination_stopovers.arrival_planned', '>=', now()->subMinutes(20))
34+
->orWhere('destination_stopovers.arrival_real', '>=', now()->subMinutes(20));
35+
})
2936
->where(function($query) {
30-
$query->where('origin_stopovers.departure_planned', '<=', now()->addMinutes(20))
31-
->orWhere('origin_stopovers.departure_real', '<=', now()->addMinutes(20));
32-
})
37+
$query->where('origin_stopovers.departure_planned', '<=', now()->addMinutes(20))
38+
->orWhere('origin_stopovers.departure_real', '<=', now()->addMinutes(20));
39+
})
3340
->where(function($query) {
34-
$query->where('hafas_trips.last_refreshed', '<', now()->subMinutes(5))
35-
->orWhereNull('hafas_trips.last_refreshed');
36-
})
37-
->where('hafas_trips.source', TripSource::HAFAS->value)
38-
->select('hafas_trips.*')
39-
->distinct()
40-
->orderBy('hafas_trips.last_refreshed')
41-
->get();
41+
$query->where('hafas_trips.last_refreshed', '<', now()->subMinutes(5))
42+
->orWhereNull('hafas_trips.last_refreshed');
43+
})
44+
->where('hafas_trips.source', TripSource::HAFAS->value)
45+
->select('hafas_trips.*')
46+
->distinct()
47+
->orderBy('hafas_trips.last_refreshed')
48+
->get();
4249

4350
if ($trips->isEmpty()) {
4451
$this->warn('No trips to be refreshed');
@@ -53,8 +60,8 @@ public function handle(): int {
5360
$this->info('Refreshing trip ' . $trip->trip_id . ' (' . $trip->linename . ')...');
5461
$trip->update(['last_refreshed' => now()]);
5562

56-
$rawHafas = HafasController::fetchRawHafasTrip($trip->trip_id, $trip->linename);
57-
$updatedCounts = HafasController::refreshStopovers($rawHafas);
63+
$rawHafas = $this->getDataProvider()->fetchRawHafasTrip($trip->trip_id, $trip->linename);
64+
$updatedCounts = HafasStopoverService::refreshStopovers($rawHafas);
5865
$this->info('Updated ' . $updatedCounts->stopovers . ' stopovers.');
5966

6067
//set duration for refreshed trips to null, so it will be recalculated
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\DataProviders;
4+
5+
class DataProviderBuilder
6+
{
7+
public function build(): DataProviderInterface {
8+
return new Hafas();
9+
}
10+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace App\DataProviders;
4+
5+
use App\Enum\TravelType;
6+
use App\Models\Station;
7+
use Carbon\Carbon;
8+
9+
interface DataProviderInterface
10+
{
11+
public function fetchHafasTrip(string $tripID, string $lineName);
12+
13+
public function fetchRawHafasTrip(string $tripId, string $lineName);
14+
15+
public function getStations(string $query, int $results);
16+
17+
public function getDepartures(Station $station, Carbon $when, int $duration = 15, TravelType $type = null, bool $localtime = false);
18+
19+
public function getNearbyStations(float $latitude, float $longitude, int $results);
20+
21+
public function getStationByRilIdentifier(string $rilIdentifier);
22+
23+
public function getStationsByFuzzyRilIdentifier(string $rilIdentifier);
24+
}

app/DataProviders/FptfHelper.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace App\DataProviders;
4+
5+
use App\Enum\TravelType;
6+
7+
class FptfHelper
8+
{
9+
public static function checkTravelType(?TravelType $type, TravelType $travelType): string {
10+
return (is_null($type) || $type === $travelType) ? 'true' : 'false';
11+
}
12+
}

0 commit comments

Comments
 (0)