Widget   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 3
dl 0
loc 172
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getTitle() 0 6 2
A getDescription() 0 4 1
A getPriority() 0 4 1
A getStatus() 0 4 1
A getPosition() 0 4 1
F beforeRender() 0 81 12
A render() 0 7 1
A __toString() 0 6 1
1
<?php
2
/**
3
 * Widget.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        https://www.ipublikuj.eu
7
 * @author         Adam Kadlec <[email protected]>
8
 * @package        iPublikuj:Widgets!
9
 * @subpackage     Widgets
10
 * @since          1.0.0
11
 *
12
 * @date           15.09.14
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\Widgets\Widgets;
18
19
use Nette;
20
use Nette\Application;
21
use Nette\ComponentModel;
22
use Nette\Localization;
23
use Nette\Utils;
24
25
use IPub\Widgets;
26
use IPub\Widgets\Entities;
27
use IPub\Widgets\Exceptions;
28
29
/**
30
 * Widgets control definition
31
 *
32
 * @package        iPublikuj:Widgets!
33
 * @subpackage     Widgets
34
 *
35
 * @author         Adam Kadlec <[email protected]>
36
 *
37
 * @property Application\UI\ITemplate $template
38
 */
39
abstract class Widget extends Widgets\Application\UI\BaseControl implements IWidget
40
{
41
	/**
42
	 * @var Entities\IData
43
	 */
44
	protected $data;
45
46
	/**
47
	 * @param Entities\IData $data
48
	 * @param ComponentModel\IContainer|NULL $parent
49
	 * @param string|NULL $name
50
	 */
51
	public function __construct(
52
		Entities\IData $data,
53
		?ComponentModel\IContainer $parent = NULL,
54
		?string $name = NULL
55
	) {
56
		parent::__construct($parent, $name);
57
58
		$this->data = $data;
59
	}
60
61
	/**
62
	 * {@inheritdoc}
63
	 */
64
	public function getTitle() : string
65
	{
66
		$title = $this->data->getTitle();
67
68
		return $title ? $title : '';
69
	}
70
71
	/**
72
	 * {@inheritdoc}
73
	 */
74
	public function getDescription() : string
75
	{
76
		return $this->data->getDescription();
77
	}
78
79
	/**
80
	 * {@inheritdoc}
81
	 */
82
	public function getPriority() : int
83
	{
84
		return $this->data->getPriority();
85
	}
86
87
	/**
88
	 * {@inheritdoc}
89
	 */
90
	public function getStatus() : bool
91
	{
92
		return $this->data->getStatus();
93
	}
94
95
	/**
96
	 * {@inheritdoc}
97
	 */
98
	public function getPosition() : string
99
	{
100
		return $this->data->getPosition();
101
	}
102
103
	/**
104
	 * Before render actions
105
	 */
106
	protected function beforeRender() : void
107
	{
108
		// Check if control has template
109
		if (!$this->template instanceof Nette\Bridges\ApplicationLatte\Template) {
0 ignored issues
show
Bug introduced by
The class Nette\Bridges\ApplicationLatte\Template does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
110
			throw new Exceptions\InvalidStateException('Widgets container control is without template.');
111
		}
112
113
		// Get widget title
114
		$name = $this->getTitle();
115
116
		// If widget name has space...
117
		$pos = mb_strpos($name, ' ');
118
		if ($pos !== FALSE && mb_strpos($name, '||') === FALSE) {
119
			$title = Utils\Html::el('span')
120
				->addAttributes(['class' => 'color'])
121
				->setText(mb_substr($name, 0, $pos))
122
				->render();
123
124
			// Modify widget name
125
			$name = $title . mb_substr($name, $pos);
126
		}
127
128
		// If widget name has subtitle...
129
		$pos = mb_strpos($name, '||');
130
		if ($pos !== FALSE) {
131
			$title = Utils\Html::el('span')
132
				->addAttributes(['class' => 'title'])
133
				->setText(mb_substr($name, 0, $pos))
134
				->render();
135
136
			$subtitle = Utils\Html::el('span')
137
				->addAttributes(['class' => 'subtitle'])
138
				->setText(mb_substr($name, $pos + 2))
139
				->render();
140
141
			// Split name to title & subtitle
142
			$name = $title . $subtitle;
143
		}
144
145
		// Set badge if exists
146
		if ($badge = $this->data->getBadge()) {
147
			$badge = Utils\Html::el('i')
148
				->addAttributes(['class' => 'badge badge-' . $badge]);
149
		}
150
151
		// Set icon if exists
152
		if ($icon = $this->data->getIcon()) {
153
			$icon = Utils\Html::el('i')
154
				->addAttributes(['class' => 'ipub-icon ipub-icon-' . $icon]);
155
		}
156
157
		// Assign basic widget data to template
158
		$this->template->badge = $badge;
159
		$this->template->icon = $icon;
160
		$this->template->title = [
161
			'text'   => $name,
162
			'insert' => $this->data->getParam('widget.title.insert', TRUE),
163
			'hidden' => $this->data->getParam('widget.title.hidden', FALSE)
164
		];
165
166
		// Check if translator is available
167
		if ($this->getTranslator() instanceof Localization\ITranslator) {
0 ignored issues
show
Bug introduced by
The class Nette\Localization\ITranslator does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
168
			$this->template->setTranslator($this->getTranslator());
169
		}
170
171
		$templateFile = $this->getTemplate()->getFile();
172
173
		if (is_callable($templateFile)) {
174
			$templateFile = call_user_func($templateFile);
175
		}
176
177
		// If template was not defined before...
178
		if ($templateFile === NULL) {
179
			// Get component actual dir
180
			$dir = dirname($this->getReflection()->getFileName());
181
182
			// ...try to get base component template file
183
			$templateFile = $this->templateFile !== NULL && is_file($this->templateFile) ? $this->templateFile : $dir . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . 'default.latte';
184
			$this->template->setFile($templateFile);
185
		}
186
	}
187
188
	/**
189
	 * Render widget
190
	 */
191
	public function render() : void
192
	{
193
		$this->beforeRender();
194
195
		// Render component template
196
		$this->template->render();
197
	}
198
199
	/**
200
	 * Convert widget name to string representation
201
	 *
202
	 * @return string
203
	 */
204
	public function __toString()
205
	{
206
		$class = explode('\\', get_class($this));
207
208
		return end($class);
209
	}
210
}
211