Passed
Push — developer ( 517202...3a47f7 )
by Mariusz
192:00 queued 156:58
created

DetailView   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 113
Duplicated Lines 11.5 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 5 1
A __construct() 0 4 1
A setRecordModel() 0 4 1
B getLinksHeader() 0 45 6
A getTabsFromApi() 13 13 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Base file model for Detail View.
4
 *
5
 * @package Model
6
 *
7
 * @copyright YetiForce Sp. z o.o.
8
 * @license   YetiForce Public License 3.0 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author    Tomasz Kur <[email protected]>
10
 * @author    Radosław Skrzypczak <[email protected]>
11
 * @author Mariusz Krzaczkowski <[email protected]>
12
 */
13
14
namespace YF\Modules\Base\Model;
15
16
/**
17
 * Base class model for Detail View.
18
 */
19
class DetailView
20
{
21
	/** @var string Name of module. */
22
	protected $moduleName;
23
24
	/** @var \YF\Modules\Base\Model\Record Record model. */
25
	protected $record;
26
27
	/**
28
	 * Returns model for detail view.
29
	 *
30
	 * @param string $moduleName
31
	 *
32
	 * @return self
0 ignored issues
show
Documentation introduced by
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
33
	 */
34
	public static function getInstance(string $moduleName): self
35
	{
36
		$handlerModule = \App\Loader::getModuleClassName($moduleName, 'Model', 'DetailView');
37
		return new $handlerModule($moduleName);
38
	}
39
40
	/**
41
	 * Constructor.
42
	 *
43
	 * @param string $moduleName
44
	 */
45
	public function __construct(string $moduleName)
46
	{
47
		$this->moduleName = $moduleName;
48
	}
49
50
	/**
51
	 * Sets record model.
52
	 *
53
	 * @param \YF\Modules\Base\Model\Record $recordModel
54
	 *
55
	 * @return void
56
	 */
57
	public function setRecordModel(Record $recordModel): void
58
	{
59
		$this->record = $recordModel;
60
	}
61
62
	/**
63
	 * Get detail view header links.
64
	 *
65
	 * @return array
66
	 */
67
	public function getLinksHeader(): array
68
	{
69
		$links = [];
70
		if ($this->record->isPermitted('ExportPdf') && \App\Pdf::getTemplates($this->moduleName, $this->record->getId())) {
71
			$links[] = [
72
				'label' => 'BTN_EXPORT_PDF',
73
				'moduleName' => $this->moduleName,
74
				'data' => ['url' => 'index.php?module=' . $this->moduleName . '&view=Pdf&&record=' . $this->record->getId()],
75
				'icon' => 'fas fa-file-pdf',
76
				'class' => 'btn-sm btn-dark js-show-modal js-pdf',
77
				'showLabel' => 1,
78
			];
79
		}
80
		if ($this->record->isEditable()) {
81
			$links[] = [
82
				'label' => 'BTN_EDIT',
83
				'moduleName' => $this->moduleName,
84
				'href' => $this->record->getEditViewUrl(),
85
				'icon' => 'fas fa-edit',
86
				'class' => 'btn-sm btn-success',
87
				'showLabel' => 1,
88
			];
89
		}
90
		if ($this->record->isInventory()) {
91
			$links[] = [
92
				'label' => 'BTN_EDIT',
93
				'moduleName' => $this->moduleName,
94
				'href' => 'index.php?module=Products&view=ShoppingCart&reference_id=' . $this->record->getId() . '&reference_module=' . $this->moduleName,
95
				'icon' => 'fas fa-shopping-cart',
96
				'class' => 'btn-sm btn-success',
97
				'showLabel' => 1,
98
			];
99
		}
100
		if ($this->record->isDeletable()) {
101
			$links[] = [
102
				'label' => 'LBL_DELETE',
103
				'moduleName' => $this->moduleName,
104
				'data' => ['url' => $this->record->getDeleteUrl()],
105
				'icon' => 'fas fa-trash-alt',
106
				'class' => 'btn-sm btn-danger js-delete-record',
107
				'showLabel' => 1,
108
			];
109
		}
110
		return $links;
111
	}
112
113
	/**
114
	 * Get tabs.
115
	 *
116
	 * @return array
117
	 */
118 View Code Duplication
	public function getTabsFromApi(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
	{
120
		if (\App\Cache::has('moduleTabs', $this->moduleName)) {
121
			$data = \App\Cache::get('moduleTabs', $this->moduleName);
122
		} else {
123
			$data = \App\Api::getInstance()->call($this->moduleName . '/RelatedModules/' . $this->record->getId());
124
			\App\Cache::save('moduleTabs', $this->moduleName, $data, \App\Cache::LONG);
125
		}
126
		// echo '<pre>';
127
		// print_r($data);
128
		// exit;
129
		return $data;
130
	}
131
}
132