Completed
Push — master ( c72cc4...8d8365 )
by Mikołaj
02:08
created

Controller::redirect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Egzaminer;
4
5
use Egzaminer\Roll\ExamsGroupModel;
6
use Egzaminer\Themes\MaterialDesignLite;
7
use Exception;
8
use Twig_Environment;
9
use Twig_Loader_Filesystem;
10
11
class Controller
12
{
13
    /**
14
     * @var array
15
     */
16
    private $container;
17
18
    /**
19
     * @var array
20
     */
21
    protected $data;
22
23
    /**
24
     * Constructor.
25
     *
26
     * @param array $container
27
     */
28
    public function __construct(array $container)
29
    {
30
        $this->container = $container;
31
    }
32
33
    /**
34
     * @param string $name Item name
35
     *
36
     * @return mixed Item from container
37
     */
38
    public function get($name)
39
    {
40
        if (isset($this->container[$name])) {
41
            return $this->container[$name];
42
        }
43
    }
44
45
    /**
46
     * @param string $name Config name
47
     *
48
     * @return mixed Config value
49
     */
50
    public function config($name)
51
    {
52
        if (isset($this->get('config')[$name])) {
53
            return $this->get('config')[$name];
54
        }
55
    }
56
57
    /**
58
     * Get request variable.
59
     *
60
     * @param string $type Request type
61
     * @param string $name Index name. Null for all
62
     *
63
     * @return mixed
64
     */
65
    public function getFromRequest($type = 'get', $name = null)
66
    {
67
        $request = $this->get('request');
68
69
        // if unknow request type
70
        if (!isset($request[$type])) {
71
            return;
72
        }
73
74
        // for get all indexes from type
75
        if (null === $name) {
76
            return $request[$type];
77
        }
78
79
        if (isset($request[$type][$name])) {
80
            return $request[$type][$name];
81
        }
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function dir()
88
    {
89
        return $this->get('dir');
90
    }
91
92
    /**
93
     * Check is user logged.
94
     *
95
     * @return bool Return true, if logged
96
     */
97
    public function isLogged()
98
    {
99
        return $this->get('auth')->isLogged();
100
    }
101
102
    /**
103
     * @param string $type    Message type
104
     * @param mixed  $message Message content
105
     *
106
     * @return void
107
     */
108
    public function setMessage($type = 'success', $message = 'Success')
109
    {
110
        switch ($type) {
111
            case 'success':
112
                $this->get('flash')->success($message);
113
                break;
114
            case 'info':
115
                $this->get('flash')->info($message);
116
                break;
117
            case 'warning':
118
                $this->get('flash')->warning($message);
119
                break;
120
            case 'error':
121
                $this->get('flash')->error($message);
122
                break;
123
124
            default:
125
                $this->get('flash')->error($message);
126
                break;
127
        }
128
    }
129
130
    /**
131
     * Redirect.
132
     *
133
     * @param string $path    Path to redirect
134
     *
135
     * @return void
136
     */
137
    public function redirect($path)
138
    {
139
        header('Location: '.$this->dir().$path);
140
    }
141
142
    public function terminate($code = 1)
143
    {
144
        exit($code);
0 ignored issues
show
Coding Style Compatibility introduced by
The method terminate() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
145
    }
146
147
    private function selectMessagesTemplate()
148
    {
149
        switch ($this->config('theme')) {
150
            case 'mdl':
151
                $this->get('flash')->setTemplate(new MaterialDesignLite());
152
                break;
153
        }
154
    }
155
156
    /**
157
     * @param string $template Template name
158
     * @param array  $data     Data to use in template
159
     */
160
    public function render($template, $data = [])
161
    {
162
        $this->selectMessagesTemplate();
163
164
        $data['version'] = $this->get('version');
165
        $data['dir'] = $this->dir();
166
        $data['flash'] = $this->get('flash')->display();
167
        $data['headerTitle'] = isset($data['title']) ? $data['title'] : '';
168
        $data['isLogged'] = $this->isLogged();
169
        $data['siteTitle'] = $this->config('title');
170
        $data['pageTitle'] = isset($data['title'])
171
            ? $data['title'].' '.$this->config('title_divider').' '.$this->config('title')
172
            : $this->config('title');
173
174
        $data['examsGroups'] = (new ExamsGroupModel($this->get('dbh')))->getExamsGroups();
175
176
        $loader = new Twig_Loader_Filesystem(
177
            $this->get('rootDir').'/resources/themes/'.$this->config('theme').'/templates/'
178
        );
179
        $twig = new Twig_Environment($loader, [
180
            'cache' => $this->config('cache') ? $this->get('rootDir').'/var/twig' : false,
181
            'debug' => $this->config('debug') ? true : false,
182
        ]);
183
184
        try {
185
            echo $twig->render($template.'.twig', $data);
186
        } catch (Exception $e) {
0 ignored issues
show
Unused Code introduced by
catch (\Exception $e) { ...ho 'Error 500'; } } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
187
            if ($this->config('debug')) {
188
                echo $e->getMessage();
189
            } else {
190
                echo 'Error 500';
191
            }
192
        }
193
    }
194
}
195