FeedbackAjaxController::actionSave()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
nc 3
nop 1
dl 0
loc 17
c 1
b 0
f 0
cc 3
rs 9.9332
1
<?php
2
3
namespace app\controllers\ajax;
4
5
use Yii;
6
use yii\base\InvalidArgumentException;
7
use yii\filters\{ContentNegotiator, VerbFilter};
8
use yii\web\{Controller, Request, Response, BadRequestHttpException};
9
use app\models\Feedback;
10
use app\traits\ResponseTrait;
11
12
/**
13
 * Class FeedbackAjaxController
14
 *
15
 * @package app\controllers
16
 */
17
class FeedbackAjaxController extends Controller
18
{
19
    use ResponseTrait;
20
21
    /**
22
     * @var string|array the configuration for creating the serializer that formats the response data.
23
     */
24
    public $serializer = 'yii\rest\Serializer';
25
26
    /**
27
     * @return array
28
     */
29
    public function behaviors()
30
    {
31
        return [
32
            'contentNegotiator' => [
33
                'class' => ContentNegotiator::class,
34
                'formats' => [
35
                    'application/json' => Response::FORMAT_JSON,
36
                ],
37
            ],
38
            'verbFilter' => [
39
                'class' => VerbFilter::class,
40
                'actions' => $this->verbs(),
41
            ],
42
        ];
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function afterAction($action, $result)
49
    {
50
        $result = parent::afterAction($action, $result);
51
        return $this->serializeData($result);
52
    }
53
54
    /**
55
     * @return array
56
     */
57
    public function verbs()
58
    {
59
        return [
60
            'send' => ['POST']
61
        ];
62
    }
63
64
    /**
65
     * Send new file to upload it.
66
     *
67
     * @throws BadRequestHttpException
68
     *
69
     * @return array
70
     */
71
    public function actionSend()
72
    {
73
        try {
74
            return $this->actionSave(Yii::$app->request);
75
76
        } catch (InvalidArgumentException|\Exception $e) {
77
            throw new BadRequestHttpException($e->getMessage(), $e->getCode());
78
        }
79
    }
80
81
    /**
82
     * Provides upload or update file.
83
     *
84
     * @throws InvalidArgumentException
85
     *
86
     * @param $request
87
     *
88
     * @return array
89
     */
90
    private function actionSave($request)
91
    {
92
        if (!($request instanceof Request)) {
93
            throw new InvalidArgumentException('Param $request must be instanceof yii\web\Request.');
94
        }
95
96
        $feedback = new Feedback();
97
        $feedback->setScenario(Feedback::SCENARIO_FEEDBACK);
98
        $feedback->setAttributes($request->post(), false);
0 ignored issues
show
Bug introduced by
It seems like $request->post() can also be of type object; however, parameter $values of yii\base\Model::setAttributes() 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

98
        $feedback->setAttributes(/** @scrutinizer ignore-type */ $request->post(), false);
Loading history...
99
100
        if ($feedback->contact(Yii::$app->params['adminEmail'])) {
101
102
            return $this->getSuccessResponse(Yii::t('feedback', 'You have successfully sent your message.'));
103
        } else {
104
105
            return $this->getFailResponse(Yii::t('feedback', 'Error send data.'), [
106
                'errors' => $feedback->getErrors()
107
            ]);
108
        }
109
    }
110
111
    /**
112
     * Serializes the specified data.
113
     * The default implementation will create a serializer based on the configuration given by [[serializer]].
114
     * It then uses the serializer to serialize the given data.
115
     *
116
     * @param mixed $data the data to be serialized
117
     *
118
     * @return mixed the serialized data.
119
     */
120
    private function serializeData($data)
121
    {
122
        return Yii::createObject($this->serializer)->serialize($data);
123
    }
124
}
125