Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
28 / 28 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
Globals | |
100.00% |
28 / 28 |
|
100.00% |
3 / 3 |
8 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getGlobals | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getGlobalValues | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
5 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Engelsystem\Renderer\Twig\Extensions; |
6 | |
7 | use Engelsystem\Helpers\Authenticator; |
8 | use Engelsystem\Helpers\DayOfEvent; |
9 | use Engelsystem\Http\Request; |
10 | use Twig\Extension\AbstractExtension as TwigExtension; |
11 | use Twig\Extension\GlobalsInterface as GlobalsInterface; |
12 | |
13 | use function array_key_exists; |
14 | |
15 | class Globals extends TwigExtension implements GlobalsInterface |
16 | { |
17 | protected array $globals = []; |
18 | |
19 | public function __construct(protected Authenticator $auth, protected Request $request) |
20 | { |
21 | } |
22 | |
23 | /** |
24 | * Returns a list of global variables to add to the existing list. |
25 | */ |
26 | public function getGlobals(): array |
27 | { |
28 | if (empty($this->globals)) { |
29 | $this->globals = $this->getGlobalValues(); |
30 | } |
31 | |
32 | return $this->globals; |
33 | } |
34 | |
35 | /** |
36 | * Generates the list of global variables |
37 | */ |
38 | protected function getGlobalValues(): array |
39 | { |
40 | $user = $this->auth->user(); |
41 | $themes = config('themes'); |
42 | $themeId = config('theme'); |
43 | $userMessages = null; |
44 | |
45 | if ($user) { |
46 | $themeId = $user->settings->theme; |
47 | $userMessages = $user |
48 | ->messagesReceived() |
49 | ->where('read', false) |
50 | ->count(); |
51 | } |
52 | |
53 | $query = $this->request->query->get('theme'); |
54 | if (!is_null($query) && isset($themes[(int) $query])) { |
55 | $themeId = (int) $query; |
56 | } |
57 | |
58 | if (array_key_exists($themeId, $themes) === false) { |
59 | $themeId = array_key_first($themes); |
60 | } |
61 | |
62 | $theme = $themes[$themeId]; |
63 | |
64 | return [ |
65 | 'user' => $user ?? [], |
66 | 'user_messages' => $userMessages, |
67 | 'request' => $this->request, |
68 | 'themeId' => $themeId, |
69 | 'theme' => $theme, |
70 | 'day_of_event' => DayOfEvent::get(), |
71 | ]; |
72 | } |
73 | } |