Completed
Push — master ( 7c01e0...b01669 )
by Wilmer
04:14
created

SiteController::behaviors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 7
ccs 1
cts 1
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 10
     * behaviors
30
     */
31
    public function behaviors()
32
    {
33 10
        return [
34
            'verbs' => [
35
                'class' => VerbFilter::class,
36
                'actions' => [
37
                    'logout' => ['post'],
38
                ],
39
            ],
40
        ];
41
    }
42
43
44
    /**
45 10
     * actions
46
     */
47
    public function actions()
48
    {
49 10
        return [
50
            'error' => [
51
                'class' => ErrorAction::class,
52
            ],
53 10
            'captcha' => [
54
                'class' => CaptchaAction::class,
55
                '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 1
     * __construct
62
     */
63 1
    public function __construct($id, Module $module, array $config = [])
64
    {
65
        $this->mailer = new Mailer(\Yii::$app->mailer);
66
        parent::__construct($id, $module, $config);
67
    }
68
69 1
    /**
70
     * actionIndex
71 1
     */
72
    public function actionIndex(): string
73
    {
74
        return $this->render('index');
75
    }
76
77
    /**
78 8
     * actionAbout
79
     **/
80 8
    public function actionAbout(): string
81
    {
82 8
        return $this->render('about');
83 2
    }
84
85 2
    /**
86
     * actionContact
87 2
     * @return response|string
88
     **/
89
    public function actionContact()
90 8
    {
91 8
        $model = new ContactForm();
92
93
        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
            $this->sendContact($model);
95
96
            $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
            return $this->refresh();
99
        }
100 3
101
        return $this->render('contact', [
102 3
            'model' => $model,
103 3
        ]);
104 3
    }
105 3
106
    /**
107 3
     * sendContact
108 3
     * @param Model $model
109 3
     **/
110 3
    public function sendContact(Model $model): void
111 3
    {
112
        $this->mailer->sendMessage(
113
            $model->email,
114
            $model->subject,
115
            [
116
                'replyTo' => [
117
                    $model->email => $model->name
118
                ],
119
                'textBody' => $model->body
120
            ]
121
        );
122
    }
123
}
124