Completed
Push — master ( 35046b...d215ab )
by Mikołaj
03:13
created

Controller::selectMessagesTemplate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace Egzaminer;
4
5
use Egzaminer\Roll\ExamsGroupModel;
6
use Exception;
7
use Twig_Environment;
8
use Twig_Loader_Filesystem;
9
use Egzaminer\Themes\MaterialDesignLite;
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
        return $this->container[$name];
41
    }
42
43
    /**
44
     * @param string $name Config name
45
     *
46
     * @return mixed Config value
47
     */
48
    public function config($name)
49
    {
50
        return $this->get('config')[$name];
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function dir()
57
    {
58
        return $this->get('dir');
59
    }
60
61
    /**
62
     * Check is user logged.
63
     *
64
     * @return bool Return true, if logged
65
     */
66
    public function isLogged()
67
    {
68
        return $this->get('auth')->isLogged();
69
    }
70
71
    public function terminate($code = 1)
72
    {
73
        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...
74
    }
75
76
    /**
77
     * @param string $path Path to redirect
78
     * @param string $type Message type
79
     * @param mixed  $message Message content
80
     *
81
     * @return void
82
     */
83
    public function redirectWithMessage($path, $type = 'success', $message = 'Success')
84
    {
85
        switch ($type) {
86
            case 'success':
87
                $this->get('flash')->success($message);
88
                break;
89
            case 'info':
90
                $this->get('flash')->info($message);
91
                break;
92
            case 'warning':
93
                $this->get('flash')->warning($message);
94
                break;
95
            case 'error':
96
                $this->get('flash')->error($message);
97
                break;
98
99
            default:
100
                $this->get('flash')->error($message);
101
                break;
102
        }
103
104
        header('Location: '.$this->dir().$path);
105
106
        $this->terminate();
107
    }
108
109
    private function selectMessagesTemplate()
110
    {
111
        switch ($this->config('theme')) {
112
            case 'mdl':
113
                $this->get('flash')->setTemplate(new MaterialDesignLite());
114
                break;
115
        }
116
    }
117
118
    /**
119
     * @param string $template Template name
120
     * @param array  $data     Data to use in template
121
     */
122
    public function render($template, $data = [])
123
    {
124
        $this->selectMessagesTemplate();
125
126
        $data['flash'] = $this->get('flash')->display();
127
        $data['dir'] = $this->dir();
128
        $data['siteTitle'] = $this->config('title');
129
        $data['isLogged'] = $this->isLogged();
130
        $data['headerTitle'] = isset($data['title']) ? $data['title'] : '';
131
        $data['pageTitle'] = isset($data['title'])
132
            ? $data['title'].' '.$this->config('title_divider').' '.$this->config('title')
133
            : $this->config('title');
134
135
        $data['examsGroups'] = (new ExamsGroupModel($this->get('dbh')))->getExamsGroups();
136
137
        $loader = new Twig_Loader_Filesystem(
138
            $this->get('rootDir').'/resources/themes/'.$this->config('theme').'/templates/'
139
        );
140
        $twig = new Twig_Environment($loader, [
141
            'cache' => $this->config('cache') ? $this->get('rootDir').'/var/twig' : false,
142
            'debug' => $this->config('debug') ? true : false,
143
        ]);
144
145
        try {
146
            echo $twig->render($template.'.twig', $data);
147
        } 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...
148
            if ($this->config('debug')) {
149
                echo $e->getMessage();
150
            } else {
151
                echo 'Error 500';
152
            }
153
        }
154
    }
155
}
156