DefaultController   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 17
c 1
b 0
f 0
dl 0
loc 101
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A actionCreate() 0 3 1
A commonAction() 0 9 3
A actionUpdate() 0 3 1
A actionView() 0 3 1
A actionDelete() 0 9 2
A actionIndex() 0 3 1
A __construct() 0 5 1
1
<?php
2
/**
3
 * @link https://github.com/yiimaker/yii2-email-templates
4
 * @copyright Copyright (c) 2017-2019 Yii Maker
5
 * @license BSD 3-Clause License
6
 */
7
8
namespace ymaker\email\templates\controllers;
9
10
use Yii;
11
use yii\web\Controller;
12
use ymaker\email\templates\Module as TemplatesModule;
13
use ymaker\email\templates\repositories\EmailTemplatesRepositoryInterface;
14
15
/**
16
 * CRUD controller for backend.
17
 *
18
 * @author Volodymyr Kupriienko <[email protected]>
19
 * @since 1.0
20
 */
21
class DefaultController extends Controller
22
{
23
    /**
24
     * Email templates repository instance.
25
     * Instance will be gotten from DI container.
26
     *
27
     * @var EmailTemplatesRepositoryInterface
28
     */
29
    protected $repository;
30
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function __construct($id, $module, EmailTemplatesRepositoryInterface $repository, $config = [])
36
    {
37
        $this->repository = $repository;
38
39
        parent::__construct($id, $module, $config);
40
    }
41
42
    /**
43
     * Renders entities list.
44
     *
45
     * @return string
46
     */
47
    public function actionIndex()
48
    {
49
        return $this->render('index', ['dataProvider' => $this->repository->getDataProvider()]);
50
    }
51
52
    /**
53
     * Creates new entity.
54
     *
55
     * @return string|\yii\web\Response
56
     */
57
    public function actionCreate()
58
    {
59
        return $this->commonAction($this->repository->create(), ['index'], 'create');
60
    }
61
62
    /**
63
     * Updates entity.
64
     *
65
     * @param int $id
66
     *
67
     * @return string|\yii\web\Response
68
     */
69
    public function actionUpdate($id)
70
    {
71
        return $this->commonAction($this->repository->getById($id), ['view', 'id' => $id], 'update');
72
    }
73
74
    /**
75
     * Renders entity details.
76
     *
77
     * @param int $id
78
     *
79
     * @return string
80
     */
81
    public function actionView($id)
82
    {
83
        return $this->render('view', ['model' => $this->repository->getById($id)]);
84
    }
85
86
    /**
87
     * Delete entity.
88
     *
89
     * @param int $id
90
     *
91
     * @return \yii\web\Response
92
     */
93
    public function actionDelete($id)
94
    {
95
        $message = $this->repository->delete($id)
96
            ? TemplatesModule::t('Removed successfully')
97
            : TemplatesModule::t('Error: banner not removed');
98
99
        Yii::$app->getSession()->setFlash('yii2-email-templates', $message);
100
101
        return $this->redirect(['index']);
102
    }
103
104
    /**
105
     * Common code for create and update actions.
106
     *
107
     * @param mixed $model
108
     * @param array $redirectUrl
109
     * @param string $view
110
     *
111
     * @return string|\yii\web\Response
112
     */
113
    protected function commonAction($model, $redirectUrl, $view)
114
    {
115
        $request = Yii::$app->getRequest();
116
117
        if ($request->getIsPost() && $this->repository->save($model, $request->post())) {
118
            return $this->redirect($redirectUrl);
119
        }
120
121
        return $this->render($view, \compact('model'));
122
    }
123
}
124