Completed
Push — master ( 531a23...3492bb )
by Vladimir
27:36
created

DefaultController::commonAction()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 2
nop 2
1
<?php
2
/**
3
 * @link https://github.com/yiimaker/yii2-banner
4
 * @copyright Copyright (c) 2017 Yii Maker
5
 * @license BSD 3-Clause License
6
 */
7
8
namespace ymaker\banner\backend\controllers;
9
10
use Yii;
11
use yii\web\Controller;
12
use ymaker\banner\backend\services\BannerServiceInterface;
13
14
/**
15
 * Default controller for backend banner module.
16
 *
17
 * @author Vladimir Kuprienko <[email protected]>
18
 * @since 1.0
19
 */
20
class DefaultController extends Controller
21
{
22
    /**
23
     * @var BannerServiceInterface
24
     */
25
    private $_service;
26
27
28
    /**
29
     * @inheritdoc
30
     * @param BannerServiceInterface $service
31
     */
32
    public function __construct($id, $module, BannerServiceInterface $service, $config = [])
33
    {
34
        $this->_service = $service;
35
        parent::__construct($id, $module, $config);
36
    }
37
38
    /**
39
     * Renders banners list.
40
     *
41
     * @return string
42
     */
43
    public function actionIndex()
44
    {
45
        return $this->render('index', [
46
            'dataProvider' => $this->_service->getDataProvider(),
47
        ]);
48
    }
49
50
    /**
51
     * Creates new banner.
52
     *
53
     * @return string|\yii\web\Response
54
     */
55
    public function actionCreate()
56
    {
57
        return $this->commonAction($this->redirect(['index']), 'index');
58
    }
59
60
    /**
61
     * Updates banner.
62
     *
63
     * @param int $id
64
     * @return string|\yii\web\Response
65
     */
66
    public function actionUpdate($id)
67
    {
68
        return $this->commonAction($this->redirect(['view', 'id' => $id]), 'update');
69
    }
70
71
    /**
72
     * Common code for create and update actions.
73
     *
74
     * @param \yii\web\Response $redirect
75
     * @param string $view
76
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be \yii\web\Response|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
77
     */
78
    protected function commonAction($redirect, $view)
79
    {
80
        $model = $this->_service->getModel();
81
        $request = Yii::$app->getRequest();
82
        if ($request->getIsPost() && $this->_service->save($request->post())) {
83
            return $redirect;
84
        }
85
86
        return $this->render($view, compact('model'));
87
    }
88
89
    /**
90
     * Renders details about banner.
91
     *
92
     * @param int $id Banner model ID.
93
     * @return string
94
     */
95
    public function actionView($id)
96
    {
97
        $model = $this->_service->getModel($id);
98
        return $this->render('view', compact('model'));
99
    }
100
101
    /**
102
     * Delete banner.
103
     *
104
     * @param int $id
105
     * @return \yii\web\Response
106
     */
107
    public function actionDelete($id)
108
    {
109
        $message = 'Error: banner not removed';
110
        if ($this->_service->delete($id)) {
111
            $message = 'Removed successfully';
112
        }
113
        Yii::$app->getSession()->setFlash('yii2-banner', $message);
114
115
        return $this->redirect(['index']);
116
    }
117
}
118