$shipment->carrier]); if (! $service instanceof SupportsTracking || blank($shipment->tracking_reference)) { return 0; } return $this->record($shipment, $service->trackShipment($shipment)); } /** * @param iterable $checkpoints */ public function record(Shipment $shipment, iterable $checkpoints): int { $existing = $shipment->shipmentInfo() ->get(['status', 'occurred_at']) ->map(fn (ShipmentInfo $info) => $this->fingerprint($info->status->value, $info->occurred_at->toIso8601String())) ->flip(); $added = 0; foreach ($checkpoints as $checkpoint) { $fingerprint = $this->fingerprint($checkpoint->status->value, $checkpoint->occurredAt->toIso8601String()); if ($existing->has($fingerprint)) { continue; } $existing->put($fingerprint, true); $info = ShipmentInfo::create([ 'shipment_id' => $shipment->id, 'status' => $checkpoint->status, 'carrier_status' => $checkpoint->carrierStatus, 'message' => $checkpoint->message, 'location' => $checkpoint->location, 'occurred_at' => $checkpoint->occurredAt, 'meta' => $checkpoint->meta, ]); ShipmentStatusUpdatedByCarrier::dispatch($info); $added++; } return $added; } private function fingerprint(string $status, string $occurredAt): string { return $status.'|'.$occurredAt; } }