66 lines
2.0 KiB
PHP
66 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Channels\ICal;
|
|
|
|
use App\Channels\ChannelSyncException;
|
|
use App\Channels\Contracts\ChannelConnector;
|
|
use App\Models\Channel;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Illuminate\Http\Client\ConnectionException;
|
|
use Illuminate\Http\Client\RequestException;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
/**
|
|
* Generic calendar connection that works with almost every portal: we read the portal's iCal export
|
|
* and the portal reads our feed for this channel.
|
|
*/
|
|
class ICalConnector implements ChannelConnector
|
|
{
|
|
public function __construct(private ICalParser $parser) {}
|
|
|
|
public static function key(): string
|
|
{
|
|
return 'ical';
|
|
}
|
|
|
|
public static function label(): string
|
|
{
|
|
return 'Kalender-Abgleich (iCal)';
|
|
}
|
|
|
|
public static function settingsSchema(): array
|
|
{
|
|
return [
|
|
TextInput::make('settings.import_url')
|
|
->label('Importadresse: iCal-Export des externen Portals')
|
|
->helperText('Im Vermieterbereich des Portals unter „Kalender exportieren“. Leer lassen, wenn das Portal keinen Kalender anbietet.')
|
|
->url()
|
|
->password()
|
|
->revealable()
|
|
->maxLength(2048),
|
|
];
|
|
}
|
|
|
|
public function canImport(Channel $channel): bool
|
|
{
|
|
return filled($channel->setting('import_url'));
|
|
}
|
|
|
|
public function fetchOccupations(Channel $channel): array
|
|
{
|
|
$url = preg_replace('#^webcals?://#i', 'https://', (string) $channel->setting('import_url'));
|
|
|
|
try {
|
|
$response = Http::timeout(20)
|
|
->retry(2, 1000)
|
|
->withHeaders(['Accept' => 'text/calendar'])
|
|
->get($url)
|
|
->throw();
|
|
} catch (ConnectionException|RequestException $exception) {
|
|
throw new ChannelSyncException('Kalender konnte nicht abgerufen werden. Netzwerkverbindung und Portaladresse prüfen.', previous: $exception);
|
|
}
|
|
|
|
return $this->parser->parse($response->body());
|
|
}
|
|
}
|