FlashService   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 99
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A message() 0 16 2
A success() 0 4 1
A info() 0 4 1
A warning() 0 4 1
A danger() 0 4 1
1
<?php
2
3
namespace Jalle19\Laravel\UnshittyFlash;
4
5
use Illuminate\Http\Request;
6
7
/**
8
 * Class FlashService
9
 * @package Jalle19\Laravel\UnshittyFlash
10
 */
11
class FlashService
12
{
13
14
    const LEVEL_SUCCESS = 'success';
15
    const LEVEL_INFO    = 'info';
16
    const LEVEL_WARNING = 'warning';
17
    const LEVEL_DANGER  = 'danger';
18
19
    /**
20
     * @var string
21
     */
22
    private $sessionKey;
23
24
25
    /**
26
     * FlashService constructor.
27
     *
28
     * @param array $configuration
29
     *
30
     * @throws \InvalidArgumentException
31
     */
32
    public function __construct(array $configuration)
33
    {
34
        if (!isset($configuration['session_key'])) {
35
            throw new \InvalidArgumentException('session_key cannot be empty');
36
        }
37
38
        $this->sessionKey = $configuration['session_key'];
39
    }
40
41
42
    /**
43
     * @param Request $request
44
     * @param string  $message
45
     * @param string  $level
46
     * @param bool    $sameRequest
47
     */
48
    public function message(Request $request, string $message, string $level, bool $sameRequest = false)
49
    {
50
        $session = $request->session();
51
52
        $data = $session->get($this->sessionKey, []);
53
54
        // Append the new message and store again
55
        $data[] = ['message' => $message, 'level' => $level];
56
57
        // Flash the message, either immediately or for the next request
58
        if ($sameRequest) {
59
            $session->now($this->sessionKey, $data);
0 ignored issues
show
Bug introduced by
The method now() does not seem to exist on object<Symfony\Component...ssion\SessionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
        } else {
61
            $session->flash($this->sessionKey, $data);
0 ignored issues
show
Bug introduced by
The method flash() does not seem to exist on object<Symfony\Component...ssion\SessionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
        }
63
    }
64
65
66
    /**
67
     * @param Request $request
68
     * @param string  $message
69
     * @param bool    $sameRequest
70
     */
71
    public function success(Request $request, string $message, bool $sameRequest = false)
72
    {
73
        $this->message($request, $message, self::LEVEL_SUCCESS, $sameRequest);
74
    }
75
76
77
    /**
78
     * @param Request $request
79
     * @param string  $message
80
     * @param bool    $sameRequest
81
     */
82
    public function info(Request $request, string $message, bool $sameRequest = false)
83
    {
84
        $this->message($request, $message, self::LEVEL_INFO, $sameRequest);
85
    }
86
87
88
    /**
89
     * @param Request $request
90
     * @param string  $message
91
     * @param bool    $sameRequest
92
     */
93
    public function warning(Request $request, string $message, bool $sameRequest = false)
94
    {
95
        $this->message($request, $message, self::LEVEL_WARNING, $sameRequest);
96
    }
97
98
99
    /**
100
     * @param Request $request
101
     * @param string  $message
102
     * @param bool    $sameRequest
103
     */
104
    public function danger(Request $request, string $message, bool $sameRequest = false)
105
    {
106
        $this->message($request, $message, self::LEVEL_DANGER, $sameRequest);
107
    }
108
109
}
110