accounts/common/tasks/ClearAccountSessions.php

62 lines
1.6 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace common\tasks;
use common\models\Account;
use Yii;
use yii\queue\RetryableJobInterface;
final class ClearAccountSessions implements RetryableJobInterface {
private int $accountId;
public function __construct(int $accountId) {
$this->accountId = $accountId;
}
/**
* @return int time to reserve in seconds
*/
public function getTtr(): int {
return 5 * 60;
}
/**
* @param int $attempt number
* @param \Exception|\Throwable $error from last execute of the job
*
* @return bool
*/
public function canRetry($attempt, $error): bool {
return true;
}
/**
* @param \yii\queue\Queue $queue which pushed and is handling the job
* @throws \Exception
*/
public function execute($queue): void {
$account = Account::findOne(['id' => $this->accountId]);
if ($account === null) {
return;
}
/** @var \common\models\AccountSession $authSession */
foreach ($account->getSessions()->each(100, Yii::$app->unbufferedDb) as $authSession) {
$authSession->delete();
}
/** @var \common\models\MinecraftAccessKey $key */
foreach ($account->getMinecraftAccessKeys()->each(100, Yii::$app->unbufferedDb) as $key) {
$key->delete();
}
/** @var \common\models\OauthSession $oauthSession */
foreach ($account->getOauthSessions()->each(100, Yii::$app->unbufferedDb) as $oauthSession) {
$oauthSession->delete();
}
}
}