Passed
Push — master ( cc7747...d2190f )
by Mihail
10:36
created

Application   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 120
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 14

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 120
loc 120
rs 10
c 1
b 0
f 0
wmc 15
lcom 1
cbo 14

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

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
127
        if ($model->send()) {
128
            $model->update();
129
            App::$Session->getFlashBag()->add('success', __('Application status was changed'));
130
        }
131
132
        return $this->view->render('turn', [
133
            'app' => $search,
134
            'model' => $model
135
        ]);
136
    }
137
}