ContactController::behaviors()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
nc 1
nop 0
dl 0
loc 18
c 0
b 0
f 0
cc 1
rs 9.8666
1
<?php
2
3
namespace app\controllers;
4
5
use Yii;
6
use yii\filters\{AccessControl, VerbFilter};
7
use yii\helpers\ArrayHelper;
8
use yii\web\NotFoundHttpException;
9
use app\models\{Contact, Feedback};
10
11
/**
12
 * Class ContactController
13
 *
14
 * @package app\controllers
15
 */
16
class ContactController extends BaseController
17
{
18
    /**
19
     * @return array
20
     */
21
    public function behaviors()
22
    {
23
        return ArrayHelper::merge(parent::behaviors(), [
24
            'access' => [
25
                'class' => AccessControl::class,
26
                'rules' => [
27
                    [
28
                        'allow' => true,
29
                        'actions' => ['index', 'captcha'],
30
                        'roles' => ['?', '@'],
31
                    ],
32
                ],
33
            ],
34
            'verbs' => [
35
                'class' => VerbFilter::class,
36
                'actions' => [
37
                    'index' => ['get', 'post'],
38
                    'captcha' => ['get'],
39
                ],
40
            ],
41
        ]);
42
    }
43
44
    public function actions()
45
    {
46
        return ArrayHelper::merge(parent::actions(), [
47
            'captcha' => [
48
                'class' => 'yii\captcha\CaptchaAction',
49
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
50
            ],
51
        ]);
52
    }
53
54
    /**
55
     * Displays contacts page.
56
     *
57
     * @return string
58
     *
59
     * @throws NotFoundHttpException
60
     */
61
    public function actionIndex()
62
    {
63
        $feedback = new Feedback();
64
        $feedback->setScenario(Feedback::SCENARIO_CONTACT);
65
66
        if ($feedback->load(Yii::$app->request->post()) && $feedback->contact(Yii::$app->params['adminEmail'])) {
0 ignored issues
show
Bug introduced by
It seems like Yii::app->request->post() can also be of type object; however, parameter $data of yii\base\Model::load() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
        if ($feedback->load(/** @scrutinizer ignore-type */ Yii::$app->request->post()) && $feedback->contact(Yii::$app->params['adminEmail'])) {
Loading history...
67
68
            Yii::$app->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

68
            Yii::$app->session->/** @scrutinizer ignore-call */ 
69
                                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...
69
70
            return $this->refresh();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->refresh() returns the type yii\web\Response which is incompatible with the documented return type string.
Loading history...
71
        }
72
73
        $model = Contact::getDefaultContacts();
74
75
        if (null === $model) {
76
            throw new NotFoundHttpException('Contacts not fount.');
77
        }
78
79
        $this->setMetaParams($model);
0 ignored issues
show
Bug introduced by
It seems like $model can also be of type array; however, parameter $model of app\controllers\BaseController::setMetaParams() does only seem to accept null|yii\db\ActiveRecord, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
        $this->setMetaParams(/** @scrutinizer ignore-type */ $model);
Loading history...
80
81
        return $this->render('index', [
82
            'model' => $model,
83
            'feedback' => $feedback
84
        ]);
85
    }
86
}
87