diff --git a/app/Console/Commands/RefreshCurrentTrips.php b/app/Console/Commands/RefreshCurrentTrips.php index 2fb29a385..11ad93fe9 100644 --- a/app/Console/Commands/RefreshCurrentTrips.php +++ b/app/Console/Commands/RefreshCurrentTrips.php @@ -2,9 +2,8 @@ namespace App\Console\Commands; -use App\DataProviders\DataProviderFactory; +use App\DataProviders\DataProviderBuilder; use App\DataProviders\DataProviderInterface; -use App\DataProviders\Hafas; use App\DataProviders\HafasStopoverService; use App\Enum\TripSource; use App\Exceptions\HafasException; @@ -20,7 +19,7 @@ class RefreshCurrentTrips extends Command private function getDataProvider(): DataProviderInterface { // Probably only HafasController is needed here, because this Command is very Hafas specific - return (new DataProviderFactory)->create(Hafas::class); + return (new DataProviderBuilder)->build(); } public function handle(): int { diff --git a/app/DataProviders/DataProviderBuilder.php b/app/DataProviders/DataProviderBuilder.php new file mode 100644 index 000000000..ff11dbecf --- /dev/null +++ b/app/DataProviders/DataProviderBuilder.php @@ -0,0 +1,10 @@ + $class - * - * @return T - * @throws InvalidArgumentException - */ - public function create(string $class) { - $this->checkClass($class); - - return new $class(); - } - - private function checkClass(string $class): void { - // check instance of DataProviderInterface - if (!class_implements($class, DataProviderInterface::class)) { - throw new InvalidArgumentException('Class must implement DataProviderInterface'); - } - } -} diff --git a/app/DataProviders/HafasStopoverService.php b/app/DataProviders/HafasStopoverService.php index 0bc726ae1..5deca73e7 100644 --- a/app/DataProviders/HafasStopoverService.php +++ b/app/DataProviders/HafasStopoverService.php @@ -14,11 +14,11 @@ class HafasStopoverService /** * @template T of DataProviderInterface * @param class-string $dataProvider - * @param DataProviderFactory|null $dataProviderFactory + * @param DataProviderBuilder|null $dataProviderFactory */ - public function __construct(string $dataProvider, ?DataProviderFactory $dataProviderFactory = null) { - $dataProviderFactory ??= new DataProviderFactory(); - $this->dataProvider = $dataProviderFactory->create($dataProvider); + public function __construct(string $dataProvider, ?DataProviderBuilder $dataProviderFactory = null) { + $dataProviderFactory ??= new DataProviderBuilder(); + $this->dataProvider = $dataProviderFactory->build($dataProvider); } public static function refreshStopovers(stdClass $rawHafas): stdClass { diff --git a/app/Http/Controllers/API/v1/Controller.php b/app/Http/Controllers/API/v1/Controller.php index f16649298..8c0ec19fe 100644 --- a/app/Http/Controllers/API/v1/Controller.php +++ b/app/Http/Controllers/API/v1/Controller.php @@ -2,9 +2,8 @@ namespace App\Http\Controllers\API\v1; -use App\DataProviders\DataProviderFactory; +use App\DataProviders\DataProviderBuilder; use App\DataProviders\DataProviderInterface; -use App\DataProviders\Hafas; use App\Models\OAuthClient; use App\Models\User; use Illuminate\Contracts\Auth\Authenticatable; @@ -103,7 +102,7 @@ class Controller extends \App\Http\Controllers\Controller public function __construct() { // todo: set data provider based on user settings - $this->dataProvider = (new DataProviderFactory())->create(Hafas::class); + $this->dataProvider = (new DataProviderBuilder())->build(); } public function sendResponse( diff --git a/app/Http/Controllers/Frontend/Admin/CheckinController.php b/app/Http/Controllers/Frontend/Admin/CheckinController.php index 6664280d4..6ecdbc5f8 100644 --- a/app/Http/Controllers/Frontend/Admin/CheckinController.php +++ b/app/Http/Controllers/Frontend/Admin/CheckinController.php @@ -2,8 +2,7 @@ namespace App\Http\Controllers\Frontend\Admin; -use App\DataProviders\DataProviderFactory; -use App\DataProviders\Hafas; +use App\DataProviders\DataProviderBuilder; use App\Enum\Business; use App\Enum\StatusVisibility; use App\Enum\TravelType; @@ -36,7 +35,7 @@ class CheckinController * @deprecated adapt admin panel to api endpoints */ public static function lookupStation(string|int $query): Station { - $dataProvider = (new DataProviderFactory)->create(Hafas::class); + $dataProvider = (new DataProviderBuilder)->build(); //Lookup by station ibnr if (is_numeric($query)) { @@ -93,7 +92,7 @@ public static function getDeprecatedDepartures( 'next' => $when->clone()->addMinutes(15) ]; - $departures = (new DataProviderFactory)->create(Hafas::class)->getDepartures( + $departures = (new DataProviderBuilder)->build()->getDepartures( station: $station, when: $when, type: $travelType, diff --git a/app/Http/Controllers/Frontend/Admin/EventController.php b/app/Http/Controllers/Frontend/Admin/EventController.php index b19c8eeb7..25efd543e 100644 --- a/app/Http/Controllers/Frontend/Admin/EventController.php +++ b/app/Http/Controllers/Frontend/Admin/EventController.php @@ -2,7 +2,7 @@ namespace App\Http\Controllers\Frontend\Admin; -use App\DataProviders\DataProviderFactory; +use App\DataProviders\DataProviderBuilder; use App\DataProviders\DataProviderInterface; use App\DataProviders\Hafas; use App\Enum\EventRejectionReason; @@ -26,7 +26,7 @@ class EventController extends Controller public function __construct(string $dataProvider = null) { $dataProvider ??= Hafas::class; - $this->dataProvider = (new DataProviderFactory())->create($dataProvider); + $this->dataProvider = (new DataProviderBuilder())->build($dataProvider); } private const VALIDATOR_RULES = [ diff --git a/app/Http/Controllers/TransportController.php b/app/Http/Controllers/TransportController.php index c4ffc8103..40f94c326 100644 --- a/app/Http/Controllers/TransportController.php +++ b/app/Http/Controllers/TransportController.php @@ -2,7 +2,7 @@ namespace App\Http\Controllers; -use App\DataProviders\DataProviderFactory; +use App\DataProviders\DataProviderBuilder; use App\DataProviders\DataProviderInterface; use App\Exceptions\HafasException; use App\Http\Resources\StationResource; @@ -25,7 +25,7 @@ class TransportController extends Controller * @param class-string $dataProvider */ public function __construct(string $dataProvider) { - $this->dataProvider = (new DataProviderFactory())->create($dataProvider); + $this->dataProvider = (new DataProviderBuilder())->build($dataProvider); } /** diff --git a/app/Repositories/CheckinHydratorRepository.php b/app/Repositories/CheckinHydratorRepository.php index d7d8d881e..a6f76e687 100644 --- a/app/Repositories/CheckinHydratorRepository.php +++ b/app/Repositories/CheckinHydratorRepository.php @@ -2,8 +2,7 @@ namespace App\Repositories; -use App\DataProviders\DataProviderFactory; -use App\DataProviders\Hafas; +use App\DataProviders\DataProviderBuilder; use App\Exceptions\HafasException; use App\Models\Event; use App\Models\Station; @@ -27,7 +26,7 @@ public function getOneStation(string $searchKey, string|int $id): ?Station { */ public function getHafasTrip(string $tripID, string $lineName): Trip { // todo: create trip IDs with a prefix, to distinguish between different data providers - $dataProvider = (new DataProviderFactory)->create(Hafas::class); + $dataProvider = (new DataProviderBuilder)->build(); if (is_numeric($tripID)) { $trip = Trip::where('id', $tripID)->where('linename', $lineName)->first(); diff --git a/tests/Feature/StationSearchTest.php b/tests/Feature/StationSearchTest.php index 339c2bbb8..04404fa66 100644 --- a/tests/Feature/StationSearchTest.php +++ b/tests/Feature/StationSearchTest.php @@ -2,9 +2,8 @@ namespace Tests\Feature; -use App\DataProviders\DataProviderFactory; +use App\DataProviders\DataProviderBuilder; use App\DataProviders\DataProviderInterface; -use App\DataProviders\Hafas; use App\Exceptions\HafasException; use App\Http\Controllers\Frontend\Admin\CheckinController; use App\Models\Station; @@ -19,7 +18,7 @@ class StationSearchTest extends FeatureTestCase public function setUp(): void { parent::setUp(); - $this->dataProvider = (new DataProviderFactory())->create(Hafas::class); + $this->dataProvider = (new DataProviderBuilder())->build(); } diff --git a/tests/Feature/Transport/BackendCheckinTest.php b/tests/Feature/Transport/BackendCheckinTest.php index 76d193e00..148ee26fc 100644 --- a/tests/Feature/Transport/BackendCheckinTest.php +++ b/tests/Feature/Transport/BackendCheckinTest.php @@ -2,9 +2,8 @@ namespace Tests\Feature\Transport; -use App\DataProviders\DataProviderFactory; +use App\DataProviders\DataProviderBuilder; use App\DataProviders\DataProviderInterface; -use App\DataProviders\Hafas; use App\Enum\TravelType; use App\Exceptions\CheckInCollisionException; use App\Exceptions\HafasException; @@ -27,7 +26,7 @@ class BackendCheckinTest extends FeatureTestCase public function setUp(): void { parent::setUp(); - $this->dataProvider = (new DataProviderFactory())->create(Hafas::class); + $this->dataProvider = (new DataProviderBuilder())->build(); } use RefreshDatabase;