feat(27-shipment-tracking-backend): infrastruktura sledzenia przesylek — statusy, tracking services, cron handler
Dwupoziomowy system statusow dostawy (normalized + raw z API), implementacje trackingu dla InPost ShipX, Apaczka i Allegro WZA, cron handler odpytujacy aktywne przesylki co 15 minut. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
53
src/Modules/Shipments/AllegroTrackingService.php
Normal file
53
src/Modules/Shipments/AllegroTrackingService.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Shipments;
|
||||
|
||||
use App\Modules\Settings\AllegroApiClient;
|
||||
use App\Modules\Settings\AllegroTokenManager;
|
||||
use Throwable;
|
||||
|
||||
final class AllegroTrackingService implements ShipmentTrackingInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly AllegroApiClient $apiClient,
|
||||
private readonly AllegroTokenManager $tokenManager
|
||||
) {
|
||||
}
|
||||
|
||||
public function supports(string $provider): bool
|
||||
{
|
||||
return $provider === 'allegro_wza';
|
||||
}
|
||||
|
||||
public function getDeliveryStatus(array $package): ?array
|
||||
{
|
||||
$shipmentId = trim((string) ($package['shipment_id'] ?? ''));
|
||||
if ($shipmentId === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->fetchStatus($shipmentId);
|
||||
}
|
||||
|
||||
private function fetchStatus(string $shipmentId): ?array
|
||||
{
|
||||
try {
|
||||
[$accessToken, $env] = $this->tokenManager->resolveToken();
|
||||
$details = $this->apiClient->getShipmentDetails($env, $accessToken, $shipmentId);
|
||||
|
||||
$rawStatus = strtoupper(trim((string) ($details['status'] ?? '')));
|
||||
if ($rawStatus === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return [
|
||||
'status' => DeliveryStatus::normalize('allegro_wza', $rawStatus),
|
||||
'status_raw' => $rawStatus,
|
||||
'description' => DeliveryStatus::description('allegro_wza', $rawStatus),
|
||||
];
|
||||
} catch (Throwable) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user