Completed
Push — master ( 466d8a...4a1721 )
by Adam
04:54
created

BaseControl   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 77.78%

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 6
dl 0
loc 130
ccs 21
cts 27
cp 0.7778
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
	 * @return void
60
	 */
61
	public function injectTranslator(Localization\ITranslator $translator = NULL) : void
62
	{
63 1
		$this->translator = $translator;
64 1
	}
65
66
	/**
67
	 * @param Localization\ITranslator $translator
68
	 *
69
	 * @return void
70
	 */
71
	public function setTranslator(Localization\ITranslator $translator) : void
72
	{
73
		$this->translator = $translator;
74
	}
75
76
	/**
77
	 * @return Localization\ITranslator|NULL
78
	 */
79
	public function getTranslator() : ?Localization\ITranslator
80
	{
81 1
		if ($this->translator instanceof Localization\ITranslator) {
82
			return $this->translator;
83
		}
84
85 1
		return NULL;
86
	}
87
88
	/**
89
	 * Render control
90
	 *
91
	 * @return Application\UI\ITemplate
92
	 *
93
	 * @throws Exceptions\InvalidStateException
94
	 */
95
	public function render()
96
	{
97
		// Check if control has template
98 1
		if ($this->template instanceof Nette\Bridges\ApplicationLatte\Template) {
99
			// Check if translator is available
100 1
			if ($this->getTranslator() instanceof Localization\ITranslator) {
101
				$this->template->setTranslator($this->getTranslator());
102
			}
103
104
			// Render component template
105 1
			return $this->template;
106
		}
107
108
		throw new Exceptions\InvalidStateException('Control is without template.');
109
	}
110
111
	/**
112
	 * Change default control template path
113
	 *
114
	 * @param string $templateFile
115
	 * @param string $type
116
	 *
117
	 * @return void
118
	 *
119
	 * @throws Exceptions\FileNotFoundException
120
	 * @throws Exceptions\InvalidArgumentException
121
	 */
122
	protected function setTemplateFilePath(string $templateFile, string $type) : void
123
	{
124 1
		if (!in_array($type, [self::TEMPLATE_CONFIRMER, self::TEMPLATE_LAYOUT])) {
125
			throw new Exceptions\InvalidArgumentException('Wrong template type');
126
		}
127
128
		// Check if template file exists...
129 1
		if (!is_file($templateFile)) {
130 1
			$templateFile = $this->transformToTemplateFilePath($templateFile);
131
		}
132
133
		switch ($type)
134
		{
135 1
			case self::TEMPLATE_LAYOUT:
136
				$this->layoutFile = $templateFile;
137
				break;
138
139 1
			case self::TEMPLATE_CONFIRMER:
140 1
				$this->templateFile = $templateFile;
141 1
				break;
142
		}
143 1
	}
144
145
	/**
146
	 * @param string $templateFile
147
	 *
148
	 * @return string
149
	 */
150
	private function transformToTemplateFilePath(string $templateFile) : string
151
	{
152
		// Get component actual dir
153 1
		$dir = dirname($this->getReflection()->getFileName());
154
155 1
		$templateName = preg_replace('/.latte/', '', $templateFile);
156
157
		// ...check if extension template is used
158 1
		if (is_file($dir . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateName . '.latte')) {
159 1
			return $dir . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateName . '.latte';
160
		}
161
162
		// ...if not throw exception
163 1
		throw new Exceptions\FileNotFoundException(sprintf('Template file "%s" was not found.', $templateFile));
164
	}
165
}
166