VerifyEmailForm.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace frontend\models;
  3. use common\models\User;
  4. use yii\base\InvalidArgumentException;
  5. use yii\base\Model;
  6. class VerifyEmailForm extends Model
  7. {
  8. /**
  9. * @var string
  10. */
  11. public $token;
  12. /**
  13. * @var User
  14. */
  15. private $_user;
  16. /**
  17. * Creates a form model with given token.
  18. *
  19. * @param string $token
  20. * @param array $config name-value pairs that will be used to initialize the object properties
  21. * @throws InvalidArgumentException if token is empty or not valid
  22. */
  23. public function __construct($token, array $config = [])
  24. {
  25. if (empty($token) || !is_string($token)) {
  26. throw new InvalidArgumentException('Verify email token cannot be blank.');
  27. }
  28. $this->_user = User::findByVerificationToken($token);
  29. if (!$this->_user) {
  30. throw new InvalidArgumentException('Wrong verify email token.');
  31. }
  32. parent::__construct($config);
  33. }
  34. /**
  35. * Verify email
  36. *
  37. * @return User|null the saved model or null if saving fails
  38. */
  39. public function verifyEmail()
  40. {
  41. $user = $this->_user;
  42. $user->status = User::STATUS_ACTIVE;
  43. return $user->save(false) ? $user : null;
  44. }
  45. }