Completed
Push — master ( 672d95...766bd1 )
by Korotkov
01:54
created

BaseController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 97
Duplicated Lines 24.74 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 24
loc 97
c 0
b 0
f 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 5 1
A before() 0 4 1
A after() 0 6 1
B templateEngine() 0 41 2
A error404() 8 8 1
A error503() 8 8 1
A error500() 8 8 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
 * Date: 16.07.15
5
 * Time: 12:41
6
 *
7
 * @author    : Korotkov Danila <[email protected]>
8
 * @copyright Copyright (c) 2016, Korotkov Danila
9
 * @license   http://www.gnu.org/licenses/gpl.html GNU GPLv3.0
10
 */
11
12
namespace App\Http;
13
14
use Rudra\Container;
15
use Rudra\Controller;
16
use Rudra\IContainer;
17
18
/**
19
 * Class Module
20
 *
21
 * @package Main
22
 */
23
class BaseController extends Controller
24
{
25
26
    /**
27
     * @param IContainer $container
28
     * @param string     $templateEngine
29
     */
30
    public function init(IContainer $container, string $templateEngine)
31
    {
32
        parent::init($container, $templateEngine);
33
        $this->setData('Rudra', 'title');
34
    }
35
36
    /**
37
     * Метод выполняется перед вызовом контроллера
38
     */
39
    public function before()
40
    {
41
        $this->container()->get('auth')->check();
42
    }
43
44
    public function after()
45
    {
46
        // Очищаем сессию от alert
47
        $this->container()->unsetSession('value');
48
        $this->container()->unsetSession('alert');
49
    }
50
51
    public function templateEngine(string $config): void
52
    {
53
        parent::templateEngine($config);
54
55
        $d = new \Twig_SimpleFunction('d', function ($var) {
56
            return d($var);
57
        });
58
59
        $this->getTwig()->addFunction($d);
60
61
        $date = new \Twig_SimpleFunction('date', function ($var) {
62
            return date($var);
63
        });
64
65
        $this->getTwig()->addFunction($date);
66
67
        $auth = new \Twig_SimpleFunction('auth', function () {
68
            return $this->container()->get('auth')->regularAccess();
69
        });
70
71
        $this->getTwig()->addFunction($auth);
72
73
74
        $active = new \Twig_SimpleFunction('active', function ($var) {
75
            return $this->container()->get('pagination')->activeClass($var);
76
        });
77
78
        $this->getTwig()->addFunction($active);
79
80
        $strstr = new \Twig_SimpleFunction('strstr', function ($var) {
81
            return strstr($var, '<a id="strstr" name="strstr"></a>', true);
82
        });
83
84
        $this->getTwig()->addFunction($strstr);
85
86
        if (DEV) {
87
            $debugbarRenderer = Container::$app->get('debugbar')->getJavascriptRenderer();
88
            $this->getTwig()->addGlobal('debugbar', $debugbarRenderer);
89
        }
90
91
    }
92
93 View Code Duplication
    public function error404()
0 ignored issues
show
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...
94
    {
95
        $this->container()->get('redirect')->responseCode('404');
96
97
        return $this->twig('errors/error.html.twig', [
98
            'title'  => '404 Page Not Found :: ' . $this->getData('title'),
99
        ]);
100
    }
101
102 View Code Duplication
    public function error503()
0 ignored issues
show
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...
103
    {
104
        $this->container()->get('redirect')->responseCode('503');
105
106
        return $this->twig('errors/error.html.twig', [
107
            'title'  => '503 Service Unavailable :: ' . $this->getData('title'),
108
        ]);
109
    }
110
111 View Code Duplication
    public function error500()
0 ignored issues
show
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...
112
    {
113
        $this->container()->get('redirect')->responseCode('503');
114
115
        return $this->twig('errors/503.html.twig', [
116
            'title'  => '503 Service Unavailable :: ' . $this->getData('title'),
117
        ]);
118
    }
119
}
120