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

Application   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 109
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 13

Importance

Changes 6
Bugs 1 Features 0
Metric Value
wmc 15
c 6
b 1
f 0
lcom 1
cbo 13
dl 109
loc 109
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A actionIndex() 6 6 1
B actionInstall() 24 24 4
B actionUpdate() 24 24 5
B actionTurn() 22 22 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}