Completed
Push — master ( 0636af...e088fc )
by Adam
07:34
created

BaseControl   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 3
dl 0
loc 70
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A injectTranslator() 0 4 1
B setTemplateFile() 0 22 4
A setTranslator() 0 4 1
A getTranslator() 0 8 2
1
<?php
2
/**
3
 * BaseControl.php
4
 *
5
 * @copyright      Vice v copyright.php
6
 * @license        http://www.ipublikuj.eu
7
 * @author         Adam Kadlec http://www.ipublikuj.eu
8
 * @package        iPublikuj:Widgets!
9
 * @subpackage     Application
10
 * @since          1.0.0
11
 *
12
 * @date           24.07.13
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\Widgets\Application\UI;
18
19
use Nette;
20
use Nette\Application;
21
use Nette\Localization;
22
23
use IPub;
24
use IPub\Widgets\Exceptions;
25
26
/**
27
 * Extensions base control definition
28
 *
29
 * @package        iPublikuj:Widgets!
30
 * @subpackage     Application
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
	/**
39
	 * @var string
40
	 */
41
	protected $templateFile;
42
43
	/**
44
	 * @var Localization\ITranslator
45
	 */
46
	protected $translator;
47
48
	/**
49
	 * @param Localization\ITranslator $translator
50
	 */
51
	public function injectTranslator(Localization\ITranslator $translator = NULL)
52
	{
53
		$this->translator = $translator;
54
	}
55
56
	/**
57
	 * Change default control template path
58
	 *
59
	 * @param string $templateFile
60
	 *
61
	 * @throws Exceptions\FileNotFoundException
62
	 */
63
	public function setTemplateFile($templateFile)
64
	{
65
		// Check if template file exists...
66
		if (!is_file($templateFile)) {
67
			// Get component actual dir
68
			$dir = dirname($this->getReflection()->getFileName());
69
70
			// ...check if extension template is used
71
			if (is_file($dir . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateFile)) {
72
				$templateFile = $dir . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateFile;
73
74
			} else if (is_file($dir . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateFile . '.latte')) {
75
				$templateFile = $dir . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $templateFile . '.latte';
76
77
			} else {
78
				// ...if not throw exception
79
				throw new Exceptions\FileNotFoundException(sprintf('Template file "%s" was not found.', $templateFile));
80
			}
81
		}
82
83
		$this->templateFile = $templateFile;
84
	}
85
86
	/**
87
	 * @param Localization\ITranslator $translator
88
	 */
89
	public function setTranslator(Localization\ITranslator $translator)
90
	{
91
		$this->translator = $translator;
92
	}
93
94
	/**
95
	 * @return Localization\ITranslator|NULL
96
	 */
97
	public function getTranslator()
98
	{
99
		if ($this->translator instanceof Localization\ITranslator) {
100
			return $this->translator;
101
		}
102
103
		return NULL;
104
	}
105
}
106