Controller   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 32.5 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
dl 13
loc 40
rs 10
c 0
b 0
f 0
wmc 7
lcom 2
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A redirect() 13 13 3
A returnJSON() 0 8 1
A flashErrors() 0 6 2
A setAdminEnvironment() 0 5 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
/**
4
 * Controller
5
 * @copyright Copyright (c) 2011 - 2014 Aleksandr Torosh (http://wezoom.com.ua)
6
 * @author Aleksandr Torosh <[email protected]>
7
 */
8
9
namespace Application\Mvc;
10
11
/**
12
 * @property \Phalcon\Cache\Backend\Memcache $cache
13
 * @property \Phalcon\Mvc\View\Simple $viewSimple
14
 * @property \Application\Mvc\Helper $helper
15
 * @property \Phalcon\Http\Cookie $cookies
16
 */
17
18
class Controller extends \Phalcon\Mvc\Controller
19
{
20
21 View Code Duplication
    public function redirect($url, $code = 302)
22
    {
23
        switch ($code) {
24
            case 301:
25
                header('HTTP/1.1 301 Moved Permanently');
26
                break;
27
            case 302:
28
                header('HTTP/1.1 302 Moved Temporarily');
29
                break;
30
        }
31
        header('Location: ' . $url);
32
        $this->response->send();
33
    }
34
35
    public function returnJSON($response)
36
    {
37
        $this->view->disable();
38
39
        $this->response->setContentType('application/json', 'UTF-8');
40
        $this->response->setContent(json_encode($response));
41
        $this->response->send();
42
    }
43
44
    public function flashErrors($model)
45
    {
46
        foreach ($model->getMessages() as $message) {
47
            $this->flash->error($message);
48
        }
49
    }
50
51
    public function setAdminEnvironment()
52
    {
53
        $this->view->setMainView(MAIN_VIEW_PATH . 'admin');
54
        $this->view->setLayout(null);
55
    }
56
57
}