Files
fewo-stommer/tests/Support/Process/FakeApiConnector.php
T

59 lines
1.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace Tests\Support\Process;
use App\Channels\Contracts\ChannelConnector;
use App\Channels\Contracts\DeliversEvents;
use App\Channels\Events\IncomingBooking;
use App\Channels\Events\PaymentReported;
use App\Channels\Events\Provenance;
use App\Models\Channel;
use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\Http;
/**
* N7: a portal interface as it could come later – only an adapter and its registry entry. It reads the portal's
* bookings over HTTP (faked in the test) and delivers normalised channel events; it decides nothing.
*/
final class FakeApiConnector implements ChannelConnector, DeliversEvents
{
public static function key(): string
{
return 'fake_api';
}
public static function label(): string
{
return 'Beispiel-Schnittstelle (Test)';
}
public static function settingsSchema(): array
{
return [];
}
public function canImport(Channel $channel): bool
{
return false;
}
public function fetchOccupations(Channel $channel): array
{
return [];
}
public function fetchEvents(Channel $channel, ?CarbonImmutable $since): array
{
$events = [];
foreach ((array) Http::get('https://api.portal.example/bookings')->json('bookings', []) as $row) {
$provenance = new Provenance($channel, 'api', Provenance::AUTHENTICATED, (string) $row['reference'], 'fake-api:'.$row['reference'], CarbonImmutable::now());
$events[] = new IncomingBooking($provenance, CarbonImmutable::parse($row['arrival']), CarbonImmutable::parse($row['departure']), $row['guests'] ?? null);
if (isset($row['paid'])) {
$events[] = new PaymentReported($provenance, (int) $row['paid'], CarbonImmutable::parse($row['paid_on']));
}
}
return $events;
}
}