Test Failed
Pull Request — master (#5)
by
unknown
02:44
created

Control::setTemplateFile()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
ccs 0
cts 8
cp 0
cc 3
nc 3
nop 1
crap 12
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);
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) {
0 ignored issues
show
Bug introduced by
The class Nette\Bridges\ApplicationLatte\Template does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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) {
0 ignored issues
show
Bug introduced by
The class Nette\Localization\ITranslator does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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) {
0 ignored issues
show
Bug introduced by
The class Nette\Bridges\ApplicationLatte\Template does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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) {
0 ignored issues
show
Bug introduced by
The class Nette\Localization\ITranslator does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
234
			return $this->translator;
235
		}
236
237
		return NULL;
238
	}
239
}
240