Test Failed
Push — master ( e3c39f...fe570d )
by Mihail
07:20
created

Apps/Controller/Admin/Widget.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Apps\Controller\Admin;
4
5
use Apps\Model\Admin\Application\FormInstall;
6
use Apps\Model\Admin\Application\FormTurn;
7
use Apps\Model\Admin\Application\FormUpdate;
8
use Extend\Core\Arch\AdminController;
9
use Ffcms\Core\App;
10
use Ffcms\Core\Exception\ForbiddenException;
11
use Ffcms\Core\Exception\NotFoundException;
12
use Ffcms\Core\Helper\Type\Str;
13
14
/**
15
 * Class Widget. Control installed and not-installed widgets.
16
 * @package Apps\Controller\Admin
17
 */
18
class Widget extends AdminController
19
{
20
    public $type = 'widget';
21
22
    /**
23
     * Widget constructor. Disable installation checking for this controller
24
     */
25
    public function __construct()
26
    {
27
        parent::__construct(false);
28
    }
29
30
    /**
31
     * Show all installed widgets
32
     * @return string|null
33
     */
34
    public function actionIndex(): ?string
35
    {
36
        return $this->view->render('widget/index', [
37
            'widgets' => $this->widgets
38
        ]);
39
    }
40
41
    /**
42
     * Show installation form for widget
43
     * @return string|null
44
     * @throws \Ffcms\Core\Exception\SyntaxException
45
     */
46
    public function actionInstall(): ?string
47
    {
48
        $model = new FormInstall($this->applications, 'widget');
49
50
        // check if model is sended
51
        if ($model->send()) {
52
            // validate app name
53
            if ($model->validate()) {
54
                // try to run ::install method from remoute controller
55
                if ($model->make()) {
56
                    App::$Session->getFlashBag()->add('success', __('Widget "%widget%" is successful installed!', ['widget' => $model->sysname]));
57
                    $this->response->redirect('widget/index');
58
                } else {
59
                    App::$Session->getFlashBag()->add('error', __('During the installation process an error has occurred! Please contact with widget developer.'));
60
                }
61
            } else {
62
                App::$Session->getFlashBag()->add('error', __('Probably, app or widget with the same name is always used! Try to solve this conflict.'));
63
            }
64
        }
65
66
        return $this->view->render('widget/install', [
67
            'model' => $model
68
        ]);
69
    }
70
71
    /**
72
     * Run widget update - display submit form & callback execution
73
     * @param string $sys
74
     * @return string|null
75
     * @throws NotFoundException
76
     * @throws \Ffcms\Core\Exception\SyntaxException
77
     */
78
    public function actionUpdate($sys): ?string
79
    {
80
        // get controller name and try to find app in db
81
        $controller = ucfirst(Str::lowerCase($sys));
82
        $search = \Apps\ActiveRecord\App::getItem('widget', $controller);
0 ignored issues
show
Are you sure the assignment to $search is correct as Apps\ActiveRecord\App::g...('widget', $controller) targeting Apps\ActiveRecord\App::getItem() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
83
84
        // check what we got
85
        if (!$search || (int)$search->id < 1) {
86
            throw new NotFoundException('Widget is not founded');
87
        }
88
89
        // init model and make update with notification
90
        $model = new FormUpdate($search);
91
92
        if ($model->send() && $model->validate()) {
93
            $model->make();
94
            App::$Session->getFlashBag()->add('success', __('Widget %w% is successful updated to %v% version', ['w' => $sys, 'v' => $model->scriptVersion]));
95
            $this->response->redirect('application/index');
96
        }
97
98
        // render response
99
        return $this->view->render('widget/update', [
100
            'model' => $model
101
        ]);
102
    }
103
104
    /**
105
     * Allow turn on/off widget
106
     * @param string $controllerName
107
     * @return string
108
     * @throws ForbiddenException
109
     */
110
    public function actionTurn($controllerName): ?string
111
    {
112
        // get controller name & find object in db
113
        $controllerName = ucfirst(Str::lowerCase($controllerName));
114
        /** @var \Apps\ActiveRecord\App $record */
115
        $record = \Apps\ActiveRecord\App::where('sys_name', $controllerName)
116
            ->where('type', 'widget')
117
            ->first();
118
119
        // check if widget admin controller exists
120
        if (!$record || (int)$record->id < 1) {
121
            throw new ForbiddenException('Widget is not founded');
122
        }
123
124
        // initialize turn on/off model
125
        $model = new FormTurn($record);
126
        if ($model->send()) {
127
            $model->update();
128
            App::$Session->getFlashBag()->add('success', __('Widget status was changed'));
129
        }
130
131
        // render view
132
        return $this->view->render('widget/turn', [
133
            'widget' => $record,
134
            'model' => $model
135
        ]);
136
    }
137
}
138