Skip to content

Commit

Permalink
Merge branch 'develop' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKrisKrisu committed Dec 5, 2021
2 parents 6146538 + c612f20 commit 0e5e758
Show file tree
Hide file tree
Showing 25 changed files with 304 additions and 260 deletions.
32 changes: 2 additions & 30 deletions app/Helpers/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function number($number, $decimals = 2) {
* Calculate hours and minutes from a given duration in seconds.
*
* @param int $seconds How long in seconds?
*
* @return array with `hours`, `minutes` and `showHours`.
* @deprecated as soon as vue is implemented
*/
Expand All @@ -47,6 +48,7 @@ function secondsToDuration($seconds): array {

/**
* @param array $duration from the secondsToDuration
*
* @return string
* @deprecated as soon as vue is implemented
*/
Expand All @@ -59,33 +61,3 @@ function durationToSpan($duration): string {

return $return;
}

/**
* @param $name
* @param string $classes
* @return string
* @deprecated as soon as vue is implemented
*/
function stationLink($name, $classes = "text-trwl clearfix"): string {
$urlname = $name;

switch ($name) {
// Those are stations that you can ride to but you can't search for them.
case $name == "Köln Messe/Deutz Gl. 9-10":
case $name == "Köln Messe/Deutz Gl.11-12":
$urlname = "Köln Messe/Deutz";
break;

// Hamburg's Landungsbrücken has three bridges [1..3], but you cannot search for them.
case preg_match('/Landungsbr.*cken Br.*cke \d/i', $name) > 0:
$urlname = "Landungsbrücken, Hamburg";
break;
}

return strtr('<a href=":href?provider=train&station=:station" class=":classes">:name</a>', [
':href' => route('trains.stationboard'),
':station' => urlencode($urlname),
':classes' => $classes,
':name' => $name
]);
}
6 changes: 3 additions & 3 deletions app/Http/Controllers/API/TransportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public function TrainStationboard(Request $request): JsonResponse {

try {
$trainStationboardResponse = TransportBackend::getDepartures(
$validated['station'],
isset($validated['when']) ? Carbon::parse($validated['when']) : null,
$validated['travelType'] ?? null
stationQuery: $validated['station'],
when: isset($validated['when']) ? Carbon::parse($validated['when']) : null,
travelType: $validated['travelType'] ?? null
);
} catch (HafasException $exception) {
return $this->sendError($exception->getMessage(), 503);
Expand Down
12 changes: 5 additions & 7 deletions app/Http/Controllers/API/v1/TransportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;

class TransportController extends ResponseController
{
Expand All @@ -33,7 +32,6 @@ class TransportController extends ResponseController
* @param string $name
*
* @return JsonResponse
* @throws ValidationException
* @see All slashes (as well as encoded to %2F) in $name need to be replaced, preferrably by a spache (%20)
*/
public function departures(Request $request, string $name): JsonResponse {
Expand All @@ -44,9 +42,9 @@ public function departures(Request $request, string $name): JsonResponse {

try {
$trainStationboardResponse = TransportBackend::getDepartures(
$name,
isset($validated['when']) ? Carbon::parse($validated['when']) : null,
$validated['travelType'] ?? null
stationQuery: $name,
when: isset($validated['when']) ? Carbon::parse($validated['when']) : null,
travelType: $validated['travelType'] ?? null
);
} catch (HafasException) {
return $this->sendv1Error("There has been an error with our data provider", 400);
Expand Down Expand Up @@ -115,8 +113,8 @@ public function create(Request $request): JsonResponse {
'tweet' => ['nullable', 'boolean'],
'toot' => ['nullable', 'boolean'],
'ibnr' => ['nullable', 'boolean'],
'tripId' => 'required',
'lineName' => 'required',
'tripId' => ['required'],
'lineName' => ['required'],
'start' => ['required', 'numeric'],
'destination' => ['required', 'numeric'],
'departure' => ['required', 'date'],
Expand Down
43 changes: 43 additions & 0 deletions app/Http/Controllers/Backend/Transport/StationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Http\Controllers\Backend\Transport;

use App\Exceptions\HafasException;
use App\Http\Controllers\Controller;
use App\Http\Controllers\HafasController;
use App\Models\TrainStation;
use Illuminate\Database\Eloquent\ModelNotFoundException;

abstract class StationController extends Controller
{

/**
* @throws HafasException
* @throws ModelNotFoundException
*/
public static function lookupStation(string|int $query): TrainStation {
//Lookup by station ibnr
if (is_numeric($query)) {
$station = TrainStation::where('ibnr', $query)->first();
if ($station !== null) {
return $station;
}
}

//Lookup by ril identifier
if (!is_numeric($query) && strlen($query) <= 5 && ctype_upper($query)) {
$station = HafasController::getTrainStationByRilIdentifier($query);
if ($station !== null) {
return $station;
}
}

//Lookup HAFAS
$station = HafasController::getStations(query: $query, results: 1)->first();
if ($station !== null) {
return $station;
}

throw new ModelNotFoundException;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,7 @@ public static function createTrainCheckin(

foreach ($alsoOnThisConnection as $otherStatus) {
if ($otherStatus?->user) {
$otherStatus->user->notify(new UserJoinedConnection(
statusId: $status->id,
linename: $trip->linename,
origin: $firstStop->name,
destination: $lastStop->name
));
$otherStatus->user->notify(new UserJoinedConnection($status));
}
}

Expand Down
9 changes: 5 additions & 4 deletions app/Http/Controllers/FrontendStatusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Http\Controllers\Backend\User\DashboardController;
use App\Http\Controllers\StatusController as StatusBackend;
use App\Models\Status;
use App\Models\TrainStation;
use Carbon\Carbon;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Database\Eloquent\ModelNotFoundException;
Expand Down Expand Up @@ -177,16 +178,16 @@ public function getStatus($statusId): Renderable {
/**
* @param $status
*
* @return mixed
* @return TrainStation|null
* @deprecated when vue is implemented
*/
public static function nextStation(&$status) {
public static function nextStation(&$status): ?TrainStation {
if ($status->trainCheckin->HafasTrip->stopoversNEW->count() > 0) {
return $status->trainCheckin->HafasTrip->stopoversNEW
->filter(function($stopover) {
return $stopover->arrival->isFuture();
})->sortBy('arrival')
->first()?->trainStation?->name;
->first()?->trainStation;
}

$stops = json_decode($status->trainCheckin->HafasTrip->stopovers);
Expand All @@ -201,6 +202,6 @@ public static function nextStation(&$status) {
}
break; // Wenn wir diesen Teil der Loop erreichen, kann die Loop beendert werden.
}
return $stops[$nextStopIndex]->stop->name;
return $stops[$nextStopIndex]->stop;
}
}
10 changes: 5 additions & 5 deletions app/Http/Controllers/FrontendTransportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function TrainAutocomplete(string $station): JsonResponse {
public function TrainStationboard(Request $request): Renderable|RedirectResponse {

$validated = $request->validate([
'station' => ['required', 'string'],
'station' => ['required'],
'when' => ['nullable', 'date'],
'travelType' => ['nullable', Rule::in(TravelType::getList())]
]);
Expand All @@ -46,9 +46,9 @@ public function TrainStationboard(Request $request): Renderable|RedirectResponse

try {
$TrainStationboardResponse = TransportBackend::getDepartures(
$validated['station'],
$when,
$validated['travelType'] ?? null
stationQuery: $validated['station'],
when: $when,
travelType: $validated['travelType'] ?? null
);
} catch (HafasException $exception) {
return back()->with('error', $exception->getMessage());
Expand Down Expand Up @@ -84,7 +84,7 @@ public function StationByCoordinates(Request $request): RedirectResponse {
}

return redirect()->route('trains.stationboard', [
'station' => $nearestStation->name,
'station' => $nearestStation->ibnr,
'provider' => 'train'
]);
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/HafasController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ abstract class HafasController extends Controller

public static function getTrainStationByRilIdentifier(string $rilIdentifier): ?TrainStation {
$trainStation = TrainStation::where('rilIdentifier', $rilIdentifier)->first();
if ($trainStation != null) {
if ($trainStation !== null) {
return $trainStation;
}
try {
Expand Down
30 changes: 11 additions & 19 deletions app/Http/Controllers/TransportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\Http\Controllers\Backend\Social\MastodonController;
use App\Http\Controllers\Backend\Social\TwitterController;
use App\Http\Controllers\Backend\Transport\PointsCalculationController;
use App\Http\Controllers\Backend\Transport\StationController;
use App\Http\Resources\HafasTripResource;
use App\Models\Event;
use App\Models\HafasTrip;
Expand Down Expand Up @@ -51,7 +52,7 @@ public static function getTrainStationAutocomplete(string $query): Collection {
}

/**
* @param string $stationName
* @param string|int $stationQuery
* @param Carbon|null $when
* @param string|null $travelType
*
Expand All @@ -60,22 +61,16 @@ public static function getTrainStationAutocomplete(string $query): Collection {
* @api v1
*/
#[ArrayShape([
'station' => "\App\Models\TrainStation|mixed|null",
'departures' => "\Illuminate\Support\Collection",
'station' => TrainStation::class,
'departures' => Collection::class,
'times' => "array"
])]
public static function getDepartures(string $stationName, Carbon $when = null, string $travelType = null): array {
//first check if the query is a valid DS100 identifier
if (strlen($stationName) <= 5 && ctype_upper($stationName)) {
$station = HafasController::getTrainStationByRilIdentifier($stationName);
}
//if we cannot find any station by DS100 identifier continue to search normal
if (empty($station)) {
$station = HafasController::getStations($stationName)->first();
if ($station == null) {
throw new ModelNotFoundException;
}
}
public static function getDepartures(
string|int $stationQuery,
Carbon $when = null,
string $travelType = null
): array {
$station = StationController::lookupStation($stationQuery);

$when = $when ?? Carbon::now()->subMinutes(5);
$times = [
Expand Down Expand Up @@ -390,10 +385,7 @@ public static function TrainCheckin($tripId,

// check for other people on this train
foreach ($trainCheckin->alsoOnThisConnection as $otherStatus) {
$otherStatus->user->notify(new UserJoinedConnection($status->id,
$status->trainCheckin->HafasTrip->linename,
$status->trainCheckin->Origin->name,
$status->trainCheckin->Destination->name));
$otherStatus->user->notify(new UserJoinedConnection($status));
}

return [
Expand Down
Loading

0 comments on commit 0e5e758

Please sign in to comment.