justbetter/laravel-magento-async

通过异步请求与 Magento 交互

1.1.0 2024-09-19 10:35 UTC

This package is auto-updated.

Last update: 2024-09-19 10:35:43 UTC


README

Banner

Laravel Magento Async

Tests Coverage Analysis Total downloads

此软件包提供了一种将异步/批量请求存储到 Magento 的方法,以便稍后检索操作状态并处理状态。它通过在数据库中存储发送的异步请求以及操作,并定期检索状态来实现。

一旦操作状态发生变化,就会触发一个事件。可以将任何模型与操作相关联,这使得可以知道操作是从哪里开始的,并处理操作结果。

安装

此软件包是 justbetter/laravel-magento-client 的扩展。请确保首先安装和配置该软件包。

要求此软件包

composer require justbetter/laravel-magento-async

可选地,发布此软件包的配置文件以编辑队列和清理时间。

php artisan vendor:publish --provider="JustBetter\MagentoAsync\ServiceProvider" --tag=config

调度器

为了更新和清理异步操作的状态,您的调度器中需要两个命令

<?php

protected function schedule(Schedule $schedule): void
{
    $schedule->command(\JustBetter\MagentoAsync\Commands\UpdateBulkStatusesCommand::class)->everyFiveMinutes();
    $schedule->command(\JustBetter\MagentoAsync\Commands\CleanBulkRequestsCommand::class)->everyFiveMinutes();
}

用法

此软件包提供了一个 MagentoAsync 客户端来发送请求。稍后,该软件包将派发一个事件,通知应用程序操作已被处理。您可以监听此事件来处理操作结果。

发送请求

要使用此软件包发送异步(批量)请求,请使用 MagentoAsync 客户端

<?php

public function __construct(
    protected \JustBetter\MagentoAsync\Client\MagentoAsync $magentoAsync
) {}
...
public function handle(string $store): void {
    $products = Product::query()->take(1000)->get();

    $payload = $products
        ->map(fn(Product $product) => ['product' => ['sku' => $product->sku, 'status' => $product->status]])
        ->toArray();

    $this->magentoAsync
        ->configure(fn (Magento $magento): Magento => $magento->store($store)) // Optionally configure the Magento client
        ->subjects($products->all()) // Pass the models in the same order as the payload
        ->postBulk('products', $payload); // Bulk post this payload to the `products` endpoint
}

主题数组是模型列表,索引必须与有效负载匹配,以便将 Magento 的操作 ID 绑定到正确的模型。

发送非批量请求时,有一个 ->subject() 方法可用

<?php

public function __construct(
    protected \JustBetter\MagentoAsync\Client\MagentoAsync $magentoAsync
) {}
...
public function handle(): void {
    $product = Product::query()->first();

    $payload =  ['product' => ['sku' => $product->sku, 'status' => $product->status]];

    $this->magentoAsync
        ->subject($product) // Pass the subject
        ->post('products', $payload); // Post this payload to the `products` endpoint
}

检索操作状态

此软件包提供了一个事件 \JustBetter\MagentoAsync\Events\BulkOperationStatusEvent,用于派发所有操作。该事件包括已更新的批量操作,以便您可以处理响应。

此事件为所有操作更改派发,您可以使用操作模型来确定调用了哪个端点

如果您已绑定主题,该软件包提供了一个抽象监听器 \JustBetter\MagentoAsync\Listeners\BulkOperationStatusListener。此类提供了一种按特定模型筛选并忽略 打开 状态的方法。

示例实现

<?php

use \JustBetter\MagentoAsync\Listeners\BulkOperationStatusListener as BaseBulkOperationStatusListener;
use \JustBetter\MagentoAsync\Models\BulkOperation;
use \JustBetter\MagentoAsync\Enums\OperationStatus;

class BulkOperationStatusListener extends BaseBulkOperationStatusListener
{
    protected string $model = Product::class;

    public function execute(BulkOperation $operation): void
    {
        /** @var Model $status */
        $status = $operation->subject;

        if ($operation->status === OperationStatus::Complete) {
            // Handle complete status
            return;
        }

        // Handle failed status
    }
}

质量

为确保此软件包的质量,请运行以下命令

composer quality

这将执行三个任务

  1. 确保所有测试都通过
  2. 检查使用静态代码分析的有任何问题
  3. 检查代码是否格式正确

贡献

有关详细信息,请参阅 CONTRIBUTING

安全漏洞

请查看我们如何报告安全漏洞的 安全策略

致谢

许可

MIT 许可证(MIT)。请参阅 许可文件 了解更多信息。

JustBetter logo