diff --git a/api/exceptions/Exception.php b/api/exceptions/Exception.php deleted file mode 100644 index 50972ae..0000000 --- a/api/exceptions/Exception.php +++ /dev/null @@ -1,6 +0,0 @@ -account_id = $account->id; $activation->key = UserFriendlyRandomKey::make(); - if (!$activation->save()) { - throw new ThisShouldNotHappenException('Unable save email-activation model.'); - } + Assert::true($activation->save(), 'Unable save email-activation model.'); $this->emailActivation = $activation; diff --git a/api/modules/accounts/models/BanAccountForm.php b/api/modules/accounts/models/BanAccountForm.php index f3712d2..b88aee3 100644 --- a/api/modules/accounts/models/BanAccountForm.php +++ b/api/modules/accounts/models/BanAccountForm.php @@ -1,10 +1,10 @@ getAccount(); $account->status = Account::STATUS_BANNED; - if (!$account->save()) { - throw new ThisShouldNotHappenException('Cannot ban account'); - } + Assert::true($account->save(), 'Cannot ban account'); Yii::$app->queue->push(ClearAccountSessions::createFromAccount($account)); diff --git a/api/modules/accounts/models/ChangeEmailForm.php b/api/modules/accounts/models/ChangeEmailForm.php index 3de16c6..c0d4b5b 100644 --- a/api/modules/accounts/models/ChangeEmailForm.php +++ b/api/modules/accounts/models/ChangeEmailForm.php @@ -2,9 +2,9 @@ namespace api\modules\accounts\models; use api\aop\annotations\CollectModelMetrics; -use api\exceptions\ThisShouldNotHappenException; use api\validators\EmailActivationKeyValidator; use common\models\EmailActivation; +use Webmozart\Assert\Assert; use Yii; class ChangeEmailForm extends AccountActionForm { @@ -33,9 +33,7 @@ class ChangeEmailForm extends AccountActionForm { $account = $this->getAccount(); $account->email = $activation->newEmail; - if (!$account->save()) { - throw new ThisShouldNotHappenException('Cannot save new account email value'); - } + Assert::true($account->save(), 'Cannot save new account email value'); $transaction->commit(); diff --git a/api/modules/accounts/models/ChangeLanguageForm.php b/api/modules/accounts/models/ChangeLanguageForm.php index b9502e3..8be94c9 100644 --- a/api/modules/accounts/models/ChangeLanguageForm.php +++ b/api/modules/accounts/models/ChangeLanguageForm.php @@ -2,8 +2,8 @@ namespace api\modules\accounts\models; use api\aop\annotations\CollectModelMetrics; -use api\exceptions\ThisShouldNotHappenException; use common\validators\LanguageValidator; +use Webmozart\Assert\Assert; class ChangeLanguageForm extends AccountActionForm { @@ -26,9 +26,7 @@ class ChangeLanguageForm extends AccountActionForm { $account = $this->getAccount(); $account->lang = $this->lang; - if (!$account->save()) { - throw new ThisShouldNotHappenException('Cannot change user language'); - } + Assert::true($account->save(), 'Cannot change user language'); return true; } diff --git a/api/modules/accounts/models/ChangePasswordForm.php b/api/modules/accounts/models/ChangePasswordForm.php index 1782ef2..042ded8 100644 --- a/api/modules/accounts/models/ChangePasswordForm.php +++ b/api/modules/accounts/models/ChangePasswordForm.php @@ -3,10 +3,10 @@ namespace api\modules\accounts\models; use api\aop\annotations\CollectModelMetrics; use api\components\User\Component; -use api\exceptions\ThisShouldNotHappenException; use api\validators\PasswordRequiredValidator; use common\helpers\Error as E; use common\validators\PasswordValidator; +use Webmozart\Assert\Assert; use Yii; use yii\helpers\ArrayHelper; @@ -61,9 +61,7 @@ class ChangePasswordForm extends AccountActionForm { Yii::$app->user->terminateSessions($account, Component::KEEP_CURRENT_SESSION); } - if (!$account->save()) { - throw new ThisShouldNotHappenException('Cannot save user model'); - } + Assert::true($account->save(), 'Cannot save user model'); $transaction->commit(); diff --git a/api/modules/accounts/models/ChangeUsernameForm.php b/api/modules/accounts/models/ChangeUsernameForm.php index 53ad10d..ef77056 100644 --- a/api/modules/accounts/models/ChangeUsernameForm.php +++ b/api/modules/accounts/models/ChangeUsernameForm.php @@ -2,11 +2,11 @@ namespace api\modules\accounts\models; use api\aop\annotations\CollectModelMetrics; -use api\exceptions\ThisShouldNotHappenException; use api\validators\PasswordRequiredValidator; use common\models\UsernameHistory; use common\tasks\PullMojangUsername; use common\validators\UsernameValidator; +use Webmozart\Assert\Assert; use Yii; class ChangeUsernameForm extends AccountActionForm { @@ -40,16 +40,12 @@ class ChangeUsernameForm extends AccountActionForm { $transaction = Yii::$app->db->beginTransaction(); $account->username = $this->username; - if (!$account->save()) { - throw new ThisShouldNotHappenException('Cannot save account model with new username'); - } + Assert::true($account->save(), 'Cannot save account model with new username'); $usernamesHistory = new UsernameHistory(); $usernamesHistory->account_id = $account->id; $usernamesHistory->username = $account->username; - if (!$usernamesHistory->save()) { - throw new ThisShouldNotHappenException('Cannot save username history record'); - } + Assert::true($usernamesHistory->save(), 'Cannot save username history record'); Yii::$app->queue->push(PullMojangUsername::createFromAccount($account)); diff --git a/api/modules/accounts/models/DisableTwoFactorAuthForm.php b/api/modules/accounts/models/DisableTwoFactorAuthForm.php index ce8a429..cfb0e55 100644 --- a/api/modules/accounts/models/DisableTwoFactorAuthForm.php +++ b/api/modules/accounts/models/DisableTwoFactorAuthForm.php @@ -2,10 +2,10 @@ namespace api\modules\accounts\models; use api\aop\annotations\CollectModelMetrics; -use api\exceptions\ThisShouldNotHappenException; use api\validators\PasswordRequiredValidator; use api\validators\TotpValidator; use common\helpers\Error as E; +use Webmozart\Assert\Assert; class DisableTwoFactorAuthForm extends AccountActionForm { @@ -33,9 +33,7 @@ class DisableTwoFactorAuthForm extends AccountActionForm { $account = $this->getAccount(); $account->is_otp_enabled = false; $account->otp_secret = null; - if (!$account->save()) { - throw new ThisShouldNotHappenException('Cannot disable otp for account'); - } + Assert::true($account->save(), 'Cannot disable otp for account'); return true; } diff --git a/api/modules/accounts/models/EnableTwoFactorAuthForm.php b/api/modules/accounts/models/EnableTwoFactorAuthForm.php index 0e19639..40df158 100644 --- a/api/modules/accounts/models/EnableTwoFactorAuthForm.php +++ b/api/modules/accounts/models/EnableTwoFactorAuthForm.php @@ -3,10 +3,10 @@ namespace api\modules\accounts\models; use api\aop\annotations\CollectModelMetrics; use api\components\User\Component; -use api\exceptions\ThisShouldNotHappenException; use api\validators\PasswordRequiredValidator; use api\validators\TotpValidator; use common\helpers\Error as E; +use Webmozart\Assert\Assert; use Yii; class EnableTwoFactorAuthForm extends AccountActionForm { @@ -36,9 +36,7 @@ class EnableTwoFactorAuthForm extends AccountActionForm { $account = $this->getAccount(); $account->is_otp_enabled = true; - if (!$account->save()) { - throw new ThisShouldNotHappenException('Cannot enable otp for account'); - } + Assert::true($account->save(), 'Cannot enable otp for account'); Yii::$app->user->terminateSessions($account, Component::KEEP_CURRENT_SESSION); diff --git a/api/modules/accounts/models/PardonAccountForm.php b/api/modules/accounts/models/PardonAccountForm.php index fe9c073..4ff5b07 100644 --- a/api/modules/accounts/models/PardonAccountForm.php +++ b/api/modules/accounts/models/PardonAccountForm.php @@ -1,9 +1,9 @@ getAccount(); $account->status = Account::STATUS_ACTIVE; - if (!$account->save()) { - throw new ThisShouldNotHappenException('Cannot pardon account'); - } + Assert::true($account->save(), 'Cannot pardon account'); $transaction->commit(); diff --git a/api/modules/accounts/models/SendEmailVerificationForm.php b/api/modules/accounts/models/SendEmailVerificationForm.php index ff09c80..c6189aa 100644 --- a/api/modules/accounts/models/SendEmailVerificationForm.php +++ b/api/modules/accounts/models/SendEmailVerificationForm.php @@ -2,12 +2,12 @@ namespace api\modules\accounts\models; use api\aop\annotations\CollectModelMetrics; -use api\exceptions\ThisShouldNotHappenException; use api\validators\PasswordRequiredValidator; use common\helpers\Error as E; use common\models\confirmations\CurrentEmailConfirmation; use common\models\EmailActivation; use common\tasks\SendCurrentEmailConfirmation; +use Webmozart\Assert\Assert; use Yii; class SendEmailVerificationForm extends AccountActionForm { @@ -59,9 +59,7 @@ class SendEmailVerificationForm extends AccountActionForm { $account = $this->getAccount(); $emailActivation = new CurrentEmailConfirmation(); $emailActivation->account_id = $account->id; - if (!$emailActivation->save()) { - throw new ThisShouldNotHappenException('Cannot save email activation model'); - } + Assert::true($emailActivation->save(), 'Cannot save email activation model'); return $emailActivation; } diff --git a/api/modules/accounts/models/SendNewEmailVerificationForm.php b/api/modules/accounts/models/SendNewEmailVerificationForm.php index 1c9470a..f4db3a0 100644 --- a/api/modules/accounts/models/SendNewEmailVerificationForm.php +++ b/api/modules/accounts/models/SendNewEmailVerificationForm.php @@ -2,12 +2,12 @@ namespace api\modules\accounts\models; use api\aop\annotations\CollectModelMetrics; -use api\exceptions\ThisShouldNotHappenException; use api\validators\EmailActivationKeyValidator; use common\models\confirmations\NewEmailConfirmation; use common\models\EmailActivation; use common\tasks\SendNewEmailConfirmation; use common\validators\EmailValidator; +use Webmozart\Assert\Assert; use Yii; class SendNewEmailVerificationForm extends AccountActionForm { @@ -50,9 +50,7 @@ class SendNewEmailVerificationForm extends AccountActionForm { $emailActivation = new NewEmailConfirmation(); $emailActivation->account_id = $this->getAccount()->id; $emailActivation->newEmail = $this->email; - if (!$emailActivation->save()) { - throw new ThisShouldNotHappenException('Cannot save email activation model'); - } + Assert::true($emailActivation->save(), 'Cannot save email activation model'); return $emailActivation; } diff --git a/api/modules/accounts/models/TwoFactorAuthInfo.php b/api/modules/accounts/models/TwoFactorAuthInfo.php index 987369a..a46c475 100644 --- a/api/modules/accounts/models/TwoFactorAuthInfo.php +++ b/api/modules/accounts/models/TwoFactorAuthInfo.php @@ -3,7 +3,6 @@ declare(strict_types=1); namespace api\modules\accounts\models; -use api\exceptions\ThisShouldNotHappenException; use api\models\base\BaseAccountForm; use BaconQrCode\Common\ErrorCorrectionLevel; use BaconQrCode\Encoder\Encoder; @@ -14,6 +13,7 @@ use common\components\Qr\ElyDecorator; use OTPHP\TOTP; use OTPHP\TOTPInterface; use ParagonIE\ConstantTime\Base32; +use Webmozart\Assert\Assert; class TwoFactorAuthInfo extends BaseAccountForm { @@ -61,16 +61,10 @@ class TwoFactorAuthInfo extends BaseAccountForm { return 'data:image/svg+xml,' . $svg; } - /** - * @param int $length - * @throws ThisShouldNotHappenException - */ private function setOtpSecret(int $length = 24): void { $account = $this->getAccount(); $account->otp_secret = $this->generateOtpSecret($length); - if (!$account->save()) { - throw new ThisShouldNotHappenException('Cannot set account otp_secret'); - } + Assert::true($account->save(), 'Cannot set account otp_secret'); } /** diff --git a/api/modules/oauth/controllers/ClientsController.php b/api/modules/oauth/controllers/ClientsController.php index 9439376..a23a8bc 100644 --- a/api/modules/oauth/controllers/ClientsController.php +++ b/api/modules/oauth/controllers/ClientsController.php @@ -2,7 +2,6 @@ namespace api\modules\oauth\controllers; use api\controllers\Controller; -use api\exceptions\ThisShouldNotHappenException; use api\modules\oauth\exceptions\UnsupportedOauthClientType; use api\modules\oauth\models\OauthClientForm; use api\modules\oauth\models\OauthClientFormFactory; @@ -10,6 +9,7 @@ use api\modules\oauth\models\OauthClientTypeForm; use api\rbac\Permissions as P; use common\models\Account; use common\models\OauthClient; +use Webmozart\Assert\Assert; use Yii; use yii\filters\AccessControl; use yii\helpers\ArrayHelper; @@ -68,9 +68,7 @@ class ClientsController extends Controller { public function actionCreate(string $type): array { $account = Yii::$app->user->identity->getAccount(); - if ($account === null) { - throw new ThisShouldNotHappenException('This form should not to be executed without associated account'); - } + Assert::notNull($account === null, 'This form should not to be executed without associated account'); $client = new OauthClient(); $client->account_id = $account->id; diff --git a/api/modules/oauth/models/OauthClientForm.php b/api/modules/oauth/models/OauthClientForm.php index 683e414..3932e89 100644 --- a/api/modules/oauth/models/OauthClientForm.php +++ b/api/modules/oauth/models/OauthClientForm.php @@ -3,10 +3,10 @@ declare(strict_types=1); namespace api\modules\oauth\models; -use api\exceptions\ThisShouldNotHappenException; use api\modules\oauth\exceptions\InvalidOauthClientState; use common\models\OauthClient; use common\tasks\ClearOauthSessions; +use Webmozart\Assert\Assert; use Yii; use yii\helpers\Inflector; @@ -48,9 +48,7 @@ class OauthClientForm { $client->generateSecret(); } - if (!$client->save()) { - throw new ThisShouldNotHappenException('Cannot save oauth client'); - } + Assert::true($client->save(), 'Cannot save oauth client'); return true; } @@ -60,9 +58,7 @@ class OauthClientForm { $client = $this->client; $client->is_deleted = true; - if (!$client->save()) { - throw new ThisShouldNotHappenException('Cannot update oauth client'); - } + Assert::true($client->save(), 'Cannot update oauth client'); Yii::$app->queue->push(ClearOauthSessions::createFromOauthClient($client)); @@ -77,9 +73,7 @@ class OauthClientForm { $client = $this->client; if ($regenerateSecret) { $client->generateSecret(); - if (!$client->save()) { - throw new ThisShouldNotHappenException('Cannot update oauth client'); - } + Assert::true($client->save(), 'Cannot update oauth client'); } Yii::$app->queue->push(ClearOauthSessions::createFromOauthClient($client, time())); diff --git a/common/tasks/PullMojangUsername.php b/common/tasks/PullMojangUsername.php index 610d435..041a7c1 100644 --- a/common/tasks/PullMojangUsername.php +++ b/common/tasks/PullMojangUsername.php @@ -3,13 +3,13 @@ declare(strict_types=1); namespace common\tasks; -use api\exceptions\ThisShouldNotHappenException; use common\models\Account; use common\models\MojangUsername; use Ely\Mojang\Api as MojangApi; use Ely\Mojang\Exception\MojangApiException; use Ely\Mojang\Exception\NoContentException; use GuzzleHttp\Exception\GuzzleException; +use Webmozart\Assert\Assert; use Yii; use yii\queue\JobInterface; @@ -60,9 +60,7 @@ class PullMojangUsername implements JobInterface { $mojangUsername->touch('last_pulled_at'); } - if (!$mojangUsername->save()) { - throw new ThisShouldNotHappenException('Cannot save mojang username'); - } + Assert::true($mojangUsername->save(), 'Cannot save mojang username'); } } diff --git a/console/models/WebHookForm.php b/console/models/WebHookForm.php index 4f9cfcd..806bcc3 100644 --- a/console/models/WebHookForm.php +++ b/console/models/WebHookForm.php @@ -3,9 +3,9 @@ declare(strict_types=1); namespace console\models; -use api\exceptions\ThisShouldNotHappenException; use common\models\WebHook; use common\models\WebHookEvent; +use Webmozart\Assert\Assert; use Yii; use yii\base\Model; @@ -43,17 +43,13 @@ class WebHookForm extends Model { $webHook = $this->webHook; $webHook->url = $this->url; $webHook->secret = $this->secret; - if (!$webHook->save()) { - throw new ThisShouldNotHappenException('Cannot save webhook.'); - } + Assert::true($webHook->save(), 'Cannot save webhook.'); foreach ($this->events as $event) { $eventModel = new WebHookEvent(); $eventModel->webhook_id = $webHook->id; $eventModel->event_type = $event; - if (!$eventModel->save()) { - throw new ThisShouldNotHappenException('Cannot save webhook event.'); - } + Assert::true($eventModel->save(), 'Cannot save webhook event.'); } $transaction->commit();