Passed
Branch tests1.5 (e599bd)
by Wanderson
01:46
created

Alert   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A isType() 0 2 2
A __toString() 0 2 1
A isGroup() 0 2 2
A getBlock() 0 2 1
A create() 0 5 2
A __construct() 0 5 1
A load() 0 2 1
1
<?php
2
3
namespace Win\Alert;
4
5
use Win\Mvc\Block;
6
7
/**
8
 * Alertas
9
 * São mensagens armazenadas na sessão e exibidas ao usuárioTYPE
10
 */
11
abstract class Alert {
12
13
	public $type;
14
	public $group;
15
	public $message;
16
	protected static $file = 'layout/html/alert';
17
18
	const TYPE = '';
19
20
	/**
21
	 * Cria um novo alerta
22
	 * @param string $message
23
	 * @param string $group
24
	 */
25
	public function __construct($message, $group = '') {
26
		$this->message = $message;
27
		$this->type = static::TYPE;
28
		$this->group = $group;
29
		Session::addAlert($this);
30
	}
31
32
	/** @return string */
33
	public function __toString() {
34
		return $this->getBlock()->toString();
35
	}
36
37
	/** Exibe o conteúdo HTML do alerta */
38
	public function load() {
39
		return $this->getBlock()->load();
40
	}
41
42
	/** @return Block */
43
	protected function getBlock() {
44
		return new Block(static::$file, ['alert' => $this]);
45
	}
46
47
	/**
48
	 * Cria um alerta de erro ou sucesso, dependendo dos parâmetros
49
	 * Possibilitando criar um "AlertError" ou "AlertSuccess" em um único método
50
	 * @param string|null $error
51
	 * @param string $success
52
	 * @param string $group
53
	 * @return Alert
54
	 */
55
	public static function create($error, $success, $group = '') {
56
		if (!is_null($error)) {
57
			return new AlertError($error, $group);
58
		} else {
59
			return new AlertSuccess($success, $group);
60
		}
61
	}
62
63
	/**
64
	 * @param string $group
65
	 * @return boolean
66
	 */
67
	public function isGroup($group) {
68
		return ($group == '' || $group == $this->group);
69
	}
70
71
	/**
72
	 * @param string $type
73
	 * @return boolean
74
	 */
75
	public function isType($type) {
76
		return ($type == '' || $type == $this->type);
77
	}
78
79
}
80