FlashMessages   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
eloc 23
c 2
b 0
f 0
dl 0
loc 72
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A warning() 0 3 1
A error() 0 3 1
A info() 0 3 1
A getFlashBag() 0 11 3
A success() 0 3 1
A add() 0 11 2
A __construct() 0 4 1
A all() 0 3 1
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace FSi\Bundle\AdminBundle\Message;
13
14
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
15
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
16
use Symfony\Component\HttpFoundation\Session\SessionInterface;
17
18
class FlashMessages
19
{
20
    /**
21
     * @var FlashBagInterface
22
     */
23
    private $flashBag;
24
25
    /**
26
     * @var string
27
     */
28
    private $prefix;
29
30
    /**
31
     * @var SessionInterface
32
     */
33
    private $session;
34
35
    public function __construct(SessionInterface $session, string $prefix)
36
    {
37
        $this->prefix = $prefix;
38
        $this->session = $session;
39
    }
40
41
    public function success(string $message, array $params = [], string $domain = 'FSiAdminBundle')
42
    {
43
        $this->add('success', $message, $params, $domain);
44
    }
45
46
    public function error(string $message, array $params = [], string $domain = 'FSiAdminBundle')
47
    {
48
        $this->add('error', $message, $params, $domain);
49
    }
50
51
    public function warning(string $message, array $params = [], string $domain = 'FSiAdminBundle')
52
    {
53
        $this->add('warning', $message, $params, $domain);
54
    }
55
56
    public function info(string $message, array $params = [], string $domain = 'FSiAdminBundle')
57
    {
58
        $this->add('info', $message, $params, $domain);
59
    }
60
61
    public function all(): array
62
    {
63
        return $this->getFlashBag()->get($this->prefix);
64
    }
65
66
    private function add(string $type, string $message, array $params, string $domain): void
67
    {
68
        if ($this->getFlashBag()->has($this->prefix)) {
69
            $messages = $this->getFlashBag()->get($this->prefix);
70
        } else {
71
            $messages = [];
72
        }
73
74
        $messages[$type][] = ['text' => $message, 'domain' => $domain, 'params' => $params];
75
76
        $this->flashBag->set($this->prefix, $messages);
77
    }
78
79
    private function getFlashBag(): FlashBagInterface
80
    {
81
        if ($this->flashBag instanceof FlashBagInterface) {
0 ignored issues
show
introduced by
$this->flashBag is always a sub-type of Symfony\Component\HttpFo...Flash\FlashBagInterface.
Loading history...
82
            return $this->flashBag;
83
        }
84
85
        $this->flashBag = method_exists($this->session, 'getFlashBag')
86
            ? $this->session->getFlashBag()
87
            : new FlashBag();
88
89
        return $this->flashBag;
90
    }
91
}
92