Completed
Push — master ( ccb281...425e3a )
by Adam
03:08
created

BaseControl::transformToTemplateFilePath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 4.125

Importance

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