Passed
Branch tests1.5 (59f3fd)
by Wanderson
02:32
created

Alert::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Win\Message;
4
5
use Win\DesignPattern\SingletonTrait;
6
use Win\Mvc\Block;
7
use Win\Request\Session;
8
9
/**
10
 * Alertas
11
 */
12
class Alert {
13
14
	/** @var Session */
15
	private $session;
16
17
	use SingletonTrait {
18
		instance as instanceSingleton;
19
	}
20
21
	/**
22
	 * @return static
23
	 * @param string $group
24
	 */
25
	public static function instance($group = 'default') {
26
		$instance = static::instanceSingleton();
27
		$instance->session = Session::instance('alerts.' . $group);
28
		return $instance;
29
	}
30
31
	/**
32
	 * Adiciona um mensagem de alerta
33
	 * @param string $message
34
	 * @param string $type
35
	 */
36
	public function add($message, $type = 'default') {
37
		$messages = $this->session->get($type, []);
38
		array_push($messages, $message);
39
		$this->session->set($type, $messages);
40
	}
41
42
	/** @return mixed[] */
43
	public function all() {
44
		return $this->session->all(true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type string expected by parameter $clear of Win\Request\Session::all(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
		return $this->session->all(/** @scrutinizer ignore-type */ true);
Loading history...
45
	}
46
47
	/**
48
	 * Retorna os alertas por $type
49
	 * @return mixed[]
50
	 * @param string $type
51
	 */
52
	public function get($type) {
53
		return $this->session->get($type, null, true);
54
	}
55
56
	/** Limpa mensagens */
57
	public function clear() {
58
		$this->session->clear();
59
	}
60
61
	/**
62
	 * Exibe html dos alertas
63
	 * @return Block
64
	 */
65
	public function html() {
66
		return new Block('layout/html/alerts', ['alerts' => $this->session->all(true)]);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type string expected by parameter $clear of Win\Request\Session::all(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
		return new Block('layout/html/alerts', ['alerts' => $this->session->all(/** @scrutinizer ignore-type */ true)]);
Loading history...
67
	}
68
69
	/** @param string $message */
70
	public static function alert($message) {
71
		static::instance()->add($message, 'default');
72
	}
73
74
	/** @param string $message */
75
	public static function success($message) {
76
		static::instance()->add($message, 'success');
77
	}
78
79
	/** @param string $message */
80
	public static function error($message) {
81
		static::instance()->add($message, 'danger');
82
	}
83
84
	/** @param string $message */
85
	public static function info($message) {
86
		static::instance()->add($message, 'info');
87
	}
88
89
	/** @param string $message */
90
	public static function warning($message) {
91
		static::instance()->add($message, 'warning');
92
	}
93
94
	/**
95
	 * Cria um alerta de erro ou sucesso em um único método
96
	 * @param string|null $error
97
	 * @param string $success
98
	 */
99
	public static function create($error, $success) {
100
		if (!is_null($error)) {
101
			static::error($error);
102
		} else {
103
			static::success($success);
104
		}
105
	}
106
107
}
108