Completed
Push — master ( cffc39...fddde3 )
by Wilmer
05:25
created

SiteController::actions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 4
cts 4
cp 1
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace terabytesoft\app\basic\controllers;
4
5
use terabytesoft\app\basic\forms\ContactForm;
6
use yii\base\Model;
7
use yii\filters\AccessControl;
8
use yii\filters\VerbFilter;
9
use yii\web\Controller;
10
use yii\web\Response;
11
12
class SiteController extends Controller
13
{
14
    /**
15
     * Undocumented variable
16
     *
17
     * @var [type]
18
     */
19
    public $app;
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 10
    public function behaviors()
25
    {
26
        return [
27
            'access' => [
28 10
                'class' => AccessControl::class,
29
                'only' => ['logout'],
30
                'rules' => [
31
                    [
32
                        'actions' => ['logout'],
33
                        'allow' => true,
34
                        'roles' => ['@'],
35
                    ],
36
                ],
37
            ],
38
            'verbs' => [
39
                'class' => VerbFilter::class,
40
                'actions' => [
41
                    'logout' => ['post'],
42
                ],
43
            ],
44
        ];
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 10
    public function actions()
51
    {
52
        return [
53
            'error' => [
54 10
                'class' => 'yii\web\ErrorAction',
55
            ],
56
            'captcha' => [
57 10
                'class' => 'yii\captcha\CaptchaAction',
58 10
                'fixedVerifyCode' => YII_ENV ? 'testme' : null,
59
            ],
60
        ];
61
    }
62
63
    /**
64
     * Displays homepage.
65
     *
66
     * @return string
67
     */
68 1
    public function actionIndex()
69
    {
70 1
        return $this->render('index');
71
    }
72
73
    /**
74
     * actionAbout.
75
     *
76
     * displays about page
77
     *
78
     * @return string
79
     **/
80 1
    public function actionAbout()
81
    {
82 1
        return $this->render('about');
83
    }
84
85
    /**
86
     * actionContact.
87
     *
88
     * displays contact page
89
     *
90
     * @return response|string
91
     **/
92 8
    public function actionContact()
93
    {
94 8
        $model = new ContactForm();
95
96 8
        if ($model->load($this->module->request->post()) && $model->validate()) {
97 2
            $this->sendContact($this->module->params['app.basic.email'], $model);
98 2
            $this->module->session->setFlash('contactFormSubmitted');
99
100 2
            return $this->refresh();
101
        }
102
103 8
        return $this->render('contact', [
104 8
            'model' => $model,
105
        ]);
106
    }
107
108
    /**
109
     * sendContactForm.
110
     *
111
     * sends an email to the specified email address using the information collected by this model
112
     *
113
     * @param string $email the target email address
114
     * @param Model  $model
115
     *
116
     * @return bool whether the model passes validation
117
     **/
118 3
    public function sendContact(string $email, Model $model)
119
    {
120 3
        $this->module->mailer->compose()
121 3
            ->setTo($email)
122 3
            ->setFrom([$this->module->params['senderEmail'] => $this->module->params['senderName']])
123 3
            ->setReplyTo([$model->email => $model->name])
124 3
            ->setSubject($model->subject)
125 3
            ->setTextBody($model->body)
126 3
            ->send();
127 3
    }
128
}
129