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

Application::actionUpdate()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 24
Code Lines 12

Duplication

Lines 24
Ratio 100 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 24
loc 24
rs 8.5126
cc 5
eloc 12
nc 3
nop 1
1
<?php
2
3
namespace Apps\Controller\Admin;
4
5
use Apps\Model\Admin\Application\FormTurn;
6
use Apps\Model\Admin\Application\FormInstall;
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 View Code Duplication
class Application 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...
15
{
16
    public $type = 'app';
17
18
    public function __construct()
19
    {
20
        // prevent version checks
21
        parent::__construct(false);
22
    }
23
24
25
    /**
26
     * List of all installed applications
27
     * @return string|null
28
     * @throws \Ffcms\Core\Exception\SyntaxException
29
     */
30
    public function actionIndex()
31
    {
32
        return App::$View->render('index', [
33
            'apps' => $this->applications
34
        ]);
35
    }
36
37
38
    /**
39
     * Show installation for of applications
40
     * @return string
41
     * @throws \Ffcms\Core\Exception\SyntaxException
42
     */
43
    public function actionInstall()
44
    {
45
        $model = new FormInstall($this->applications, 'app');
46
47
        // check if model is sended
48
        if ($model->send()) {
49
            // validate app name
50
            if ($model->validate()) {
51
                // try to run ::install method from remoute controller
52
                if ($model->make()) {
53
                        App::$Session->getFlashBag()->add('success', __('Application "%app%" is successful installed!', ['app' => $model->sysname]));
54
                    App::$Response->redirect('application/index');
55
                } else {
56
                    App::$Session->getFlashBag()->add('error', __('During the installation process an error has occurred! Please contact with application developer.'));
57
                }
58
            } else {
59
                App::$Session->getFlashBag()->add('error', __('Probably, app or widget with the same name is always used! Try to solve this conflict.'));
60
            }
61
        }
62
63
        return App::$View->render('install', [
64
            'model' => $model->export()
65
        ]);
66
    }
67
68
    public function actionUpdate($sys_name)
69
    {
70
        // get controller name and try to find app in db
71
        $controller = ucfirst(Str::lowerCase($sys_name));
72
        $search = \Apps\ActiveRecord\App::getItem('app', $controller);
73
74
        // check what we got
75
        if ($search === null || (int)$search->id < 1) {
76
            throw new NotFoundException('App is not founded');
77
        }
78
79
        // init model and make update with notification
80
        $model = new FormUpdate($search);
81
        if ($model->send() && $model->validate()) {
82
            $model->make();
83
            App::$Session->getFlashBag()->add('success', __('Application %s% is successful updated to %v% version', ['s' => $sys_name, 'v' => $model->scriptVersion]));
84
            App::$Response->redirect('application/index');
85
        }
86
87
        // render response
88
        return App::$View->render('update', [
89
            'model' => $model
90
        ]);
91
    }
92
93
    /**
94
     * Allow turn on/off applications
95
     * @param $controllerName
96
     * @return string
97
     * @throws ForbiddenException
98
     * @throws \Ffcms\Core\Exception\SyntaxException
99
     */
100
    public function actionTurn($controllerName)
101
    {
102
        $controllerName = ucfirst(Str::lowerCase($controllerName));
103
104
        $search = \Apps\ActiveRecord\App::where('sys_name', '=', $controllerName)->where('type', '=', 'app')->first();
105
106
        if ($search === null || (int)$search->id < 1) {
107
            throw new ForbiddenException('App is not founded');
108
        }
109
110
        $model = new FormTurn();
111
112
        if ($model->send()) {
113
            $model->update($search);
114
            App::$Session->getFlashBag()->add('success', __('Application status was changed'));
115
        }
116
117
        return App::$View->render('turn', [
118
            'app' => $search,
119
            'model' => $model
120
        ]);
121
    }
122
}