Completed
Push — master ( 10bc21...7bd81c )
by Mikołaj
02:19
created

Controller::getFromRequest()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 2
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
        return;
45
    }
46
47
    /**
48
     * @param string $name Config name
49
     *
50
     * @return mixed Config value
51
     */
52
    public function config($name)
53
    {
54
        if (isset($this->get('config')[$name])) {
55
            return $this->get('config')[$name];
56
        }
57
58
        return;
59
    }
60
61
    /**
62
     * Get request variable.
63
     *
64
     * @param string $type Request type
65
     * @param string $name Index name. Null for all
66
     *
67
     * @return mixed
68
     */
69
    public function getFromRequest($type = 'get', $name = null)
70
    {
71
        $request = $this->get('request');
72
73
        // if unknow request type
74
        if (!isset($request[$type])) {
75
            return;
76
        }
77
78
        // for get all indexes from type
79
        if (null === $name) {
80
            return $request[$type];
81
        }
82
83
        if (isset($request[$type][$name])) {
84
            return $request[$type][$name];
85
        }
86
87
        return;
88
    }
89
90
    /**
91
     * @return string
92
     */
93
    public function dir()
94
    {
95
        return $this->get('dir');
96
    }
97
98
    /**
99
     * Check is user logged.
100
     *
101
     * @return bool Return true, if logged
102
     */
103
    public function isLogged()
104
    {
105
        return $this->get('auth')->isLogged();
106
    }
107
108
    /**
109
     * Terminate app.
110
     *
111
     * @param int $code Exit code
112
     *
113
     * @return void
114
     */
115
    public function terminate($code = 1)
116
    {
117
        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...
118
    }
119
120
    /**
121
     * @param string $path    Path to redirect
122
     * @param string $type    Message type
123
     * @param mixed  $message Message content
124
     *
125
     * @return void
126
     */
127
    public function redirectWithMessage($path, $type = 'success', $message = 'Success')
128
    {
129
        switch ($type) {
130
            case 'success':
131
                $this->get('flash')->success($message);
132
                break;
133
            case 'info':
134
                $this->get('flash')->info($message);
135
                break;
136
            case 'warning':
137
                $this->get('flash')->warning($message);
138
                break;
139
            case 'error':
140
                $this->get('flash')->error($message);
141
                break;
142
143
            default:
144
                $this->get('flash')->error($message);
145
                break;
146
        }
147
148
        header('Location: '.$this->dir().$path);
149
150
        $this->terminate();
151
    }
152
153
    private function selectMessagesTemplate()
154
    {
155
        switch ($this->config('theme')) {
156
            case 'mdl':
157
                $this->get('flash')->setTemplate(new MaterialDesignLite());
158
                break;
159
        }
160
    }
161
162
    /**
163
     * @param string $template Template name
164
     * @param array  $data     Data to use in template
165
     */
166
    public function render($template, $data = [])
167
    {
168
        $this->selectMessagesTemplate();
169
170
        $data['version'] = $this->get('version');
171
        $data['dir'] = $this->dir();
172
        $data['flash'] = $this->get('flash')->display();
173
        $data['headerTitle'] = isset($data['title']) ? $data['title'] : '';
174
        $data['isLogged'] = $this->isLogged();
175
        $data['siteTitle'] = $this->config('title');
176
        $data['pageTitle'] = isset($data['title'])
177
            ? $data['title'].' '.$this->config('title_divider').' '.$this->config('title')
178
            : $this->config('title');
179
180
        $data['examsGroups'] = (new ExamsGroupModel($this->get('dbh')))->getExamsGroups();
181
182
        $loader = new Twig_Loader_Filesystem(
183
            $this->get('rootDir').'/resources/themes/'.$this->config('theme').'/templates/'
184
        );
185
        $twig = new Twig_Environment($loader, [
186
            'cache' => $this->config('cache') ? $this->get('rootDir').'/var/twig' : false,
187
            'debug' => $this->config('debug') ? true : false,
188
        ]);
189
190
        try {
191
            echo $twig->render($template.'.twig', $data);
192
        } 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...
193
            if ($this->config('debug')) {
194
                echo $e->getMessage();
195
            } else {
196
                echo 'Error 500';
197
            }
198
        }
199
    }
200
}
201