Completed
Push — master ( b57c0a...d441ef )
by Adam
02:49 queued 14s
created

BaseControl::render()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.576

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 15
ccs 3
cts 5
cp 0.6
rs 9.4285
cc 3
eloc 6
nc 3
nop 0
crap 3.576
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
			// Get component actual dir
82 1
			$dir = dirname($this->getReflection()->getFileName());
83
84
			// ...check if extension template is used
85 1
			if (is_file($dir . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateFile)) {
86 1
				$templateFile = $dir . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateFile;
87
88 1
			} elseif (is_file($dir . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateFile . '.latte')) {
89
				$templateFile = $dir . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateFile . '.latte';
90
91
			} else {
92
				// ...if not throw exception
93 1
				throw new Exceptions\FileNotFoundException(sprintf('Template file "%s" was not found.', $templateFile));
94
			}
95
		}
96
97 1
		if ($type == self::TEMPLATE_LAYOUT) {
98
			$this->layoutFile = $templateFile;
99
100
		} else {
101 1
			$this->templateFile = $templateFile;
102
		}
103 1
	}
104
105
	/**
106
	 * @param Localization\ITranslator $translator
107
	 * 
108
	 * @return void
109
	 */
110
	public function setTranslator(Localization\ITranslator $translator)
111
	{
112
		$this->translator = $translator;
113
	}
114
115
	/**
116
	 * @return Localization\ITranslator|NULL
117
	 */
118
	public function getTranslator()
119
	{
120 1
		if ($this->translator instanceof Localization\ITranslator) {
121
			return $this->translator;
122
		}
123
124 1
		return NULL;
125
	}
126
127
	/**
128
	 * Render control
129
	 *
130
	 * @return Application\UI\ITemplate
131
	 *
132
	 * @throws Exceptions\InvalidStateException
133
	 */
134
	public function render()
135
	{
136
		// Check if control has template
137 1
		if ($this->template instanceof Nette\Bridges\ApplicationLatte\Template) {
138
			// Check if translator is available
139 1
			if ($this->getTranslator() instanceof Localization\ITranslator) {
140
				$this->template->setTranslator($this->getTranslator());
141
			}
142
143
			// Render component template
144 1
			return $this->template;
145
		}
146
147
		throw new Exceptions\InvalidStateException('Control is without template.');
148
	}
149
}
150