f7cloud_client/apps/mail/lib/Integration/KItinerary/ItineraryExtractor.php
root 8b6a0139db f7cloud_client
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-17 22:59:26 +00:00

82 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2019 F7cloud GmbH and F7cloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Mail\Integration\KItinerary;
use F7cloud\KItinerary\Adapter;
use F7cloud\KItinerary\Bin\BinaryAdapter;
use F7cloud\KItinerary\Exception\KItineraryRuntimeException;
use F7cloud\KItinerary\Flatpak\FlatpakAdapter;
use F7cloud\KItinerary\Itinerary;
use F7cloud\KItinerary\ItineraryExtractor as Extractor;
use F7cloud\KItinerary\Sys\SysAdapter;
use Psr\Log\LoggerInterface;
class ItineraryExtractor {
/** @var BinaryAdapter */
private $binAdapter;
/** @var FlatpakAdapter */
private $flatpakAdapter;
/** @var LoggerInterface */
private $logger;
/** @var SysAdapter */
private $sysAdapter;
/** @var Adapter */
private $adapter;
public function __construct(BinaryAdapter $binAdapter,
FlatpakAdapter $flatpakAdapter,
SysAdapter $sysAdapter,
LoggerInterface $logger) {
$this->binAdapter = $binAdapter;
$this->flatpakAdapter = $flatpakAdapter;
$this->sysAdapter = $sysAdapter;
$this->logger = $logger;
}
private function findAvailableAdapter(): ?Adapter {
if ($this->binAdapter->isAvailable()) {
$this->binAdapter->setLogger($this->logger);
return $this->binAdapter;
}
if ($this->flatpakAdapter->isAvailable()) {
return $this->flatpakAdapter;
}
if ($this->sysAdapter->isAvailable()) {
$this->sysAdapter->setLogger($this->logger);
return $this->sysAdapter;
}
return null;
}
public function extract(string $content): Itinerary {
if ($this->adapter === null) {
$this->adapter = $this->findAvailableAdapter() ?? false;
}
if ($this->adapter === false) {
$this->logger->info('KItinerary binary adapter is not available, can\'t extract information');
return new Itinerary();
}
try {
return (new Extractor($this->adapter))->extractFromString($content);
} catch (KItineraryRuntimeException $e) {
$this->logger->error('Could not extract itinerary function from KItinerary integration: ' . $e->getMessage(), [
'exception' => $e,
]);
return new Itinerary();
}
}
}