Completed
Pull Request — master (#171)
by Corey
02:59
created

SiteController::actions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace site\controllers;
4
5
use Yii;
6
use yii\web\BadRequestHttpException;
7
use common\components\Controller;
8
use yii\filters\VerbFilter;
9
use common\components\AccessControl;
10
11
/**
12
 * Site controller
13
 */
14
class SiteController extends Controller
15
{
16
  /**
17
   * @inheritdoc
18
   */
19
  public function behaviors()
20
  {
21
    return [
22
      'access' => [
23
        'class' => AccessControl::class,
24
        'rules' => [
25
          [
26
            'actions' => ['index', 'blog', 'error', 'privacy', 'terms', 'about', 'captcha', 'contact', 'faq'],
27
            'allow' => true,
28
          ],
29
          [
30
            'actions' => ['login', 'signup', 'reset-password', 'request-password-reset', 'verify-email'],
31
            'allow' => true,
32
            'roles' => ['?'],
33
          ],
34
          [
35
            'actions' => ['logout', 'welcome'],
36
            'allow' => true,
37
            'roles' => ['@'],
38
          ],
39
        ],
40
      ],
41
      'verbs' => [
42
        'class' => VerbFilter::class,
43
        'actions' => [
44
          'logout' => ['post'],
45
        ],
46
      ],
47
    ];
48
  }
49
50
  public function actionIndex()
51
  {
52
    return $this->render('index');
53
  }
54
55
  public function actionBlog()
56
  {
57
    $time = Yii::$container->get(\common\interfaces\TimeInterface::class); 
58
    $key = "index_blog_".$time->getLocalDate('UTC');
59
    $posts = Yii::$app->cache->get($key);
60
    if($posts === false) {
61
      $posts = \Yii::$app->getModule('blog')
62
                            ->fetch()
63
                            ->parse()
64
                            ->results;
65
    }
66
    Yii::$app->cache->set($key, $posts, 60*60*24);
67
68
    return $this->render('blog', ['posts'=>$posts]);
69
  }
70
71
  public function actionLogin()
72
  {
73
    $model = Yii::$container->get(\common\models\LoginForm::class);
74
    if ($model->load(Yii::$app->request->post()) && $model->login()) {
75
      return $this->goBack();
76
    } else {
77
      return $this->render('login', [
78
        'model' => $model,
79
      ]);
80
    }
81
  }
82
83
  public function actionLogout()
84
  {
85
    Yii::$app->user->logout();
0 ignored issues
show
Bug introduced by
The method logout() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
    Yii::$app->user->/** @scrutinizer ignore-call */ 
86
                     logout();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
    return $this->goHome();
87
  }
88
89
  public function actionContact()
90
  {
91
    $model = new \site\models\ContactForm();
92
    if ($model->load(Yii::$app->request->post()) && $model->validate()) {
93
      if($model->sendEmail(Yii::$app->params['adminEmail'])) {
94
        Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
0 ignored issues
show
Bug introduced by
The method setFlash() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

94
        Yii::$app->session->/** @scrutinizer ignore-call */ 
95
                            setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
95
      } else {
96
        Yii::$app->session->setFlash('error', 'There was an error sending email.');
97
      }
98
99
      return $this->refresh();
100
    } else {
101
      return $this->render('contact', [
102
        'model' => $model,
103
      ]);
104
    }
105
  }
106
107
  public function actionAbout()
108
  {
109
    return $this->render('about');
110
  }
111
112
  public function actionFaq()
113
  {
114
    return $this->render('faq');
115
  }
116
117
  public function actionWelcome()
118
  {
119
    return $this->render('welcome');
120
  }
121
122
  public function actionSignup()
123
  {
124
    $model = Yii::$container->get(\site\models\SignupForm::class);
125
    if($model->load(Yii::$app->request->post()) && $model->validate()) {
126
      $model->signup();
127
      Yii::$app->session->setFlash('success', 'We have sent a verification email to the email address you provided. Please check your inbox and follow the instructions to verify your account.');
128
      return $this->redirect('/',302);
129
    }
130
131
    return $this->render('signup', [
132
      'model' => $model,
133
    ]);
134
  }
135
136
  public function actionRequestPasswordReset()
137
  {
138
    $model = Yii::$container->get(\site\models\PasswordResetRequestForm::class);
139
    if($model->load(Yii::$app->request->post()) && $model->validate()) {
140
      if(!$model->sendEmail()) {
141
        $ip = Yii::$app->getRequest()->getUserIP() ?: "UNKNOWN";
142
        Yii::warning("$ip has tried to reset the password for ".$model->email);
143
      }
144
145
      Yii::$app->session->setFlash('success', 'If there is an account with the submitted email address you will receive further instructions in your email inbox.');
146
      return $this->goHome();
147
    }
148
149
    return $this->render('requestPasswordResetToken', [
150
      'model' => $model,
151
    ]);
152
  }
153
154
  public function actionResetPassword($token)
155
  {
156
    try {
157
      $model = Yii::$container->get(\site\models\ResetPasswordForm::class, [$token]);
158
    } catch (\yii\base\InvalidParamException $e) {
159
      throw new BadRequestHttpException($e->getMessage());
160
    }
161
162
    if ($model->load(Yii::$app->request->post())
163
        && $model->validate()
164
        && $model->resetPassword()) {
165
      Yii::$app->session->setFlash('success', 'New password was saved.');
166
      return $this->goHome();
167
    }
168
169
    return $this->render('resetPassword', [
170
      'model' => $model,
171
    ]);
172
  }
173
174
  public function actionVerifyEmail($token)
175
  {
176
    if (empty($token) || !is_string($token)) {
177
      throw new BadRequestHttpException('Email verification token cannot be blank.');
178
    }
179
180
    $user = Yii::$container->get(\common\interfaces\UserInterface::class)
181
              ->findByVerifyEmailToken($token);
182
    if (!$user) {
183
      throw new BadRequestHttpException("Wrong or expired email verification token. If you aren't sure why this error occurs perhaps you've already verified your account. Please try logging in.");
184
    }
185
186
    if($user->isTokenConfirmed($user->verify_email_token)) {
187
      Yii::$app->session->setFlash('success', 'Your account has already been verified. Please log in.');
188
      return $this->redirect('/login',302);
189
    } else if (Yii::$app->getUser()->login($user)) {
190
      $user->confirmVerifyEmailToken();
191
      $user->save();
192
      Yii::$app->session->setFlash('success', 'Your account has been verified. Please continue with your check-in.');
193
      return $this->redirect('/welcome',302);
194
    }
195
  }
196
197
  public function actionPrivacy()
198
  {
199
    return $this->render('privacy');
200
  }
201
202
  public function actionTerms()
203
  {
204
    return $this->render('terms');
205
  }
206
}
207