self::DURATION_FOREVER], [['message'], 'string'], [['account'], 'validateAccountActivity'], ]; } public function getAccount(): Account { return $this->account; } public function validateAccountActivity() { if ($this->account->status === Account::STATUS_BANNED) { $this->addError('account', E::ACCOUNT_ALREADY_BANNED); } } public function ban(): bool { if (!$this->validate()) { return false; } $transaction = Yii::$app->db->beginTransaction(); $account = $this->account; $account->status = Account::STATUS_BANNED; if (!$account->save()) { throw new ErrorException('Cannot ban account'); } $this->createTask(); $transaction->commit(); return true; } public function createTask(): void { $model = new AccountBanned(); $model->accountId = $this->account->id; $model->duration = $this->duration; $model->message = $this->message; $message = Amqp::getInstance()->prepareMessage($model, [ 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT, ]); Amqp::sendToEventsExchange('accounts.account-banned', $message); } public function __construct(Account $account, array $config = []) { $this->account = $account; parent::__construct($config); } }