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