batteryincluded/batteryincluded-php-sdk
PHP 8.2+ SDK for the BatteryIncluded API. Install via Composer and use provided examples to call endpoints. Includes DTOs for syncing products and supports extending ProductBaseDto to send custom shop fields (e.g., keywords, material, color) in payloads.
ProductBaseDto extension pattern allows customization for domain-specific fields (e.g., keywords, material), which aligns well with Laravel’s dependency injection and service container.CurlHttpClient) and provides structured DTOs for payloads, reducing boilerplate for API interactions. This fits Laravel’s service-oriented architecture.PRODUCT, BLOG) in a single collection is a strong fit for Laravel apps with diverse content models (e.g., Shopify + CMS hybrids).GuzzleHttp or Symfony HTTP Client can replace CurlHttpClient with minimal refactoring (adapters exist for both).ApiClient and SyncService can be registered as Laravel services, enabling dependency injection and configuration via config/services.php.Illuminate\Support\Collection or custom value objects can wrap SDK DTOs for type safety and immutability.queue:work can handle sync operations asynchronously (e.g., syncFullElements() triggered by model events).ModelCreated, ModelUpdated) can trigger SDK syncs, enabling real-time updates._PRODUCT prefix, type field). Migrating to another search service would require rewriting DTOs and sync logic.try-catch in services) will need to map SDK errors to Laravel’s Problem or custom exceptions.syncFullElements() method supports multiple documents per request, but Laravel’s queue system should batch operations to avoid API rate limits.BrowseService) may introduce latency if the collection grows large. Laravel’s caching (e.g., Redis) can mitigate this by storing frequent queries.composer require php:^8.2 to enforce compatibility.config/services.php suffice for storing credentials?spatie/laravel-queue-backend-retries)?queue:listen can poll for changes.Illuminate\Validation can supplement this for pre-sync checks.Mockery can mock ApiClient for unit tests.Monolog can integrate with the SDK’s error responses.config/services.php:
'batteryincluded' => [
'api_url' => env('BATTERYINCLUDED_API_URL'),
'collection' => env('BATTERYINCLUDED_COLLECTION'),
'api_key' => env('BATTERYINCLUDED_API_KEY'),
],
ApiClient and SyncService in a service provider:
$this->app->bind(SyncService::class, function ($app) {
$httpClient = new CurlHttpClient(); // or GuzzleHttp\Client
$apiClient = new ApiClient(
$httpClient,
config('services.batteryincluded.api_url'),
config('services.batteryincluded.collection'),
config('services.batteryincluded.api_key')
);
return new SyncService($apiClient);
});
CurlHttpClient with Laravel’s Http facade or Guzzle for consistency:
use Illuminate\Support\Facades\Http;
class LaravelHttpClient implements HttpClientInterface {
public function request(string $method, string $url, array $options): array {
return Http::withOptions(['debug' => false])
->withHeaders($options['headers'] ?? [])
->{$method}($url, $options['body'] ?? [])
->throw()
->json();
}
}
Illuminate\Database\Eloquent\Model or use value objects (e.g., spatie/laravel-data) to wrap SDK DTOs:
class Product extends Model {
public function syncToBatteryIncluded(): void {
$dto = new ProductDto($this->id);
$dto->setName($this->name);
$dto->setKeywords($this->keywords);
app(SyncService::class)->syncFullElements($dto);
}
}
Product sync).telescope to monitor API calls and errors.CurlHttpClient with Laravel’s Http client.ProductSaved).throw_if for API failures).syncFullElements) using Laravel queues.BrowseService).Laravel Horizon) for sync jobs.HttpTests or Pest to mock ApiClient and test sync logic.composer require batteryincluded/batteryincluded-php-sdk.config/services.php).ProductBaseDto for custom fields (e.g., App\Dto\ProductDto).Product::sync()).Product::saved()).BrowseService into Laravel controllers for unified search.Redis).Sentry or Laravel Log.composer update with caution (test in staging first).type fields), update Laravel’s DTOs and run migrations for existing data.config/cache can store API endpoints to simplify updates.README.md in the Laravel project detailing:
telescope to debug failed API calls.Product ID, timestamp).How can I help you explore Laravel packages today?