Passed
Push — master ( db039c...4d8c20 )
by Dāvis
04:33
created

AlertManager::addAlert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sludio\HelperBundle\Script\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
        $this->session = $session;
14
    }
15
16
    public function addAlert(AlertInterface $alert)
17
    {
18
        $this->session->getFlashBag()->add($alert->getType(), $alert->getMessage());
19
    }
20
21
    public function getAlerts()
22
    {
23
        $alerts = [];
24
        foreach (self::getAlertTypes() as $type) {
25
            $messages = $this->session->getFlashBag()->get($type);
26
            if (!empty($messages)) {
27
                $alerts = array_merge($alerts, $this->createAlertsForType($type, $messages));
28
            }
29
        }
30
31
        return $alerts;
32
    }
33
34
    private function createAlertsForType($type, array $messages)
35
    {
36
        $alerts = [];
37
        foreach ($messages as $msg) {
38
            $alerts[] = new Alert($type, $msg);
39
        }
40
41
        return $alerts;
42
    }
43
44
    public static function getAlertTypes()
45
    {
46
        return [
47
            AlertInterface::SUCCESS,
48
            AlertInterface::ERROR,
49
            AlertInterface::WARNING,
50
            AlertInterface::INFO,
51
        ];
52
    }
53
}
54