Completed
Push — master ( 688f64...482ee6 )
by Adam
02:00
created

Control::beforeRender()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 26
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6.027

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 26
ccs 10
cts 11
cp 0.9091
rs 8.439
cc 6
eloc 11
nc 13
nop 0
crap 6.027
1
<?php
2
/**
3
 * Component.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        http://www.ipublikuj.eu
7
 * @author         Adam Kadlec http://www.ipublikuj.eu
8
 * @package        iPublikuj:FlashMessages!
9
 * @subpackage     Components
10
 * @since          1.0.0
11
 *
12
 * @date           12.03.14
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\FlashMessages\Components;
18
19
use Nette;
20
use Nette\Application;
21
use Nette\Localization;
22
use Nette\Utils;
23
24
use IPub;
25
use IPub\FlashMessages;
26
use IPub\FlashMessages\Entities;
27
use IPub\FlashMessages\Exceptions;
28
use IPub\FlashMessages\Storage;
29
30
/**
31
 * Flash messages control
32
 *
33
 * @package        iPublikuj:FlashMessages!
34
 * @subpackage     Components
35
 *
36
 * @author         Adam Kadlec http://www.ipublikuj.eu
37
 *
38
 * @property Application\UI\ITemplate $template
39
 */
40 1
class Control extends Application\UI\Control
41
{
42
	/**
43
	 * @var string
44
	 */
45
	private $templateFile;
46
47
	/**
48
	 * @var Storage\IStorage
49
	 */
50
	private $storage;
51
52
	/**
53
	 * @var Localization\ITranslator
54
	 */
55
	private $translator;
56
57
	/**
58
	 * @var bool
59
	 */
60
	private $useTitle = FALSE;
61
62
	/**
63
	 * @var bool
64
	 */
65
	private $useOverlay = FALSE;
66
67
	/**
68
	 * @param Localization\ITranslator $translator
69
	 */
70
	public function injectTranslator(Localization\ITranslator $translator = NULL)
71
	{
72 1
		$this->translator = $translator;
73 1
	}
74
75
	/**
76
	 * @param NULL|string $templateFile
77
	 * @param Storage\IStorage $storage
78
	 *
79
	 * @throws Exceptions\FileNotFoundException
80
	 */
81
	public function __construct(
82
		$templateFile = NULL,
83
		Storage\IStorage $storage
84
	) {
85 1
		parent::__construct();
86
87 1
		if ($templateFile !== NULL) {
88
			$this->setTemplateFile($templateFile);
89
		}
90
91 1
		$this->storage = $storage;
92 1
	}
93
94
	/**
95
	 * @param \Nette\ComponentModel\IComponent
96
	 */
97
	public function attached($presenter)
98
	{
99 1
		parent::attached($presenter);
100
101 1
		$this->redrawControl();
102 1
	}
103
104
	/**
105
	 * @return void
106
	 */
107
	public function enableTitle()
108
	{
109 1
		$this->useTitle = TRUE;
110 1
	}
111
112
	/**
113
	 * @return void
114
	 */
115
	public function disableTitle()
116
	{
117 1
		$this->useTitle = FALSE;
118 1
	}
119
120
	/**
121
	 * @return void
122
	 */
123
	public function enableOverlay()
124
	{
125 1
		$this->useOverlay = TRUE;
126 1
	}
127
128
	/**
129
	 * @return void
130
	 */
131
	public function disableOverlay()
132
	{
133 1
		$this->useOverlay = FALSE;
134 1
	}
135
136
	/**
137
	 * Prepare component for rendering
138
	 */
139
	public function beforeRender()
140
	{
141
		// Check if control has template
142 1
		if ($this->template instanceof Nette\Bridges\ApplicationLatte\Template) {
143
			// Load messages from session
144
			/** @var Entities\IMessage[] $messages */
145 1
			$messages = $this->storage->get(Storage\IStorage::KEY_MESSAGES, []);
146
147
			// Assign vars to template
148 1
			$this->template->flashes = $messages ? $messages : [];
149 1
			$this->template->useTitle = $this->useTitle;
150 1
			$this->template->useOverlay = $this->useOverlay;
151
152
			// Check if translator is available
153 1
			if ($this->getTranslator() instanceof Localization\ITranslator) {
154
				$this->template->setTranslator($this->getTranslator());
155
			}
156
157
			// If template was not defined before...
158 1
			if ($this->template->getFile() === NULL) {
159
				// ...try to get base component template file
160 1
				$templateFile = !empty($this->templateFile) ? $this->templateFile : __DIR__ . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . 'default' . DIRECTORY_SEPARATOR . 'default.latte';
161 1
				$this->template->setFile($templateFile);
162
			}
163
		}
164 1
	}
165
166
	/**
167
	 * Render control
168
	 */
169
	public function render()
170
	{
171
		// Check if control has template
172 1
		if ($this->template instanceof Nette\Bridges\ApplicationLatte\Template) {
173 1
			$this->beforeRender();
174
175
			// Render component template
176 1
			$this->template->render();
177
178
		} else {
179
			throw new Exceptions\InvalidStateException('Flash messages control is without template.');
180
		}
181 1
	}
182
183
	/**
184
	 * Change default control template path
185
	 *
186
	 * @param string $templateFile
187
	 *
188
	 * @return void
189
	 *
190
	 * @throws Exceptions\FileNotFoundException
191
	 */
192
	public function setTemplateFile($templateFile)
193
	{
194
		// Check if template file exists...
195 1
		if (!is_file($templateFile)) {
196
			// Get component actual dir
197 1
			$dir = dirname($this->getReflection()->getFileName());
198
199 1
			$templateName = preg_replace('/.latte/', '', $templateFile);
200
201
			// ...check if extension template is used
202 1
			if (is_file($dir . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateName . DIRECTORY_SEPARATOR . 'default.latte')) {
203 1
				$templateFile = $dir . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateName . DIRECTORY_SEPARATOR . 'default.latte';
204
205
			} else {
206
				// ...if not throw exception
207 1
				throw new Exceptions\FileNotFoundException(sprintf('Template file "%s" was not found.', $templateFile));
208
			}
209
		}
210
211 1
		$this->templateFile = $templateFile;
212 1
	}
213
214
	/**
215
	 * @param Localization\ITranslator $translator
216
	 *
217
	 * @return void
218
	 */
219
	public function setTranslator(Localization\ITranslator $translator)
220
	{
221
		$this->translator = $translator;
222
	}
223
224
	/**
225
	 * @return Localization\ITranslator|null
226
	 */
227
	public function getTranslator()
228
	{
229 1
		if ($this->translator instanceof Localization\ITranslator) {
230
			return $this->translator;
231
		}
232
233 1
		return NULL;
234
	}
235
}
236