baks-dev/yandex-market
Laravel/PHP пакет для работы с API Yandex Market: установка через Composer, установка конфигов и ресурсов, поддержка очередей Messenger с отдельным транспортом на токен, тесты PHPUnit. Требует PHP 8.4+.
Install the Package
composer require baks-dev/yandex-market
Ensure your project uses PHP 8.4+ and Laravel 10.x (or Symfony 6.4+ if using standalone).
Publish Configuration and Assets
php artisan vendor:publish --provider="BaksDev\YandexMarket\YandexMarketServiceProvider"
php artisan baks:assets:install
This generates config files (e.g., config/yandex-market.php) and sets up default resources.
Configure Environment Variables
Add to .env:
YANDEX_MARKET_TOKEN=your_api_token_here
MESSENGER_TRANSPORT_DSN=redis://localhost:6379/0
For Laravel queues, ensure QUEUE_CONNECTION is configured (e.g., redis, database).
First Use Case: Sync Products Asynchronously Dispatch a job to sync products to Yandex Market:
use BaksDev\YandexMarket\Jobs\SyncProducts;
SyncProducts::dispatch([
'token' => 'your_token_uuid',
'products' => $productData,
]);
The job will automatically route to the configured queue (e.g., profile_name from the README).
Each Yandex Market token (e.g., OAuth2 access token) maps to a dedicated queue transport. This isolates failures and rate limits per seller.
// Configure per-token queues in config/yandex-market.php
'tokens' => [
'seller_1_uuid' => [
'queue' => 'seller_1_orders',
'retry_strategy' => [
'max_retries' => 3,
'delay' => 1000, // ms
'multiplier' => 2,
],
],
];
Use Laravel’s queue system to dispatch Yandex Market jobs. Example:
// Sync orders with custom retry logic
SyncOrders::dispatch([
'token' => 'seller_2_uuid',
'orders' => $orderData,
'priority' => 5, // Optional: Use Laravel's queue priority
])->onQueue('seller_2_orders');
For real-time operations (e.g., fetching a single product), use the client directly:
use BaksDev\YandexMarket\Client;
$client = app(Client::class)->withToken('your_token');
$product = $client->getProduct($productId);
Listen to Yandex Market events (e.g., order status updates) via Laravel’s event system:
// In EventServiceProvider
protected $listen = [
\BaksDev\YandexMarket\Events\OrderStatusUpdated::class => [
\App\Listeners\UpdateInventory::class,
],
];
SyncProducts::dispatch([
'token' => 'seller_1_uuid',
'products' => $products,
'batch_size' => 50, // Optional: Chunk products for rate limiting
]);
php artisan queue:work to process jobs.// In a listener
public function handle(OrderStatusUpdated $event) {
$order = $event->order;
// Update your system and notify the seller
}
// In app/Console/Kernel.php
protected function schedule(Schedule $schedule) {
$schedule->command('yandex-market:refresh-tokens')->daily();
}
// app/Facades/YandexMarket.php
public static function syncProducts(array $products, string $token) {
return (new SyncProducts($products, $token))->dispatch();
}
php artisan test --group=yandex-market
// app/Exceptions/Handler.php
public function report(Throwable $exception) {
if ($exception instanceof \BaksDev\YandexMarket\Exceptions\ApiException) {
\Log::error('Yandex Market API Error', ['error' => $exception->getMessage()]);
}
parent::report($exception);
}
Queue Transport Mismatch
sync driver for testing, then migrate to redis/database.Token Management
yandex_market_tokens table) with encrypted values.HasApiTokens trait for OAuth2 tokens.Rate Limiting
'retry_strategy' => [
'delay' => 2000, // Start with 2s delay
'multiplier' => 1.5, // Increase delay by 50% per retry
],
throttle middleware for synchronous calls.Webhook Delays
PollForUpdates::dispatch()->delay(now()->addMinutes(5));
PHP 8.4+ Requirements
// config/messenger.php
'debug' => env('MESSENGER_DEBUG', false),
// In app/Console/Kernel.php
$this->commands([
\BaksDev\YandexMarket\Console\FailedJobs::class,
]);
MESSENGER_TRANSPORT_DSN matches the token’s configured queue (e.g., seller_1_orders).config/yandex-market.php:
'default_retry_strategy' => [
'max_retries' => 5,
'delay' => 5000, // 5s
'multiplier' => 2,
],
Custom Jobs Extend the package’s job classes to add pre/post-processing:
// app/Jobs/CustomSyncProducts.php
public function handle() {
// Pre-process data
$result = parent::handle();
// Post-process result
return $result;
}
API Client Extensions Override the client to add middleware or logging:
// app/Providers/YandexMarketServiceProvider.php
public function register() {
$this->app->bind(Client::class, function ($app) {
$client = new \BaksDev\YandexMarket\Client($app['config']['yandex-market']);
$client->withMiddleware(new \App\Http\Middleware\LogApiCalls());
return $client;
});
}
Event Customization Publish and modify event classes:
php artisan vendor:publish --tag=yandex-market-events
Then extend app/Events/YandexMarketEvent.php.
Testing Helpers Use Laravel’s testing helpers to mock Yandex Market responses:
$response = new \Symfony\Component\HttpFoundation\Response
How can I help you explore Laravel packages today?