Completed
Pull Request — master (#16)
by Hugo
03:17
created

ControllerBase   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 192
Duplicated Lines 5.21 %

Coupling/Cohesion

Components 1
Dependencies 2
Metric Value
wmc 26
lcom 1
cbo 2
dl 10
loc 192
rs 10

18 Methods

Rating   Name   Duplication   Size   Complexity  
A afterExecuteRoute() 0 7 1
A indexAction() 0 15 3
A frmAction() 0 4 1
A getInstance() 0 10 2
A readAction() 0 8 2
A setValuesToObject() 0 4 1
B updateAction() 0 24 5
A deleteAction() 0 6 1
A asAdminAction() 0 6 1
A asUserAction() 0 6 1
A logoutAction() 0 5 1
A _showDisplayedMessage() 0 4 1
A _showMessage() 0 5 1
A messageSuccess() 0 4 1
A messageWarning() 0 4 1
A messageDanger() 0 4 1
A messageInfo() 0 4 1
A soloUpdateAction() 10 10 1

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
use Phalcon\Mvc\Controller;
4
use Phalcon\Mvc\Url;
5
6
class ControllerBase extends Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
9
    protected $model;
10
    protected $title;
11
    protected $controller;
12
    protected $messageTimerInterval = 3000;
13
14
    public function afterExecuteRoute($dispatcher)
15
    {
16
        $baseUrl = $this->baseUrl;
17
        $this->view->setVar("baseUrl", $baseUrl);
18
        $this->view->setVar("controller", $this->controller);
19
        $this->view->setVar("title", $this->title);
20
    }
21
22
    public function indexAction($message = NULL)
23
    {
24
        $msg = "";
25
        if (isset($message)) {
26
            if (is_string($message)) {
27
                $message = new DisplayedMessage($message);
28
            }
29
            $message->setTimerInterval($this->messageTimerInterval);
30
            $msg = $this->_showDisplayedMessage($message);
31
        }
32
        $this->view->setVar("msg", $msg);
33
        $objects = call_user_func($this->model . "::find");
34
        $this->view->setVar("objects", $objects);
35
        $this->view->pick("main/index");
36
    }
37
38
    public function frmAction($id = NULL)
39
    {
40
        echo "Pas encore implémenté...";
41
    }
42
43
    public function getInstance($id = NULL)
44
    {
45
        if (isset($id)) {
46
            $object = call_user_func($this->model . "::findfirst", $id);
47
        } else {
48
            $className = $this->model;
49
            $object = new $className();
50
        }
51
        return $object;
52
    }
53
54
    public function readAction($id = NULL)
55
    {
56
        if ($id != null) {
57
            $object = call_user_func($this->model . '::find', "id = $id");
58
            $this->view->setVar("object", $object);
59
            $this->view->pick("main/read");
60
        }
61
    }
62
63
    protected function setValuesToObject(&$object)
0 ignored issues
show
Coding Style introduced by
setValuesToObject uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
64
    {
65
        $object->assign($_POST);
66
    }
67
68
69
    public function updateAction()
0 ignored issues
show
Coding Style introduced by
updateAction uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
70
    {
71
        $id = $this->request->getPost('id', 'int');
72
        if ($this->request->isPost()) {
73
            $object = $this->getInstance(@$_POST["id"]);
74
            $this->setValuesToObject($object);
75
            if ($id) {
76
                try {
77
                    $object->save();
78
                    $msg = new DisplayedMessage("Instance de " . $this->model . " modifiée");
79
                } catch (\Exception $e) {
80
                    $msg = new DisplayedMessage("Impossible d'ajouter l'instance de " . $this->model, "danger : $e");
81
                }
82
            } else {
83
                try {
84
                    $object->save();
85
                    $msg = new DisplayedMessage("Instance de " . $this->model . " ajoutée");
86
                } catch (\Exception $e) {
87
                    $msg = new DisplayedMessage("Impossible d'ajouter l'instance de " . $this->model, "danger : $e");
88
                }
89
            }
90
            $this->dispatcher->forward(array("controller" => $this->dispatcher->getControllerName(), "action" => "index", "params" => array($msg)));
91
        }
92
    }
93
94
    //PErmet l'édition d'un seul champ à la fois
95 View Code Duplication
    public function soloUpdateAction()
0 ignored issues
show
Coding Style introduced by
soloUpdateAction uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Duplication introduced by
This method 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...
96
    {
97
        $name = $this->request->getPost('name', 'string');
98
        //Créer la fonction variable 'set' en fonction du name en POST
99
        $func = 'set' . ucfirst($name);
100
        $projet = call_user_func($this->model . '::findFirst', $_POST['pk']);
101
        $projet->$func($_POST['value']);
102
        $projet->save();
103
104
    }
105
106
107
    public function deleteAction($id = null)
108
    {
109
        $object = call_user_func($this->model . '::findFirst', "$id");
110
        $object->delete();
111
        $this->response->redirect("$this->controller/index");
112
    }
113
114
    public function asAdminAction()
115
    {
116
        $user = User::findFirst("id=3");
117
        $this->session->set("user", $user);
118
        $this->response->redirect("$this->controller/index");
119
    }
120
121
    public function asUserAction()
122
    {
123
        $user = User::findFirst("id=1");
124
        $this->session->set("user", $user);
125
        $this->response->redirect("$this->controller/index");
126
    }
127
128
    public function logoutAction()
129
    {
130
        $this->session->destroy();
131
        $this->response->redirect("Index/index");
132
    }
133
134
135
    /**
136
     * Affiche un message Alert bootstrap
137
     * @param DisplayedMessage $message
138
     */
139
    public function _showDisplayedMessage($message)
140
    {
141
        return $message->compile($this->jquery);
142
    }
143
144
    /**
145
     * Affiche un message Alert bootstrap
146
     * @param string $message texte du message
147
     * @param string $type type du message (info, success, warning ou danger)
148
     * @param number $timerInterval durée en millisecondes d'affichage du message (0 pour que le message reste affiché)
149
     * @param string $dismissable si vrai, l'alert dispose d'une croix de fermeture
150
     */
151
    public function _showMessage($message, $type = "success", $timerInterval = 0, $dismissable = true, $visible = true)
152
    {
153
        $message = new DisplayedMessage($message, $type, $timerInterval, $dismissable, $visible);
0 ignored issues
show
Bug introduced by
It seems like $dismissable defined by parameter $dismissable on line 151 can also be of type string; however, DisplayedMessage::DisplayedMessage() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
154
        $this->_showDisplayedMessage($message);
155
    }
156
157
158
    public function messageSuccess($message, $timerInterval = 0, $dismissable = true)
159
    {
160
        $this->_showMessage($message, "success", $timerInterval, $dismissable);
161
    }
162
163
    /**
164
     * Affiche un message Alert bootstrap de type warning
165
     * @param string $message texte du message
166
     * @param number $timerInterval durée en millisecondes d'affichage du message (0 pour que le message reste affiché)
167
     * @param string $dismissable si vrai, l'alert dispose d'une croix de fermeture
168
     */
169
    public function messageWarning($message, $timerInterval = 0, $dismissable = true)
170
    {
171
        $this->_showMessage($message, "warning", $timerInterval, $dismissable);
172
    }
173
174
    /**
175
     * Affiche un message Alert bootstrap de type danger
176
     * @param string $message texte du message
177
     * @param number $timerInterval durée en millisecondes d'affichage du message (0 pour que le message reste affiché)
178
     * @param string $dismissable si vrai, l'alert dispose d'une croix de fermeture
179
     */
180
    public function messageDanger($message, $timerInterval = 0, $dismissable = true)
181
    {
182
        $this->_showMessage($message, "danger", $timerInterval, $dismissable);
183
    }
184
185
    /**
186
     * Affiche un message Alert bootstrap de type info
187
     * @param string $message texte du message
188
     * @param number $timerInterval durée en millisecondes d'affichage du message (0 pour que le message reste affiché)
189
     * @param string $dismissable si vrai, l'alert dispose d'une croix de fermeture
190
     */
191
    public function messageInfo($message, $timerInterval = 0, $dismissable = true)
192
    {
193
        $this->_showMessage($message, "info", $timerInterval, $dismissable);
194
    }
195
196
197
}
198