Passed
Push — master ( 54c177...21af40 )
by Wilmer
01:54
created

SiteController::sendContact()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TerabyteSoft\App\Basic\Controllers;
4
5
use TerabyteSoft\App\Basic\Forms\ContactForm;
6
use yii\base\Model;
7
use Yiisoft\Yii\Captcha\CaptchaAction;
8
use yii\web\Controller;
9
use yii\web\ErrorAction;
10
use yii\web\filters\AccessControl;
11
use yii\web\filters\VerbFilter;
12
use yii\web\Response;
13
14
/**
15
 * SiteController.
16
 *
17
 * Controller web application basic
18
 **/
19
class SiteController extends Controller
20
{
21
    /**
22
     * actions.
23
     *
24
     * @return array actions config
25
     **/
26
    public function actions(): array
27
    {
28
        return [
29
            'error' => [
30
                '__class' => ErrorAction::class,
31
            ],
32
            'captcha' => [
33
                '__class'         => CaptchaAction::class,
34
                'fixedVerifyCode' => 'testme',
35
            ],
36
        ];
37
    }
38
39
    /**
40
     * actionIndex.
41
     *
42
     * displays homepage
43
     *
44
     * @return string
45
     **/
46
    public function actionIndex(): string
47
    {
48
        return $this->render('index');
49
    }
50
51
    /**
52
     * actionAbout.
53
     *
54
     * displays about page
55
     *
56
     * @return string
57
     **/
58
    public function actionAbout(): string
59
    {
60
        return $this->render('about');
61
    }
62
63
    /**
64
     * actionContact.
65
     *
66
     * displays contact page
67
     *
68
     * @return response|string
69
     **/
70
    public function actionContact()
71
    {
72
        $model = new ContactForm();
73
74
        if ($model->load($this->getApp()->request->post()) && $model->validate()) {
75
            $this->sendContact($this->getApp()->params['adminEmail'], $model);
76
            $this->getApp()->session->setFlash('contactFormSubmitted');
77
78
            return $this->refresh();
79
        }
80
81
        return $this->render('contact', [
82
            'model' => $model,
83
        ]);
84
    }
85
86
    /**
87
     * sendContactForm.
88
     *
89
     * sends an email to the specified email address using the information collected by this model
90
     *
91
     * @param string $email the target email address
92
     * @param Model  $model
93
     *
94
     * @return bool whether the model passes validation
95
     **/
96
    public function sendContact(string $email, Model $model): void
97
    {
98
        $this->getApp()->mailer->compose()
99
            ->setTo($email)
100
            ->setFrom([$model->email => $model->name])
101
            ->setSubject($model->subject)
102
            ->setTextBody($model->body)
103
            ->send();
104
    }
105
}
106