Installation:
composer require baks-dev/yandex-market
php artisan vendor:publish --provider="BaksDev\YandexMarket\YandexMarketServiceProvider"
Publish the config file to customize API endpoints, token storage, and queue settings.
Environment Setup:
Add Yandex Market API credentials and queue configuration to .env:
YANDEX_MARKET_TOKEN=your_oauth_token_here
MESSENGER_TRANSPORT_DSN=redis://localhost:6379/1
First Use Case: Fetch a product by ID synchronously:
use BaksDev\YandexMarket\Facades\YandexMarket;
$product = YandexMarket::products()->get(12345);
dd($product);
Run Asset Installation (if needed for additional resources):
php artisan baks:assets:install
Synchronous API Calls: Use facades or service container bindings for direct API interactions:
// Via Facade
$orders = YandexMarket::orders()->list(['limit' => 10]);
// Via Service Container
$client = app(\BaksDev\YandexMarket\Contracts\YandexMarketClient::class);
$client->products()->create($productData);
Asynchronous Processing with Queues: Leverage Symfony Messenger for background tasks. Configure per-token queues:
// In a service provider or config
$messenger->transport('yandex_market_token_1')
->dsn('%env(MESSENGER_TRANSPORT_DSN)%')
->options(['queue_name' => 'yandex_market_products'])
->retryStrategy()
->maxRetries(3);
Dispatch a queued job:
use BaksDev\YandexMarket\Messages\SyncProductCatalog;
$message = new SyncProductCatalog($productIds);
$message->setToken('yandex_market_token_1');
$bus->dispatch($message);
Bulk Operations: Use batch processing for high-volume tasks (e.g., catalog syncs):
$batch = YandexMarket::products()->batch();
foreach ($products as $product) {
$batch->add($product);
}
$batch->sync(); // Queues each product for async processing
Event Listeners: Subscribe to Yandex Market webhook events (if supported) or process queue failures:
// Example: Handle failed queue jobs
public function handleFailedJob(FailedJob $event)
{
if ($event->job instanceof SyncProductCatalog) {
// Retry logic or alert
}
}
Product Catalog Sync:
YandexMarket::products()->batch()->sync() to queue updates.Order Fulfillment:
ProcessOrder message to the queue:
$message = new ProcessOrder($orderId, $fulfillmentData);
$bus->dispatch($message);
Dynamic Pricing:
$prices = YandexMarket::pricing()->get();
$message = new UpdatePricing($adjustedPrices);
$bus->dispatch($message);
Laravel Service Provider: Bind the package’s client to an interface for easier mocking in tests:
$this->app->bind(
\BaksDev\YandexMarket\Contracts\YandexMarketClient::class,
\BaksDev\YandexMarket\YandexMarketClient::class
);
Custom Transports: If using Laravel’s queue system instead of Messenger, create a custom transport:
// app/Providers/YandexMarketServiceProvider.php
public function boot()
{
$this->app->extend(
\BaksDev\YandexMarket\Contracts\YandexMarketClient::class,
function ($client) {
$client->setTransport(new LaravelQueueTransport());
return $client;
}
);
}
Testing: Use PHPUnit’s testing group to run package-specific tests:
php artisan test --group=yandex-market
Mock the HTTP client for unit tests:
$this->mock(\BaksDev\YandexMarket\Http\YandexMarketHttpClient::class)
->shouldReceive('get')
->andReturn($mockResponse);
Error Handling:
Catch YandexMarketException for API-specific errors:
try {
$product = YandexMarket::products()->get($id);
} catch (\BaksDev\YandexMarket\Exceptions\YandexMarketException $e) {
Log::error("Yandex Market API error: " . $e->getMessage());
// Retry or notify admin
}
Queue Configuration:
->options(['queue_name' => 'token_' . $tokenId])
Token Management:
.env or a secure vault (e.g., AWS Secrets Manager). Implement a token refresh mechanism:
// Example: Refresh token before expiration
$client->refreshToken();
API Rate Limits:
throttle middleware:
Route::middleware(['throttle:60,1'])->group(function () {
// Yandex Market API routes
});
Data Mismatches:
$response = YandexMarket::products()->create($data, ['idempotency_key' => uniqid()]);
Package Updates:
composer.json or monitor the GitHub repo for updates:
"baks-dev/yandex-market": "7.4.7"
Queue Debugging:
.env:
MESSENGER_DEBUG=true
php artisan queue:work --queue=yandex_market_products --verbose
API Response Logging:
$client = app(YandexMarketClient::class);
$client->setLogger(new CustomLogger());
Token Validation:
try {
$client->validateToken();
} catch (\BaksDev\YandexMarket\Exceptions\InvalidTokenException $e) {
// Regenerate token
}
HTTP Client Issues:
config/http.php:
'timeout' => 30,
'connect_timeout' => 10,
Custom Endpoints:
config/yandex-market.php:
'endpoints' => [
'products' => 'https://market.yandex.ru/api/v2/products',
],
Retry Strategy:
->retryStrategy()
->maxRetries(5)
->delay(1000)
->maxDelay(60000)
->multiplier
How can I help you explore Laravel packages today?