Passed
Push — master ( c632e6...9034d7 )
by Dāvis
05:37
created

AlertManager   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAlerts() 0 11 3
A createAlertsForType() 0 8 2
A getAlertTypes() 0 7 1
A addAlert() 0 3 1
A __construct() 0 6 2
1
<?php
2
3
namespace Sludio\HelperBundle\Flash\Model;
4
5
use Symfony\Component\HttpFoundation\Session\Session;
6
7
class AlertManager implements AlertManagerInterface
8
{
9
    private $session;
10
11
    public function __construct(Session $session)
12
    {
13
        if (!$session->isStarted()) {
14
            $session->start();
15
        }
16
        $this->session = $session;
17
    }
18
19
    public function addAlert(AlertInterface $alert)
20
    {
21
        $this->session->getFlashBag()->add($alert->getType(), $alert->getMessage());
22
    }
23
24
    public function getAlerts()
25
    {
26
        $alerts = [];
27
        foreach (self::getAlertTypes() as $type) {
28
            $messages = $this->session->getFlashBag()->get($type);
29
            if (!empty($messages)) {
30
                $alerts = array_merge($alerts, $this->createAlertsForType($type, $messages));
31
            }
32
        }
33
34
        return $alerts;
35
    }
36
37
    private function createAlertsForType($type, array $messages)
38
    {
39
        $alerts = [];
40
        foreach ($messages as $msg) {
41
            $alerts[] = new Alert($type, $msg);
42
        }
43
44
        return $alerts;
45
    }
46
47
    public static function getAlertTypes()
48
    {
49
        return [
50
            AlertInterface::SUCCESS,
51
            AlertInterface::ERROR,
52
            AlertInterface::WARNING,
53
            AlertInterface::INFO,
54
        ];
55
    }
56
}
57