BaseControl::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
 * BaseControl.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        https://www.ipublikuj.eu
7
 * @author         Adam Kadlec <[email protected]>
8
 * @package        iPublikuj:ConfirmationDialog!
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\ConfirmationDialog\Components;
18
19
use Nette\Application;
20
use Nette\Bridges;
21
use Nette\Localization;
22
23
use IPub\ConfirmationDialog\Exceptions;
24
25
/**
26
 * Abstract control definition
27
 *
28
 * @package        iPublikuj:ConfirmationDialog!
29
 * @subpackage     Components
30
 *
31
 * @author         Adam Kadlec <[email protected]>
32
 *
33
 * @property Application\UI\ITemplate $template
34
 */
35 1
abstract class BaseControl extends Application\UI\Control
36
{
37
	protected const TEMPLATE_LAYOUT = 'layout';
38
	protected const TEMPLATE_CONFIRMER = 'template';
39
40
	/**
41
	 * @var string|NULL
42
	 */
43
	protected $templateFile = NULL;
44
45
	/**
46
	 * @var string|NULL
47
	 */
48
	protected $layoutFile = NULL;
49
50
	/**
51
	 * @var Localization\ITranslator
52
	 */
53
	protected $translator;
54
55
	/**
56
	 * @param Localization\ITranslator $translator
57
	 *
58
	 * @return void
59
	 */
60
	public function injectTranslator(Localization\ITranslator $translator = NULL) : void
61
	{
62 1
		$this->translator = $translator;
63 1
	}
64
65
	/**
66
	 * @param Localization\ITranslator $translator
67
	 *
68
	 * @return void
69
	 */
70
	public function setTranslator(Localization\ITranslator $translator) : void
71
	{
72
		$this->translator = $translator;
73
	}
74
75
	/**
76
	 * @return Localization\ITranslator|NULL
77
	 */
78
	public function getTranslator() : ?Localization\ITranslator
79
	{
80 1
		if ($this->translator instanceof Localization\ITranslator) {
81
			return $this->translator;
82
		}
83
84 1
		return NULL;
85
	}
86
87
	/**
88
	 * Render control
89
	 *
90
	 * @return Application\UI\ITemplate
91
	 *
92
	 * @throws Exceptions\InvalidStateException
93
	 */
94
	public function render()
95
	{
96
		// Check if control has template
97 1
		if ($this->template instanceof Bridges\ApplicationLatte\Template) {
98
			// Check if translator is available
99 1
			if ($this->getTranslator() instanceof Localization\ITranslator) {
100
				$this->template->setTranslator($this->getTranslator());
101
			}
102
103
			// Render component template
104 1
			return $this->template;
105
		}
106
107
		throw new Exceptions\InvalidStateException('Control is without template.');
108
	}
109
110
	/**
111
	 * Change default control template path
112
	 *
113
	 * @param string $templateFile
114
	 * @param string $type
115
	 *
116
	 * @return void
117
	 *
118
	 * @throws Exceptions\FileNotFoundException
119
	 * @throws Exceptions\InvalidArgumentException
120
	 */
121
	protected function setTemplateFilePath(string $templateFile, string $type) : void
122
	{
123 1
		if (!in_array($type, [self::TEMPLATE_CONFIRMER, self::TEMPLATE_LAYOUT])) {
124
			throw new Exceptions\InvalidArgumentException('Wrong template type');
125
		}
126
127
		// Check if template file exists...
128 1
		if (!is_file($templateFile)) {
129 1
			$templateFile = $this->transformToTemplateFilePath($templateFile);
130
		}
131
132
		switch ($type)
133
		{
134 1
			case self::TEMPLATE_LAYOUT:
135
				$this->layoutFile = $templateFile;
136
				break;
137
138 1
			case self::TEMPLATE_CONFIRMER:
139 1
				$this->templateFile = $templateFile;
140 1
				break;
141
		}
142 1
	}
143
144
	/**
145
	 * @param string $templateFile
146
	 *
147
	 * @return string
148
	 */
149
	private function transformToTemplateFilePath(string $templateFile) : string
150
	{
151
		// Get component actual dir
152 1
		$dir = dirname($this->getReflection()->getFileName());
153
154 1
		$templateName = preg_replace('/.latte/', '', $templateFile);
155
156
		// ...check if extension template is used
157 1
		if (is_file($dir . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateName . '.latte')) {
158 1
			return $dir . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateName . '.latte';
159
		}
160
161
		// ...if not throw exception
162 1
		throw new Exceptions\FileNotFoundException(sprintf('Template file "%s" was not found.', $templateFile));
163
	}
164
}
165