Completed
Push — master ( 2e1376...dd50d3 )
by Wilmer
02:24
created

SiteController::sendContact()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 1
rs 10
1
<?php
2
3
namespace terabytesoft\app\basic\controllers;
4
5
use terabytesoft\app\basic\forms\ContactForm;
6
use terabytesoft\helpers\Mailer;
7
use yii\base\Model;
8
use yii\base\Module;
9
use yii\captcha\CaptchaAction;
10
use yii\filters\AccessControl;
11
use yii\filters\VerbFilter;
12
use yii\web\Controller;
13
use yii\web\ErrorAction;
14
use yii\web\Response;
15
16
/**
17
 * SiteController
18
 *
19
 * Controller web application basic
20
 */
21
class SiteController extends Controller
22
{
23
    /**
24
     * @var object
25
     */
26
    private $mailer;
27
28
    /**
29
     * behaviors
30
     */
31 5
    public function behaviors()
32
    {
33
        return [
34
            'verbs' => [
35 5
                'class' => VerbFilter::class,
36
                'actions' => [
37
                    'logout' => ['post'],
38
                ],
39
            ],
40
        ];
41
    }
42
43
44
    /**
45
     * actions
46
     */
47 5
    public function actions()
48
    {
49
        return [
50
            'error' => [
51 5
                'class' => ErrorAction::class,
52
            ],
53
            'captcha' => [
54
                'class' => CaptchaAction::class,
55 5
                'fixedVerifyCode' => (YII_ENV === 'test') ? 'testme' : null,
0 ignored issues
show
introduced by
The condition terabytesoft\app\basic\c...lers\YII_ENV === 'test' is always true.
Loading history...
56
            ],
57
        ];
58
    }
59
60
    /**
61
     * __construct
62
     */
63 7
    public function __construct($id, Module $module, array $config = [])
64
    {
65 7
        $this->mailer = new Mailer(\Yii::$app->mailer);
66 7
        parent::__construct($id, $module, $config);
67 7
    }
68
69
    /**
70
     * actionIndex
71
     */
72 1
    public function actionIndex(): string
73
    {
74 1
        return $this->render('index');
75
    }
76
77
    /**
78
     * actionAbout
79
     **/
80
    public function actionAbout(): string
81
    {
82
        return $this->render('about');
83
    }
84
85
    /**
86
     * actionContact
87
     * @return response|string
88
     **/
89 4
    public function actionContact()
90
    {
91 4
        $model = new ContactForm();
92
93 4
        if ($model->load($this->module->request->post()) && $model->validate()) {
0 ignored issues
show
Bug introduced by
The method post() 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

93
        if ($model->load($this->module->request->/** @scrutinizer ignore-call */ post()) && $model->validate()) {

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...
94 1
            $this->sendContact($model);
95
96 1
            $this->module->session->setFlash('contactFormSubmitted');
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

96
            $this->module->session->/** @scrutinizer ignore-call */ 
97
                                    setFlash('contactFormSubmitted');

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...
97
98 1
            return $this->refresh();
99
        }
100
101 4
        return $this->render('contact', [
102 4
            'model' => $model,
103
        ]);
104
    }
105
106
    /**
107
     * sendContact
108
     * @param Model $model
109
     **/
110 2
    public function sendContact(Model $model): void
111
    {
112 2
        $this->mailer->sendMessage(
113 2
            $model->email,
114 2
            $model->subject,
115
            [
116
                'replyTo' => [
117 2
                    $model->email => $model->name
118
                ],
119 2
                'textBody' => $model->body
120
            ]
121
        );
122 2
    }
123
}
124