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

AlertManager::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
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