2
2
3
3
namespace App \Console \Commands ;
4
4
5
+ use App \DataProviders \DataProviderBuilder ;
6
+ use App \DataProviders \DataProviderInterface ;
7
+ use App \DataProviders \HafasStopoverService ;
5
8
use App \Enum \TripSource ;
6
9
use App \Exceptions \HafasException ;
7
- use App \Http \Controllers \HafasController ;
8
- use App \Models \Trip ;
9
10
use App \Models \Checkin ;
11
+ use App \Models \Trip ;
10
12
use Illuminate \Console \Command ;
11
13
use PDOException ;
12
14
@@ -15,6 +17,11 @@ class RefreshCurrentTrips extends Command
15
17
protected $ signature = 'trwl:refreshTrips ' ;
16
18
protected $ description = 'Refresh delay data from current active trips ' ;
17
19
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
+
18
25
public function handle (): int {
19
26
$ this ->info ('Getting trips to be refreshed... ' );
20
27
@@ -23,22 +30,22 @@ public function handle(): int {
23
30
->join ('train_stopovers as origin_stopovers ' , 'origin_stopovers.id ' , '= ' , 'train_checkins.origin_stopover_id ' )
24
31
->join ('train_stopovers as destination_stopovers ' , 'destination_stopovers.id ' , '= ' , 'train_checkins.destination_stopover_id ' )
25
32
->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
+ })
29
36
->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
+ })
33
40
->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 ();
42
49
43
50
if ($ trips ->isEmpty ()) {
44
51
$ this ->warn ('No trips to be refreshed ' );
@@ -53,8 +60,8 @@ public function handle(): int {
53
60
$ this ->info ('Refreshing trip ' . $ trip ->trip_id . ' ( ' . $ trip ->linename . ')... ' );
54
61
$ trip ->update (['last_refreshed ' => now ()]);
55
62
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 );
58
65
$ this ->info ('Updated ' . $ updatedCounts ->stopovers . ' stopovers. ' );
59
66
60
67
//set duration for refreshed trips to null, so it will be recalculated
0 commit comments