DefaultController   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 35
c 1
b 0
f 0
dl 0
loc 88
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A actionIndex() 0 3 1
A actions() 0 11 2
A actionContact() 0 20 4
A actionAbout() 0 3 1
A processSendEmail() 0 16 2
1
<?php
2
3
namespace modules\main\controllers\frontend;
4
5
use Yii;
6
use yii\web\Controller;
7
use yii\web\ErrorAction;
8
use yii\captcha\CaptchaAction;
9
use yii\web\Response;
10
use modules\main\models\frontend\ContactForm;
11
use modules\users\models\User;
12
use modules\main\Module;
13
14
/**
15
 * Class DefaultController
16
 * @package modules\main\controllers\frontend
17
 */
18
class DefaultController extends Controller
19
{
20
    /**
21
     * @inheritdoc
22
     * @return array
23
     */
24
    public function actions()
25
    {
26
        return [
27
            'error' => [
28
                'class' => ErrorAction::class
29
            ],
30
            'captcha' => [
31
                'class' => CaptchaAction::class,
32
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
33
                'backColor' => 0xF1F1F1,
34
                'foreColor' => 0xEE7600
35
            ]
36
        ];
37
    }
38
39
    /**
40
     * Renders the index view for the module
41
     * @return string
42
     */
43
    public function actionIndex()
44
    {
45
        return $this->render('index');
46
    }
47
48
    /**
49
     * Displays contact page.
50
     *
51
     * @return mixed|Response
52
     */
53
    public function actionContact()
54
    {
55
        $model = new ContactForm();
56
        if (Yii::$app->user->isGuest) {
57
            $model->scenario = $model::SCENARIO_GUEST;
58
        } else {
59
            $user = Yii::$app->user;
60
            /** @var User $identity */
61
            $identity = $user->identity;
62
            $model->name = $identity->username;
63
            $model->email = $identity->email;
64
        }
65
        if (Yii::$app->request->post()) {
66
            $model->load(Yii::$app->request->post());
67
            if ($model->validate()) {
68
                return $this->processSendEmail($model);
69
            }
70
        }
71
        return $this->render('contact', [
72
            'model' => $model
73
        ]);
74
    }
75
76
    /**
77
     * @param ContactForm $model
78
     * @return Response
79
     */
80
    protected function processSendEmail($model)
81
    {
82
        /** @var yii\web\Session $session */
83
        $session = Yii::$app->session;
84
        if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
85
            $session->setFlash(
86
                'success',
87
                Module::translate(
88
                    'module',
89
                    'Thank you for contacting us. We will respond to you as soon as possible.'
90
                )
91
            );
92
        } else {
93
            $session->setFlash('error', Module::translate('module', 'There was an error sending email.'));
94
        }
95
        return $this->refresh();
96
    }
97
98
    /**
99
     * Displays about page.
100
     *
101
     * @return mixed
102
     */
103
    public function actionAbout()
104
    {
105
        return $this->render('about');
106
    }
107
}
108