Skip to content

Commit

Permalink
Change factory to builder
Browse files Browse the repository at this point in the history
  • Loading branch information
HerrLevin committed Dec 12, 2024
1 parent 1f7b750 commit 97f0d53
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 55 deletions.
5 changes: 2 additions & 3 deletions app/Console/Commands/RefreshCurrentTrips.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions app/DataProviders/DataProviderBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\DataProviders;

class DataProviderBuilder
{
public function build(): DataProviderInterface {
return new Hafas();
}
}
28 changes: 0 additions & 28 deletions app/DataProviders/DataProviderFactory.php

This file was deleted.

8 changes: 4 additions & 4 deletions app/DataProviders/HafasStopoverService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ class HafasStopoverService
/**
* @template T of DataProviderInterface
* @param class-string<T> $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 {
Expand Down
5 changes: 2 additions & 3 deletions app/Http/Controllers/API/v1/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
7 changes: 3 additions & 4 deletions app/Http/Controllers/Frontend/Admin/CheckinController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Frontend/Admin/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 = [
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/TransportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,7 +25,7 @@ class TransportController extends Controller
* @param class-string<T> $dataProvider
*/
public function __construct(string $dataProvider) {
$this->dataProvider = (new DataProviderFactory())->create($dataProvider);
$this->dataProvider = (new DataProviderBuilder())->build($dataProvider);
}

/**
Expand Down
5 changes: 2 additions & 3 deletions app/Repositories/CheckinHydratorRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
5 changes: 2 additions & 3 deletions tests/Feature/StationSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}


Expand Down
5 changes: 2 additions & 3 deletions tests/Feature/Transport/BackendCheckinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 97f0d53

Please sign in to comment.