1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* AlertManager.php |
4
|
|
|
* Definition of class AlertManager |
5
|
|
|
* |
6
|
|
|
* Created 17/12/14 05:15 |
7
|
|
|
* |
8
|
|
|
* @author Rasanga Perera <[email protected]> |
9
|
|
|
* @copyright (c) 2014, The MIT License (MIT) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ras\Bundle\FlashAlertBundle\Model; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
16
|
|
|
|
17
|
|
|
class AlertManager implements AlertManagerInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Session |
21
|
|
|
*/ |
22
|
|
|
private $session; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param Session $session |
27
|
|
|
*/ |
28
|
|
|
public function __construct(Session $session) |
29
|
|
|
{ |
30
|
|
|
$this->session = $session; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inheritdoc |
35
|
|
|
*/ |
36
|
|
|
public function addAlert(AlertInterface $alert) |
37
|
|
|
{ |
38
|
|
|
$this->session->getFlashBag()->add($alert->getType(), $alert->getMessage()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
*/ |
44
|
|
|
public function getAlerts() |
45
|
|
|
{ |
46
|
|
|
$alerts = array(); |
47
|
|
|
|
48
|
|
|
foreach (self::getAlertTypes() as $type) { |
49
|
|
|
$messages = $this->session->getFlashBag()->get($type); |
50
|
|
|
|
51
|
|
|
if (!empty($messages)) { |
52
|
|
|
$alerts = array_merge($alerts, $this->createAlertsForType($type, $messages)) ; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $alerts; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Creates alert objects for a type |
61
|
|
|
* |
62
|
|
|
* @param string $type |
63
|
|
|
* @param array $messages |
64
|
|
|
* @return Alert[] |
65
|
|
|
*/ |
66
|
|
|
private function createAlertsForType($type, array $messages) |
67
|
|
|
{ |
68
|
|
|
$alerts = array(); |
69
|
|
|
|
70
|
|
|
foreach ($messages as $msg) { |
71
|
|
|
$alerts[] = new Alert($type, $msg); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $alerts; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Gets allowed alert types |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
private static function getAlertTypes() |
83
|
|
|
{ |
84
|
|
|
return array( |
85
|
|
|
AlertInterface::SUCCESS_ALERT, |
86
|
|
|
AlertInterface::ERROR_ALERT, |
87
|
|
|
AlertInterface::WARNING_ALERT, |
88
|
|
|
AlertInterface::INFO_ALERT |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
} |