Installation:
composer require answear/fox-post-parcel
No additional configuration is required—just require the package in your Laravel project.
First Use Case:
Fetch a list of FoxPost parcel shops (stations) via the GetParcelShops command:
use Answear\FoxPostParcel\Command\GetParcelShops;
$getParcelShops = new GetParcelShops();
$stations = $getParcelShops->getParcelShops(); // Returns array of stations
Where to Look First:
GetParcelShops in src/Command/ is the primary entry point.src/Exception/ for ServiceUnavailable and MalformedResponse handling.https://cdn.foxpost.hu/apms.json (see release 3.0.2).Fetching Stations:
Use the GetParcelShops command in a Laravel service or controller:
public function findNearestStation(Request $request)
{
$stations = app(GetParcelShops::class)->getParcelShops();
// Filter/process stations (e.g., by distance, availability)
return response()->json($filteredStations);
}
Integration with Laravel:
public function register()
{
$this->app->singleton(GetParcelShops::class);
}
GetParcelShops::dispatch()->onQueue('foxpost');
Data Processing:
The response is an array of stations with keys like id, name, address, etc. Normalize it with Laravel’s collect():
$stations = collect($getParcelShops->getParcelShops())
->map(fn ($station) => [
'id' => $station['id'],
'formatted_address' => $station['address']['formatted'],
]);
Caching: Cache the response for 1 hour (or as needed) to avoid repeated API calls:
$stations = Cache::remember('foxpost_stations', 3600, function () {
return app(GetParcelShops::class)->getParcelShops();
});
Dependency Injection:
Inject GetParcelShops into constructors for testability:
public function __construct(private GetParcelShops $getParcelShops) {}
Custom Responses: Extend the command to add business logic:
class EnhancedGetParcelShops extends GetParcelShops
{
public function getFilteredStations(array $filters = [])
{
$stations = parent::getParcelShops();
return collect($stations)->filter($filters);
}
}
Testing: Mock the command in unit tests:
$mockCommand = $this->createMock(GetParcelShops::class);
$mockCommand->method('getParcelShops')->willReturn([...]);
$this->app->instance(GetParcelShops::class, $mockCommand);
API Unavailability:
ServiceUnavailable for Guzzle exceptions (e.g., network issues).retry helper or a queue job:
try {
$stations = $getParcelShops->getParcelShops();
} catch (ServiceUnavailable $e) {
retry(3, function () use ($getParcelShops) {
return $getParcelShops->getParcelShops();
});
}
Malformed Responses:
MalformedResponse is thrown if the API returns invalid data.try {
$stations = $getParcelShops->getParcelShops();
} catch (MalformedResponse $e) {
Log::error('FoxPost API response:', ['response' => $e->getResponse()]);
}
PHP/Symfony Version Mismatch:
2.x for Symfony 6).Empty Array Elements:
$stations = array_filter($stations, fn ($station) => !empty($station['id']));
Timeout Handling:
$this->app->bind(GetParcelShops::class, function ($app) {
$client = new Client(['timeout' => 30]);
return new GetParcelShops($client);
});
Logging:
$getParcelShops = new GetParcelShops();
$getParcelShops->setLogger($this->app->make(LoggerInterface::class));
Extension Points:
GetParcelShops to support other FoxPost APIs:
class CustomFoxPostClient extends GetParcelShops
{
protected function getApiUrl(): string
{
return 'https://custom-api.foxpost.hu/data';
}
}
Serializer to convert responses to DTOs:
$serializer = $this->app->make(SerializerInterface::class);
$stations = $serializer->deserialize($response, StationDto::class, 'json');
Performance:
@foreach(app(GetParcelShops::class)->getParcelShops() as $station)
{{ $station['name'] }}
@endforeach
How can I help you explore Laravel packages today?