Passed
Push — master ( 069627...087cc0 )
by Mihail
03:55
created

Widget::actionTurn()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 12

Duplication

Lines 22
Ratio 100 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 22
loc 22
rs 8.9197
cc 4
eloc 12
nc 3
nop 1
1
<?php
2
3
namespace Apps\Controller\Admin;
4
5
6
use Apps\Model\Admin\Application\FormInstall;
7
use Apps\Model\Admin\Application\FormTurn;
8
use Apps\Model\Admin\Application\FormUpdate;
9
use Extend\Core\Arch\AdminController;
10
use Ffcms\Core\App;
11
use Ffcms\Core\Exception\ForbiddenException;
12
use Ffcms\Core\Exception\NotFoundException;
13
use Ffcms\Core\Helper\Type\Str;
14
15
/**
16
 * Class Widget - control of user comments in website.
17
 * This class provide general admin implementation of control for user comments and its settings
18
 * @package Apps\Controller\Admin
19
 */
20 View Code Duplication
class Widget extends AdminController
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
{
22
    public $type = 'widget';
23
24
    /**
25
     * Widget constructor. Disable installation checking for this controller
26
     */
27
    public function __construct()
28
    {
29
        parent::__construct(false);
30
    }
31
32
    /**
33
     * Show all installed widgets
34
     * @return string
35
     * @throws \Ffcms\Core\Exception\SyntaxException
36
     */
37
    public function actionIndex()
38
    {
39
        return App::$View->render('index', [
40
            'widgets' => $this->widgets
41
        ]);
42
    }
43
44
    /**
45
     * Show installation for of applications
46
     * @return string
47
     * @throws \Ffcms\Core\Exception\SyntaxException
48
     */
49
    public function actionInstall()
50
    {
51
        $model = new FormInstall($this->applications, 'widget');
52
53
        // check if model is sended
54
        if ($model->send()) {
55
            // validate app name
56
            if ($model->validate()) {
57
                // try to run ::install method from remoute controller
58
                if ($model->make()) {
59
                    App::$Session->getFlashBag()->add('success', __('Widget "%widget%" is successful installed!', ['widget' => $model->sysname]));
60
                    App::$Response->redirect('widget/index');
61
                } else {
62
                    App::$Session->getFlashBag()->add('error', __('During the installation process an error has occurred! Please contact with widget developer.'));
63
                }
64
            } else {
65
                App::$Session->getFlashBag()->add('error', __('Probably, app or widget with the same name is always used! Try to solve this conflict.'));
66
            }
67
        }
68
69
        return App::$View->render('install', [
70
            'model' => $model->export()
71
        ]);
72
    }
73
74
    public function actionUpdate($sys_name)
75
    {
76
        // get controller name and try to find app in db
77
        $controller = ucfirst(Str::lowerCase($sys_name));
78
        $search = \Apps\ActiveRecord\App::getItem('widget', $controller);
79
80
        // check what we got
81
        if ($search === null || (int)$search->id < 1) {
82
            throw new NotFoundException('Widget is not founded');
83
        }
84
85
        // init model and make update with notification
86
        $model = new FormUpdate($search);
87
88
        if ($model->send() && $model->validate()) {
89
            $model->make();
90
            App::$Session->getFlashBag()->add('success', __('Widget %w% is successful updated to %v% version', ['w' => $sys_name, 'v' => $model->scriptVersion]));
91
            App::$Response->redirect('application/index');
92
        }
93
94
        // render response
95
        return App::$View->render('update', [
96
            'model' => $model
97
        ]);
98
    }
99
100
    /**
101
     * Allow turn on/off applications
102
     * @param $controllerName
103
     * @return string
104
     * @throws ForbiddenException
105
     * @throws \Ffcms\Core\Exception\SyntaxException
106
     */
107
    public function actionTurn($controllerName)
108
    {
109
        $controllerName = ucfirst(Str::lowerCase($controllerName));
110
111
        $search = \Apps\ActiveRecord\App::where('sys_name', '=', $controllerName)->where('type', '=', 'widget')->first();
112
113
        if ($search === null || (int)$search->id < 1) {
114
            throw new ForbiddenException('App is not founded');
115
        }
116
117
        $model = new FormTurn();
118
119
        if ($model->send()) {
120
            $model->update($search);
121
            App::$Session->getFlashBag()->add('success', __('Widget status was changed'));
122
        }
123
124
        return App::$View->render('turn', [
125
            'widget' => $search,
126
            'model' => $model
127
        ]);
128
    }
129
130
}