Completed
Push — master ( e30e5f...00a259 )
by Adam
02:09
created

BaseControl   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 78.56%

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 6
dl 0
loc 128
ccs 22
cts 28
cp 0.7856
rs 10
c 0
b 0
f 0

6 Methods

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