AbstractController::isLogged()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Egzaminer\Controller;
4
5
use Egzaminer\Model\ExamsGroupModel;
6
use Egzaminer\Themes\MaterialDesignLite;
7
use Exception;
8
use Twig_Environment;
9
use Twig_Loader_Filesystem;
10
11
abstract class AbstractController
12
{
13
    /**
14
     * @var array
15
     */
16
    private $container;
17
18
    /**
19
     * @var array
20
     */
21
    protected $data;
22
23
    public function __construct(array $container)
24
    {
25
        $this->container = $container;
26
    }
27
28
    /**
29
     * @param string $name Item name
30
     *
31
     * @return mixed Item from container
32
     */
33
    public function get(string $name)
34
    {
35
        if (!isset($this->container[$name])) {
36
            return;
37
        }
38
39
        return $this->container[$name];
40
    }
41
42
    /**
43
     * @param string $name Config name
44
     *
45
     * @return mixed Config value
46
     */
47
    public function config(string $name)
48
    {
49
        if (!isset($this->get('config')[$name])) {
50
            return;
51
        }
52
53
        return $this->get('config')[$name];
54
    }
55
56
    /**
57
     * Get request variable.
58
     *
59
     * @param string $type Request type
60
     * @param string $name Index name. Null for all
61
     *
62
     * @return mixed
63
     */
64
    public function getFromRequest(string $type = 'get', string $name = null)
65
    {
66
        $request = $this->get('request');
67
68
        // if unknown request type
69
        if (!isset($request[$type])) {
70
            return;
71
        }
72
73
        // for get all indexes from type
74
        if (null === $name) {
75
            return $request[$type];
76
        }
77
78
        if (isset($request[$type][$name])) {
79
            return $request[$type][$name];
80
        }
81
    }
82
83
    public function dir(): string
84
    {
85
        return $this->get('dir');
86
    }
87
88
    public function isLogged(): bool
89
    {
90
        return $this->get('auth')->isLogged();
91
    }
92
93
    /**
94
     * @param string $type    Message type
95
     * @param string $message Message content
96
     *
97
     * @return void
98
     */
99
    public function setMessage($type = 'success', $message = 'Success')
100
    {
101
        switch ($type) {
102
            case 'success':
103
                $this->get('flash')->success($message);
104
                break;
105
            case 'info':
106
                $this->get('flash')->info($message);
107
                break;
108
            case 'warning':
109
                $this->get('flash')->warning($message);
110
                break;
111
            case 'error':
112
                $this->get('flash')->error($message);
113
                break;
114
115
            default:
116
                $this->get('flash')->error($message);
117
                break;
118
        }
119
    }
120
121
    /**
122
     * Redirect.
123
     *
124
     * @param string $path Path to redirect
125
     *
126
     * @return void
127
     */
128
    public function redirect($path)
129
    {
130
        header('Location: '.$this->dir().$path);
131
    }
132
133
    public function terminate($code = 1)
134
    {
135
        exit($code);
136
    }
137
138
    private function selectMessagesTemplate()
139
    {
140
        switch ($this->config('theme')) {
141
            case 'mdl':
142
                $this->get('flash')->setTemplate(new MaterialDesignLite());
143
                break;
144
        }
145
    }
146
147
    public function render(string $template, array $data = [])
148
    {
149
        $this->selectMessagesTemplate();
150
151
        $data['version'] = $this->get('version');
152
        $data['dir'] = $this->dir();
153
        $data['flash'] = $this->get('flash')->display();
154
        $data['headerTitle'] = $data['title'] ?? '';
155
        $data['isLogged'] = $this->isLogged();
156
        $data['siteTitle'] = $this->config('title');
157
        $data['pageTitle'] = isset($data['title'])
158
            ? $data['title'].' '.$this->config('title_divider').' '.$this->config('title')
159
            : $this->config('title');
160
161
        $data['examsGroups'] = (new ExamsGroupModel($this->get('dbh')))->getExamsGroups();
162
163
        $loader = new Twig_Loader_Filesystem(
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Loader_Filesystem has been deprecated with message: since Twig 2.7, use "Twig\Loader\FilesystemLoader" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
164
            $this->get('rootDir').'/resources/themes/'.$this->config('theme').'/templates/'
165
        );
166
        $twig = new Twig_Environment($loader, [
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Environment has been deprecated with message: since Twig 2.7, use "Twig\Environment" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
167
            'cache' => $this->config('cache') ? $this->get('rootDir').'/var/twig' : false,
168
            'debug' => $this->config('debug') ? true : false,
169
        ]);
170
171
        try {
172
            return $twig->render($template.'.twig', $data);
173
        } catch (Exception $e) {
174
            if ($this->config('debug')) {
175
                echo $e->getMessage();
176
            } else {
177
                echo 'Error 500';
178
            }
179
180
            return false;
181
        }
182
    }
183
}
184