Test Failed
Pull Request — master (#5)
by
unknown
03:46
created

Control::injectTranslator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Component.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        https://www.ipublikuj.eu
7
 * @author         Adam Kadlec <[email protected]>
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\Application;
20
use Nette\Bridges;
21
use Nette\ComponentModel;
22
use Nette\Localization;
23
24
use IPub\FlashMessages\Entities;
25
use IPub\FlashMessages\Exceptions;
26
use IPub\FlashMessages\Storage;
27
28
/**
29
 * Flash messages control
30
 *
31
 * @package        iPublikuj:FlashMessages!
32
 * @subpackage     Components
33
 *
34
 * @author         Adam Kadlec <[email protected]>
35
 *
36
 * @property Application\UI\ITemplate $template
37
 */
38
class Control extends Application\UI\Control
39
{
40
	/**
41
	 * @var string
42
	 */
43
	private $templateFile;
44
45
	/**
46
	 * @var Storage\IStorage
47
	 */
48
	private $storage;
49
50
	/**
51
	 * @var Localization\ITranslator|NULL
52
	 */
53
	private $translator;
54
55
	/**
56
	 * @var bool
57
	 */
58
	private $useTitle = FALSE;
59
60
	/**
61
	 * @var bool
62
	 */
63
	private $useOverlay = FALSE;
64
65
	/**
66
	 * @param Localization\ITranslator|NULL $translator
67
	 *
68
	 * @return void
69
	 */
70
	public function injectTranslator(?Localization\ITranslator $translator = NULL) : void
71
	{
72
		$this->translator = $translator;
73
	}
74
75
	/**
76
	 * @param string|NULL $templateFile
77
	 * @param Storage\IStorage $storage
78
	 *
79
	 * @throws Exceptions\FileNotFoundException
80
	 */
81
	public function __construct(
82
		string $templateFile = NULL,
83
		Storage\IStorage $storage
84
	) {
85
		if ($templateFile !== NULL) {
86
			$this->setTemplateFile($templateFile);
87
		}
88
89
		$this->storage = $storage;
90
	}
91
92
	/**
93
	 * @param ComponentModel\IComponent
94
	 *
95
	 * @return void
96
	 */
97
	public function attached($presenter) : void
98
	{
99
		parent::attached($presenter);
0 ignored issues
show
Deprecated Code introduced by
The method Nette\ComponentModel\Component::attached() has been deprecated with message: use monitor($type, $attached)

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
100
101
		$this->redrawControl();
102
	}
103
104
	/**
105
	 * @return void
106
	 */
107
	public function enableTitle() : void
108
	{
109
		$this->useTitle = TRUE;
110
	}
111
112
	/**
113
	 * @return void
114
	 */
115
	public function disableTitle() : void
116
	{
117
		$this->useTitle = FALSE;
118
	}
119
120
	/**
121
	 * @return void
122
	 */
123
	public function enableOverlay() : void
124
	{
125
		$this->useOverlay = TRUE;
126
	}
127
128
	/**
129
	 * @return void
130
	 */
131
	public function disableOverlay() : void
132
	{
133
		$this->useOverlay = FALSE;
134
	}
135
136
	/**
137
	 * Prepare component for rendering
138
	 *
139
	 * @return void
140
	 */
141
	public function beforeRender() : void
142
	{
143
		// Check if control has template
144
		if ($this->template instanceof Bridges\ApplicationLatte\Template) {
145
			// Load messages from session
146
			/** @var Entities\IMessage[] $messages */
147
			$messages = $this->storage->get(Storage\IStorage::KEY_MESSAGES, []);
148
149
			// Assign vars to template
150
			$this->template->flashes = $messages ? $messages : [];
151
			$this->template->useTitle = $this->useTitle;
152
			$this->template->useOverlay = $this->useOverlay;
153
154
			// Check if translator is available
155
			if ($this->getTranslator() instanceof Localization\ITranslator) {
156
				$this->template->setTranslator($this->getTranslator());
157
			}
158
159
			// If template was not defined before...
160
			if ($this->template->getFile() === NULL) {
161
				// ...try to get base component template file
162
				$templateFile = !empty($this->templateFile) ? $this->templateFile : __DIR__ . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . 'default' . DIRECTORY_SEPARATOR . 'default.latte';
163
				$this->template->setFile($templateFile);
164
			}
165
		}
166
	}
167
168
	/**
169
	 * Render control
170
	 *
171
	 * @return void
172
	 */
173
	public function render() : void
174
	{
175
		// Check if control has template
176
		if ($this->template instanceof Bridges\ApplicationLatte\Template) {
177
			$this->beforeRender();
178
179
			// Render component template
180
			$this->template->render();
181
182
		} else {
183
			throw new Exceptions\InvalidStateException('Flash messages control is without template.');
184
		}
185
	}
186
187
	/**
188
	 * Change default control template path
189
	 *
190
	 * @param string $templateFile
191
	 *
192
	 * @return void
193
	 *
194
	 * @throws Exceptions\FileNotFoundException
195
	 */
196
	public function setTemplateFile(string $templateFile) : void
197
	{
198
		// Check if template file exists...
199
		if (!is_file($templateFile)) {
200
			// Get component actual dir
201
			$dir = dirname($this->getReflection()->getFileName());
202
203
			$templateName = preg_replace('/.latte/', '', $templateFile);
204
205
			// ...check if extension template is used
206
			if (is_file($dir . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateName . DIRECTORY_SEPARATOR . 'default.latte')) {
207
				$templateFile = $dir . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateName . DIRECTORY_SEPARATOR . 'default.latte';
208
209
			} else {
210
				// ...if not throw exception
211
				throw new Exceptions\FileNotFoundException(sprintf('Template file "%s" was not found.', $templateFile));
212
			}
213
		}
214
215
		$this->templateFile = $templateFile;
216
	}
217
218
	/**
219
	 * @param Localization\ITranslator $translator
220
	 *
221
	 * @return void
222
	 */
223
	public function setTranslator(Localization\ITranslator $translator) : void
224
	{
225
		$this->translator = $translator;
226
	}
227
228
	/**
229
	 * @return Localization\ITranslator|NULL
230
	 */
231
	public function getTranslator() : ?Localization\ITranslator
232
	{
233
		if ($this->translator instanceof Localization\ITranslator) {
234
			return $this->translator;
235
		}
236
237
		return NULL;
238
	}
239
}
240