Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
HasUserNotifications
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 addNotification
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 getNotifications
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace Engelsystem\Controllers;
6
7use Illuminate\Support\Collection;
8
9trait HasUserNotifications
10{
11    protected function addNotification(string|array $value, NotificationType $type = NotificationType::MESSAGE): void
12    {
13        $type = 'messages.' . $type->value;
14        session()->set(
15            $type,
16            array_merge_recursive(session()->get($type, []), (array) $value)
17        );
18    }
19
20    /**
21     * @param NotificationType[]|null $types
22     * @return array<string,Collection|array<string>>
23     */
24    protected function getNotifications(array $types = null): array
25    {
26        $return = [];
27        $types = $types ?: [
28            NotificationType::ERROR,
29            NotificationType::WARNING,
30            NotificationType::INFORMATION,
31            NotificationType::MESSAGE,
32        ];
33
34        foreach ($types as $type) {
35            $type = $type->value;
36            $path = 'messages.' . $type;
37            $return[$type] = Collection::make(
38                session()->get($path, [])
39            )->flatten();
40            session()->remove($path);
41        }
42
43        return $return;
44    }
45}