SubscribeAction::run()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 16
rs 9.2
cc 4
eloc 9
nc 2
nop 0
1
<?php
2
/**
3
 * @link https://github.com/yiimaker/yii2-newsletter
4
 * @copyright Copyright (c) 2017 Yii Maker
5
 * @license BSD 3-Clause License
6
 */
7
8
namespace ymaker\newsletter\frontend\actions;
9
10
use Yii;
11
use yii\base\Action;
12
use yii\web\NotFoundHttpException;
13
use yii\web\Response;
14
use ymaker\newsletter\common\services\ServiceInterface;
15
16
/**
17
 * Action for subscribe on newsletter.
18
 *
19
 * @author Vladimir Kuprienko <[email protected]>
20
 * @since 1.0
21
 */
22
class SubscribeAction extends Action
23
{
24
    /**
25
     * Running action.
26
     *
27
     * @return \yii\web\Response|array
28
     * @throws NotFoundHttpException
29
     */
30
    public function run()
31
    {
32
        $request = Yii::$app->getRequest();
33
        if ($request->getIsPost()) {
34
            /* @var ServiceInterface $service */
35
            $service = Yii::createObject(ServiceInterface::class);
36
            $response = Yii::$app->getResponse();
37
38
            if (!$service->create($request->post()) && $request->getIsAjax()) {
39
                $response->format = Response::FORMAT_JSON;
40
                return $service->getErrors();
41
            }
42
43
            return $response->redirect($request->getReferrer());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response->redire...request->getReferrer()) also could return the type yii\console\Response which is incompatible with the documented return type yii\web\Response|array.
Loading history...
44
        }
45
        throw new NotFoundHttpException();
46
    }
47
}
48