2018-07-08 20:50:19 +05:30
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace common\tasks;
|
|
|
|
|
|
|
|
use common\models\WebHook;
|
2020-10-01 04:10:28 +05:30
|
|
|
use common\notifications\NotificationInterface;
|
2020-06-14 03:50:31 +05:30
|
|
|
use yii\db\Expression;
|
2018-07-08 20:50:19 +05:30
|
|
|
use yii\queue\RetryableJobInterface;
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
final readonly class CreateWebHooksDeliveries implements RetryableJobInterface {
|
2018-07-08 20:50:19 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function __construct(private NotificationInterface $notification) {
|
2020-06-12 02:57:02 +05:30
|
|
|
}
|
2018-07-08 20:50:19 +05:30
|
|
|
|
2020-06-12 02:57:02 +05:30
|
|
|
public function getTtr(): int {
|
2018-07-08 20:50:19 +05:30
|
|
|
return 10;
|
|
|
|
}
|
|
|
|
|
2020-06-12 02:57:02 +05:30
|
|
|
public function canRetry($attempt, $error): bool {
|
2018-07-08 20:50:19 +05:30
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-12 02:57:02 +05:30
|
|
|
public function execute($queue): void {
|
2020-10-01 04:10:28 +05:30
|
|
|
$type = $this->notification::getType();
|
|
|
|
$payloads = $this->notification->getPayloads();
|
|
|
|
|
2018-07-08 20:50:19 +05:30
|
|
|
/** @var WebHook[] $targets */
|
|
|
|
$targets = WebHook::find()
|
2020-06-14 03:50:31 +05:30
|
|
|
// It's very important to use exactly single quote to begin the string
|
|
|
|
// and double quote to specify the string value
|
2020-10-01 04:10:28 +05:30
|
|
|
->andWhere(new Expression("JSON_CONTAINS(`events`, '\"{$type}\"')"))
|
2018-07-08 20:50:19 +05:30
|
|
|
->all();
|
|
|
|
foreach ($targets as $target) {
|
|
|
|
$job = new DeliveryWebHook();
|
2020-10-01 04:10:28 +05:30
|
|
|
$job->type = $type;
|
2018-07-08 20:50:19 +05:30
|
|
|
$job->url = $target->url;
|
|
|
|
$job->secret = $target->secret;
|
2020-10-01 04:10:28 +05:30
|
|
|
$job->payloads = $payloads;
|
|
|
|
$queue->push($job);
|
2018-07-08 20:50:19 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|