2018-07-08 20:50:19 +05:30
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace common\tasks;
|
|
|
|
|
|
|
|
use common\models\Account;
|
|
|
|
use common\models\WebHook;
|
|
|
|
use Yii;
|
2020-06-14 03:50:31 +05:30
|
|
|
use yii\db\Expression;
|
2018-07-08 20:50:19 +05:30
|
|
|
use yii\queue\RetryableJobInterface;
|
|
|
|
|
2020-06-12 02:57:02 +05:30
|
|
|
final class CreateWebHooksDeliveries implements RetryableJobInterface {
|
2018-07-08 20:50:19 +05:30
|
|
|
|
2020-06-12 02:57:02 +05:30
|
|
|
public string $type;
|
2018-07-08 20:50:19 +05:30
|
|
|
|
2020-06-12 02:57:02 +05:30
|
|
|
public array $payloads;
|
|
|
|
|
|
|
|
public function __construct(string $type, array $payloads) {
|
|
|
|
$this->type = $type;
|
|
|
|
$this->payloads = $payloads;
|
|
|
|
}
|
2018-07-08 20:50:19 +05:30
|
|
|
|
|
|
|
public static function createAccountEdit(Account $account, array $changedAttributes): self {
|
2020-06-12 02:57:02 +05:30
|
|
|
return new static('account.edit', [
|
2018-07-08 20:50:19 +05:30
|
|
|
'id' => $account->id,
|
|
|
|
'uuid' => $account->uuid,
|
|
|
|
'username' => $account->username,
|
|
|
|
'email' => $account->email,
|
|
|
|
'lang' => $account->lang,
|
|
|
|
'isActive' => $account->status === Account::STATUS_ACTIVE,
|
2020-06-12 02:57:02 +05:30
|
|
|
'isDeleted' => $account->status === Account::STATUS_DELETED,
|
2018-07-08 20:50:19 +05:30
|
|
|
'registered' => date('c', (int)$account->created_at),
|
|
|
|
'changedAttributes' => $changedAttributes,
|
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 static function createAccountDeletion(Account $account): self {
|
|
|
|
return new static('account.deletion', [
|
|
|
|
'id' => $account->id,
|
|
|
|
'uuid' => $account->uuid,
|
|
|
|
'username' => $account->username,
|
|
|
|
'email' => $account->email,
|
|
|
|
'registered' => date('c', (int)$account->created_at),
|
|
|
|
'deleted' => date('c', (int)$account->deleted_at),
|
|
|
|
]);
|
2018-07-08 20:50:19 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int time to reserve in seconds
|
|
|
|
*/
|
2020-06-12 02:57:02 +05:30
|
|
|
public function getTtr(): int {
|
2018-07-08 20:50:19 +05:30
|
|
|
return 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $attempt number
|
|
|
|
* @param \Exception|\Throwable $error from last execute of the job
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2020-06-12 02:57:02 +05:30
|
|
|
public function canRetry($attempt, $error): bool {
|
2018-07-08 20:50:19 +05:30
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \yii\queue\Queue $queue which pushed and is handling the job
|
|
|
|
*/
|
2020-06-12 02:57:02 +05:30
|
|
|
public function execute($queue): void {
|
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
|
|
|
|
->andWhere(new Expression("JSON_CONTAINS(`events`, '\"{$this->type}\"')"))
|
2018-07-08 20:50:19 +05:30
|
|
|
->all();
|
|
|
|
foreach ($targets as $target) {
|
|
|
|
$job = new DeliveryWebHook();
|
|
|
|
$job->type = $this->type;
|
|
|
|
$job->url = $target->url;
|
|
|
|
$job->secret = $target->secret;
|
|
|
|
$job->payloads = $this->payloads;
|
|
|
|
Yii::$app->queue->push($job);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|