Control::getTranslator()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.1481

Importance

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