Passed
Push — master ( 668422...591315 )
by Jean-Christophe
20:17
created

FlashBag::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Ubiquity\utils\flash;
4
5
use Ubiquity\controllers\Controller;
6
use Ubiquity\events\EventsManager;
7
use Ubiquity\events\ViewEvents;
8
use Ubiquity\utils\http\USession;
9
10
/**
11
 * Bag for Session Flash messages
12
 * Ubiquity\utils\flash$FlashBag
13
 * This class is part of Ubiquity
14
 *
15
 * @author jcheron <[email protected]>
16
 * @version 1.0.2
17
 *
18
 */
19
class FlashBag implements \Iterator {
20
	const FLASH_BAG_KEY = '_flash_bag';
21
	const VAR_VIEW_NAME='flashMessages';
22
	private array $array;
23
	private int $position = 0;
24
	private bool $autoClear;
25
26 7
	public function __construct(bool $autoClear=true) {
27 7
		$this->array = USession::get ( self::FLASH_BAG_KEY, [ ] );
28 7
		$this->autoClear=$autoClear;
29 7
		EventsManager::addListener(ViewEvents::BEFORE_RENDER,function($_, &$data) {
30
			$data[self::VAR_VIEW_NAME]=$this->array;
31
		});
32 7
		EventsManager::addListener(ViewEvents::AFTER_RENDER,function(){
33
			if($this->autoClear) {
34
				$this->clear();
35
			}
36
		});
37
	}
38
39
	/**
40
	 * Adds a temporary new message to the bag.
41
	 * @param string $content
42
	 * @param string|null $title
43
	 * @param string $type
44
	 * @param string|null $icon
45
	 */
46 7
	public function addMessage(string $content, string $title = NULL, string $type = 'info', string $icon = null): void {
47 7
		$this->array [] = new FlashMessage ( $content, $title, $type, $icon );
48
	}
49
50
	/**
51
	 * Adds and saves a message in the bag.
52
	 * @param string $content
53
	 * @param string|null $title
54
	 * @param string $type
55
	 * @param string|null $icon
56
	 */
57
	public function addMessageAndSave(string $content, string $title = NULL, string $type = 'info', string $icon = null): void  {
58
		$this->addMessage($content,$title,$type,$icon);
59
		USession::set ( self::FLASH_BAG_KEY, $this->array );
60
	}
61
62
	/**
63
	 * Returns all the message of a type in the bag.
64
	 * @param string $type
65
	 * @return FlashMessage[]
66
	 */
67 1
	public function getMessages(string $type): array {
68 1
		$result = [ ];
69 1
		foreach ( $this->array as $msg ) {
70 1
			if ($msg->getType () == $type) {
71 1
				$result [] = $msg;
72
			}
73
		}
74 1
		return $result;
75
	}
76
77
	/**
78
	 * Returns all the messages.
79
	 * @return array
80
	 */
81 5
	public function getAll(): array {
82 5
		return $this->array;
83
	}
84
85
	/**
86
	 * Clears the bag.
87
	 */
88 1
	public function clear(): void {
89 1
		$this->array = [ ];
90 1
		USession::delete ( self::FLASH_BAG_KEY );
91
	}
92
93 1
	public function rewind(): void {
94 1
		$this->position = 0;
95
	}
96
97
	/**
98
	 *
99
	 * @return FlashMessage
100
	 */
101 2
	public function current(): FlashMessage {
102 2
		return $this->array [$this->position];
103
	}
104
105 1
	public function key(): int {
106 1
		return $this->position;
107
	}
108
109 2
	public function next(): void {
110 2
		++ $this->position;
111
	}
112
113 1
	public function valid(): bool {
114 1
		return isset ( $this->array [$this->position] );
115
	}
116
117 1
	public function save(): void {
118 1
		$this->array = USession::set ( self::FLASH_BAG_KEY, $this->array );
119
	}
120
121
	/**
122
	 * @return bool
123
	 */
124
	public function isAutoClear(): bool {
125
		return $this->autoClear;
126
	}
127
128
	/**
129
	 * @param bool $autoClear
130
	 */
131
	public function setAutoClear(bool $autoClear): void {
132
		$this->autoClear = $autoClear;
133
	}
134
}
135
136