@@ -64,9 +64,6 @@ private function filterStopoversFromStatus(): ?array {
64
64
return $ newStopovers ;
65
65
}
66
66
67
- /**
68
- * @throws JsonException
69
- */
70
67
public function calculateLivePosition (): ?LivePointDto {
71
68
$ newStopovers = $ this ->filterStopoversFromStatus ();
72
69
@@ -86,43 +83,46 @@ public function calculateLivePosition(): ?LivePointDto {
86
83
$ this ->status
87
84
);
88
85
}
89
-
90
- $ now = Carbon::now ()->timestamp ;
91
- $ percentage = ($ now - $ newStopovers [0 ]->departure ->timestamp )
92
- / ($ newStopovers [1 ]->arrival ->timestamp - $ newStopovers [0 ]->departure ->timestamp );
93
- $ this ->origin = $ newStopovers [0 ];
94
- $ this ->destination = $ newStopovers [1 ];
95
- $ polyline = $ this ->getPolylineBetween (false );
96
-
97
- $ meters = $ this ->getDistanceFromGeoJson ($ polyline ) * $ percentage ;
98
- $ recentPoint = null ;
99
- $ distance = 0 ;
100
- foreach ($ polyline ->features as $ key => $ point ) {
101
- $ point = Coordinate::fromGeoJson ($ point );
102
- if ($ recentPoint !== null && $ point !== null ) {
103
- $ lineSegment = new LineSegment ($ recentPoint , $ point );
104
-
105
- $ distance += $ lineSegment ->calculateDistance ();
106
- if ($ distance >= $ meters ) {
107
- break ;
86
+ try {
87
+ $ now = Carbon::now ()->timestamp ;
88
+ $ percentage = ($ now - $ newStopovers [0 ]->departure ->timestamp )
89
+ / ($ newStopovers [1 ]->arrival ->timestamp - $ newStopovers [0 ]->departure ->timestamp );
90
+ $ this ->origin = $ newStopovers [0 ];
91
+ $ this ->destination = $ newStopovers [1 ];
92
+ $ polyline = $ this ->getPolylineBetween (false );
93
+
94
+ $ meters = $ this ->getDistanceFromGeoJson ($ polyline ) * $ percentage ;
95
+ $ recentPoint = null ;
96
+ $ distance = 0 ;
97
+ foreach ($ polyline ->features as $ key => $ point ) {
98
+ $ point = Coordinate::fromGeoJson ($ point );
99
+ if ($ recentPoint !== null && $ point !== null ) {
100
+ $ lineSegment = new LineSegment ($ recentPoint , $ point );
101
+
102
+ $ distance += $ lineSegment ->calculateDistance ();
103
+ if ($ distance >= $ meters ) {
104
+ break ;
105
+ }
108
106
}
107
+ $ recentPoint = $ point ?? $ recentPoint ;
109
108
}
110
- $ recentPoint = $ point ?? $ recentPoint ;
111
- }
112
109
113
- $ currentPosition = $ lineSegment ->interpolatePoint ($ meters / $ distance );
110
+ $ currentPosition = $ lineSegment ->interpolatePoint ($ meters / $ distance );
114
111
115
- $ polyline ->features = array_slice ($ polyline ->features , $ key );
116
- array_unshift ($ polyline ->features , Feature::fromCoordinate ($ currentPosition ));
112
+ $ polyline ->features = array_slice ($ polyline ->features , $ key );
113
+ array_unshift ($ polyline ->features , Feature::fromCoordinate ($ currentPosition ));
117
114
118
- return new LivePointDto (
119
- null ,
120
- $ polyline ,
121
- $ newStopovers [1 ]->arrival ->timestamp ,
122
- $ newStopovers [1 ]->departure ->timestamp ,
123
- $ this ->trip ->linename ,
124
- $ this ->status ,
125
- );
115
+ return new LivePointDto (
116
+ null ,
117
+ $ polyline ,
118
+ $ newStopovers [1 ]->arrival ->timestamp ,
119
+ $ newStopovers [1 ]->departure ->timestamp ,
120
+ $ this ->trip ->linename ,
121
+ $ this ->status ,
122
+ );
123
+ } catch (Exception ) {
124
+ return null ;
125
+ }
126
126
}
127
127
128
128
private function getDistanceFromGeoJson (stdClass $ geoJson ): int {
0 commit comments