Passed
Branch scrutinizer (391c16)
by Wanderson
01:43
created

Session   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getAlerts() 0 8 5
A setAutoSave() 0 2 1
A showAlerts() 0 4 2
A addAlert() 0 3 2
A clearAlerts() 0 2 1
A hasAlert() 0 2 1
1
<?php
2
3
namespace Win\Alert;
4
5
/**
6
 * Armazena e exibe Alertas
7
 */
8
class Session {
9
10
	private static $autoSave = true;
11
12
	/**
13
	 * Adiciona o alerta na sessão
14
	 * @param Alert $alert
15
	 */
16
	public static function addAlert(Alert $alert) {
17
		if (static::$autoSave) {
0 ignored issues
show
Bug introduced by
Since $autoSave is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $autoSave to at least protected.
Loading history...
18
			$_SESSION['alerts'][] = $alert;
19
		}
20
	}
21
22
	/**
23
	 * Liga/Desliga o salvamento automático de alertas na sessão
24
	 * @param boolean $mode
25
	 */
26
	public static function setAutoSave($mode = true) {
27
		static::$autoSave = $mode;
0 ignored issues
show
Bug introduced by
Since $autoSave is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $autoSave to at least protected.
Loading history...
28
	}
29
30
	/**
31
	 * Mostra os alertas criados, podendo filtrar por $type e/ou $group
32
	 * Removendo-os da sessão
33
	 * @param string $type
34
	 * @param string $group
35
	 */
36
	public static function showAlerts($type = '', $group = '') {
37
		foreach (static::getAlerts($type, $group) as $i => $alert) {
38
			$alert->load();
39
			unset($_SESSION['alerts'][$i]);
40
		}
41
	}
42
43
	/**
44
	 * Retorna alertas criados, podendo filtrar por $type e/ou $group
45
	 * @param string $type
46
	 * @param string $group
47
	 * @return Alert[]
48
	 */
49
	public static function getAlerts($type = '', $group = '') {
50
		$alerts = isset($_SESSION['alerts']) ? array_unique($_SESSION['alerts']) : [];
51
		foreach ($alerts as $i => $alert) {
52
			if (!$alert->isType($type) || !$alert->isGroup($group)) {
53
				unset($alerts[$i]);
54
			}
55
		}
56
		return $alerts;
57
	}
58
59
	/**
60
	 * Remove todos os alertas da sessão
61
	 */
62
	public static function clearAlerts() {
63
		unset($_SESSION['alerts']);
64
	}
65
66
	/**
67
	 * Retorna TRUE se a sessão possui algum alerta
68
	 * @return boolean
69
	 */
70
	public static function hasAlert() {
71
		return (count(static::getAlerts()) > 0);
72
	}
73
74
}
75