feat: read BusProNet base data from the MyE&P API

This commit is contained in:
Björn Fromme authored and Björn Fromme committed 2026-09-29 11:08:04 +02:00
1 parent 1505f9b50b
commit 2e9328fc51
37 files changed
+2302 -148

No files matched your search

+155
View File
@@ -0,0 +1,155 @@
<?php
namespace App\MyEp\Import;
use App\MyEp\Api\CountryCode;
/**
* One travel date at one hotel, as the MyE&P travel export reports it. This is the
* unit the destination table is keyed on.
*/
class DestinationRow
{
public function __construct(
public readonly int $busProId,
public readonly string $code,
public readonly string $product,
public readonly \DateTimeImmutable $dateFrom,
public readonly \DateTimeImmutable $dateTo,
public readonly int $hotelBusProId,
public readonly string $hotelCode,
public readonly string $hotelName,
public readonly ?string $costUnit,
/** lowercase ISO-3166-1 alpha-2, null when MyE&P could not resolve one */
public readonly ?string $country,
public readonly array $pickups,
) {
}
/**
* @throws InvalidRowException
*/
public static function fromArray(array $data): self
{
$hotel = $data['hotel'] ?? null;
if (false === is_array($hotel)) {
throw new InvalidRowException('Travel export row without a hotel');
}
$busProId = (int) ($data['busProId'] ?? 0);
$hotelBusProId = (int) ($hotel['busProId'] ?? 0);
if (0 === $busProId || 0 === $hotelBusProId) {
throw new InvalidRowException(sprintf('Travel export row with an unusable id pair (%d, %d)', $busProId, $hotelBusProId));
}
return new self(
$busProId,
(string) ($data['code'] ?? ''),
(string) ($data['product'] ?? ''),
self::readDate($data, 'dateFrom'),
self::readDate($data, 'dateTo'),
$hotelBusProId,
(string) ($hotel['code'] ?? ''),
(string) ($hotel['name'] ?? ''),
self::readNullableString($hotel, 'costUnit'),
CountryCode::normalize($hotel['countryCode'] ?? null),
self::readPickups($data['pickups'] ?? []),
);
}
/**
* The columns an existing destination is refreshed with. The hotel name and code
* are part of it because they do change upstream, and rows that keep an outdated
* name drop out of the house matching.
*/
public function toUpdatePayload(\DateTimeImmutable $now): array
{
return [
'code' => $this->code,
'date_from' => $this->dateFrom->format('Y-m-d'),
'date_to' => $this->dateTo->format('Y-m-d'),
'product' => $this->product,
'hotel' => $this->hotelName,
'hotel_code' => $this->hotelCode,
'pickups' => json_encode($this->pickups),
'cost_unit' => $this->costUnit,
'country' => $this->country,
'updated_at' => $now->format('Y-m-d H:i:s'),
];
}
public function toInsertPayload(\DateTimeImmutable $now): array
{
return [
'code' => $this->code,
'bus_pro_id' => $this->busProId,
'date_from' => $this->dateFrom->format('Y-m-d'),
'date_to' => $this->dateTo->format('Y-m-d'),
'product' => $this->product,
'hotel' => $this->hotelName,
'hotel_code' => $this->hotelCode,
'hotel_bus_pro_id' => $this->hotelBusProId,
'pickups' => json_encode($this->pickups),
'cost_unit' => $this->costUnit,
'country' => $this->country,
'created_at' => $now->format('Y-m-d H:i:s'),
];
}
/**
* @throws InvalidRowException
*/
private static function readDate(array $data, string $key): \DateTimeImmutable
{
$value = $data[$key] ?? null;
if (false === is_string($value) || false === $date = \DateTimeImmutable::createFromFormat('!Y-m-d', $value)) {
throw new InvalidRowException(sprintf('Travel export row with an unreadable %s', $key));
}
return $date;
}
/**
* The shape here is what the destination table stores and what
* App\BusProNet\Model\Pickup::fromArray() reads back.
*/
private static function readPickups(mixed $pickups): array
{
if (false === is_array($pickups)) {
return [];
}
$mapped = [];
foreach ($pickups as $pickup) {
if (false === is_array($pickup)) {
continue;
}
$busProId = (int) ($pickup['busProId'] ?? 0);
if (0 === $busProId) {
continue;
}
$mapped[] = [
'busProId' => $busProId,
'city' => self::readNullableString($pickup, 'city'),
'street' => self::readNullableString($pickup, 'street'),
'time' => self::readNullableString($pickup, 'time'),
];
}
return $mapped;
}
private static function readNullableString(array $data, string $key): ?string
{
$value = $data[$key] ?? null;
return null === $value ? null : (string) $value;
}
}
+11
View File
@@ -0,0 +1,11 @@
<?php
namespace App\MyEp\Import;
/**
* A single unusable row of the travel export. Thrown per row so the import can skip
* it and carry on instead of losing the whole run.
*/
class InvalidRowException extends \RuntimeException
{
}
+79
View File
@@ -0,0 +1,79 @@
<?php
namespace App\MyEp\Import;
use App\MyEp\Api\ApiClient;
use App\MyEp\Api\ApiException;
use Psr\Log\LoggerInterface;
/**
* Reads the full travel export from MyE&P, one page at a time.
*/
class TravelExportClient
{
private const PATH = 'api/travels/export';
private const PER_PAGE = 500;
/**
* Stops a malformed meta block from turning the import into an endless loop.
*/
private const MAX_PAGES = 100;
public function __construct(
private readonly ApiClient $apiClient,
private readonly LoggerInterface $logger,
) {
}
/**
* @return DestinationRow[]
*
* @throws ApiException
*/
public function fetchAll(): array
{
$rows = [];
$page = 1;
$totalPages = 1;
do {
$payload = $this->apiClient->get(self::PATH, [
'page' => $page,
'perPage' => self::PER_PAGE,
]);
$data = $payload['data'] ?? null;
if (false === is_array($data)) {
throw new ApiException('The MyE&P travel export did not contain a data list');
}
foreach ($data as $item) {
if (false === is_array($item)) {
continue;
}
try {
$rows[] = DestinationRow::fromArray($item);
} catch (InvalidRowException $e) {
$this->logger->warning('Skipped an unusable travel export row', [
'reason' => $e->getMessage(),
'row' => $item,
]);
}
}
$totalPages = max(1, (int) ($payload['meta']['totalPages'] ?? 1));
++$page;
} while ($page <= $totalPages && $page <= self::MAX_PAGES && [] !== $data);
if ($totalPages > self::MAX_PAGES) {
$this->logger->error('The MyE&P travel export reported more pages than are read', [
'total_pages' => $totalPages,
'max_pages' => self::MAX_PAGES,
]);
}
return $rows;
}
}