accounts/common/tasks/CreateWebHooksDeliveries.php
ErickSkrauch 5fc97fdd7a Implemented oauth session revocation notification.
Reworked webhooks notifications constructors
2020-10-01 01:40:28 +03:00

48 lines
1.3 KiB
PHP

<?php
declare(strict_types=1);
namespace common\tasks;
use common\models\WebHook;
use common\notifications\NotificationInterface;
use yii\db\Expression;
use yii\queue\RetryableJobInterface;
final class CreateWebHooksDeliveries implements RetryableJobInterface {
private NotificationInterface $notification;
public function __construct(NotificationInterface $notification) {
$this->notification = $notification;
}
public function getTtr(): int {
return 10;
}
public function canRetry($attempt, $error): bool {
return true;
}
public function execute($queue): void {
$type = $this->notification::getType();
$payloads = $this->notification->getPayloads();
/** @var WebHook[] $targets */
$targets = WebHook::find()
// It's very important to use exactly single quote to begin the string
// and double quote to specify the string value
->andWhere(new Expression("JSON_CONTAINS(`events`, '\"{$type}\"')"))
->all();
foreach ($targets as $target) {
$job = new DeliveryWebHook();
$job->type = $type;
$job->url = $target->url;
$job->secret = $target->secret;
$job->payloads = $payloads;
$queue->push($job);
}
}
}