Passed
Push — developer ( f00fa8...02e24e )
by Radosław
53:12 queued 18:13
created

Widget   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 0
loc 62
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A checkPermission() 0 10 4
A process() 0 15 4
A getContent() 0 7 1
1
<?php
2
/**
3
 * Widget view file.
4
 *
5
 * @package View
6
 *
7
 * @copyright YetiForce Sp. z o.o.
8
 * @license   YetiForce Public License 3.0 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author    Radosław Skrzypczak <[email protected]>
10
 */
11
12
namespace YF\Modules\Base\View;
13
14
/**
15
 * Widget view class.
16
 */
17
class Widget extends \App\Controller\View
18
{
19
	use \App\Controller\ExposeMethodTrait;
20
21
	/** @var int Record ID */
22
	protected $recordId;
23
24
	/** @var int Widget ID */
25
	protected $widgetId;
26
27
	/** @var string Module name */
28
	protected $moduleName;
29
30
	/** {@inheritdoc} */
31
	public function __construct(\App\Request $request)
32
	{
33
		parent::__construct($request);
34
		$this->exposeMethod('getContent');
35
	}
36
37
	/** {@inheritdoc} */
38
	public function checkPermission(): void
39
	{
40
		parent::checkPermission();
41
		$this->recordId = $this->request->getInteger('record');
42
		$this->widgetId = $this->request->getInteger('widgetId');
43
		$this->moduleName = $this->request->getModule();
44
		if (!$this->recordId || !$this->widgetId || !isset(\App\Widgets::getInstance($this->moduleName)->getAll()[$this->widgetId])) {
45
			throw new \App\Exceptions\AppException('ERR_PERMISSION_DENIED');
46
		}
47
	}
48
49
	/** {@inheritdoc} */
50
	public function process()
51
	{
52
		$mode = $this->request->getMode();
53
		if ($mode && $this->isMethodExposed($mode)) {
54
			$this->invokeExposedMethod($mode);
55
		} else {
56
			$widget = \App\Widgets::getInstance($this->moduleName)->getAll()[$this->widgetId];
57
			$widget->setRecordId($this->recordId);
58
			if ($scripts = $widget->getScripts()) {
59
				$widget->setScriptsObject($this->convertScripts($scripts, 'js'));
60
			}
61
			$this->viewer->assign('WIDGET', $widget);
62
			$this->viewer->view($widget->getTemplatePath(), $this->moduleName);
63
		}
64
	}
65
66
	/**
67
	 * Gets widget content.
68
	 *
69
	 * @return void
70
	 */
71
	public function getContent()
72
	{
73
		$widget = \App\Widgets::getInstance($this->moduleName)->getAll()[$this->widgetId];
74
		$widget->setRecordId($this->recordId);
75
		$this->viewer->assign('WIDGET', $widget);
76
		$this->viewer->view($widget->getTemplateContentPath(), $this->moduleName);
77
	}
78
}
79