Completed
Push — master ( d2ee45...5d494d )
by Adam
04:50 queued 02:51
created

BaseControl   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 75.86%

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 6
dl 0
loc 129
ccs 22
cts 29
cp 0.7586
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A injectTranslator() 0 4 1
B setTemplateFilePath() 0 22 5
A setTranslator() 0 4 1
A getTranslator() 0 8 2
A render() 0 15 3
A transformToTemplateFilePath() 0 16 3
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
	 * Change default control template path
66
	 *
67
	 * @param string $templateFile
68
	 * @param string $type
69
	 *
70
	 * @return void
71
	 * 
72
	 * @throws Exceptions\FileNotFoundException
73
	 * @throws Exceptions\InvalidArgumentException
74
	 */
75
	public function setTemplateFilePath(string $templateFile, string $type)
76
	{
77 1
		if (!in_array($type, [self::TEMPLATE_CONFIRMER, self::TEMPLATE_LAYOUT])) {
78
			throw new Exceptions\InvalidArgumentException('Wrong template type');
79
		}
80
81
		// Check if template file exists...
82 1
		if (!is_file($templateFile)) {
83 1
			$templateFile = $this->transformToTemplateFilePath($templateFile);
84
		}
85
86
		switch ($type)
87
		{
88 1
			case self::TEMPLATE_LAYOUT:
89
				$this->layoutFile = $templateFile;
90
				break;
91
92 1
			case self::TEMPLATE_CONFIRMER:
93 1
				$this->templateFile = $templateFile;
94 1
				break;
95
		}
96 1
	}
97
98
	/**
99
	 * @param Localization\ITranslator $translator
100
	 * 
101
	 * @return void
102
	 */
103
	public function setTranslator(Localization\ITranslator $translator)
104
	{
105
		$this->translator = $translator;
106
	}
107
108
	/**
109
	 * @return Localization\ITranslator|NULL
110
	 */
111
	public function getTranslator()
112
	{
113 1
		if ($this->translator instanceof Localization\ITranslator) {
114
			return $this->translator;
115
		}
116
117 1
		return NULL;
118
	}
119
120
	/**
121
	 * Render control
122
	 *
123
	 * @return Application\UI\ITemplate
124
	 *
125
	 * @throws Exceptions\InvalidStateException
126
	 */
127
	public function render()
128
	{
129
		// Check if control has template
130 1
		if ($this->template instanceof Nette\Bridges\ApplicationLatte\Template) {
131
			// Check if translator is available
132 1
			if ($this->getTranslator() instanceof Localization\ITranslator) {
133
				$this->template->setTranslator($this->getTranslator());
134
			}
135
136
			// Render component template
137 1
			return $this->template;
138
		}
139
140
		throw new Exceptions\InvalidStateException('Control is without template.');
141
	}
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
		// ...check if extension template is used
154 1
		if (is_file($dir . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateFile)) {
155 1
			return $dir . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateFile;
156
157 1
		} elseif (is_file($dir . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateFile . '.latte')) {
158
			return $dir . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateFile . '.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