Passed
Push — developer ( 57de19...8207c1 )
by Mariusz
35:57
created

Record::getModuleName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Basic record model class.
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    Radosław Skrzypczak <[email protected]>
10
 * @author    Mariusz Krzaczkowski <[email protected]>
11
 */
12
13
namespace YF\Modules\Base\Model;
14
15
class Record extends \App\BaseModel
16
{
17
	/** @var string Module name. */
18
	protected $moduleName;
19
20
	/** @var \YF\Modules\Base\Model\Module Module model instance. */
21
	protected $moduleModel;
22
23
	/** @var array Information about inventory. */
24
	protected $inventoryData = [];
25
26
	/** @var array Information about summary inventory. */
27
	protected $inventorySummaryData = [];
28
29
	/** @var string Record ID. */
30
	protected $id;
31
32
	/** @var string Record name. */
33
	protected $name = '';
34
35
	/** @var array Privileges. */
36
	protected $privileges = [];
37
38
	/** @var array Custom data. */
39
	protected $customData = [];
40
41
	/**
42
	 * Static Function to get the instance of a clean Record for the given module name.
43
	 *
44
	 * @param string $module
45
	 *
46
	 * @return \self
47
	 */
48
	public static function getInstance(string $module): self
49
	{
50
		$handlerModule = \App\Loader::getModuleClassName($module, 'Model', 'Record');
51
		$instance = new $handlerModule();
52
		return $instance->setModuleName($module);
53
	}
54
55
	/**
56
	 * Static function to get the instance of record.
57
	 *
58
	 * @param string $moduleName
59
	 * @param int    $recordId
60
	 * @param array  $headers
61
	 *
62
	 * @return \self
63
	 */
64
	public static function getInstanceById(string $moduleName, int $recordId, array $headers = []): self
65
	{
66
		$api = \App\Api::getInstance();
67
		if ($headers) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $headers of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
68
			$api->setCustomHeaders($headers);
69
		}
70
		$result = $api->call("{$moduleName}/Record/{$recordId}");
71
		$instance = self::getInstance($moduleName);
72
		$instance->setData($result['data'] ?? []);
73
		$instance->setInventoryData($result['inventory'] ?? [], $result['summaryInventory'] ?? []);
74
		$instance->privileges = $result['privileges'] ?? [];
75
		$instance->name = $result['name'] ?? '';
76
		unset($result['data'], $result['inventory'], $result['summaryInventory'], $result['privileges'], $result['name']);
77
		$instance->customData = $result;
78
		$instance->setId($recordId);
79
		return $instance;
80
	}
81
82
	/**
83
	 * Sets information about inventory.
84
	 *
85
	 * @param array $values
86
	 * @param array $summary
87
	 *
88
	 * @return void
89
	 */
90
	public function setInventoryData(array $values, array $summary = [])
91
	{
92
		$this->inventoryData = $values;
93
		$this->inventorySummaryData = $summary;
94
	}
95
96
	/**
97
	 * Returns information about inventory.
98
	 *
99
	 * @return void
0 ignored issues
show
Documentation introduced by
Should the return type not be array? Also, consider making the array more specific, something like array<String>, or String[].

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.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
100
	 */
101
	public function getInventoryData()
102
	{
103
		return $this->inventoryData;
104
	}
105
106
	/**
107
	 * Returns information about summary inventory.
108
	 *
109
	 * @return void
0 ignored issues
show
Documentation introduced by
Should the return type not be array? Also, consider making the array more specific, something like array<String>, or String[].

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.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
110
	 */
111
	public function getInventorySummary()
112
	{
113
		return $this->inventorySummaryData;
114
	}
115
116
	public function isInventory()
117
	{
118
		return !empty($this->inventoryData);
119
	}
120
121
	/**
122
	 * Function to get the id of the record.
123
	 *
124
	 * @return int
125
	 */
126
	public function getId(): int
127
	{
128
		return $this->id;
129
	}
130
131
	/**
132
	 * Function to set the id of the record.
133
	 *
134
	 * @param int $value
135
	 *
136
	 * @return self
137
	 */
138
	public function setId(int $value): self
139
	{
140
		$this->id = $value;
0 ignored issues
show
Documentation Bug introduced by
The property $id was declared of type string, but $value is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
141
		return $this;
142
	}
143
144
	/**
145
	 * Function to get the raw value.
146
	 *
147
	 * @return array
148
	 */
149
	public function getRawData(): array
150
	{
151
		return $this->customData['rawData'] ?? [];
152
	}
153
154
	/**
155
	 * Set raw data.
156
	 *
157
	 * @param array $rawData
158
	 *
159
	 * @return self
160
	 */
161
	public function setRawData(array $rawData): self
162
	{
163
		$this->customData['rawData'] = $rawData;
164
		return $this;
165
	}
166
167
	/**
168
	 * Function to get the raw value for a given key.
169
	 *
170
	 * @param string $key
171
	 *
172
	 * @return mixed
173
	 */
174
	public function getRawValue(string $key)
175
	{
176
		return $this->customData['rawData'][$key] ?? '';
177
	}
178
179
	/**
180
	 * Function to set the raw value for a given key.
181
	 *
182
	 * @param string $key
183
	 * @param mixed  $value
184
	 *
185
	 * @return self
186
	 */
187
	public function setRawValue(string $key, $value): self
188
	{
189
		$this->customData['rawData'][$key] = $value;
190
		return $this;
191
	}
192
193
	/**
194
	 * Function to get the name of the module to which the record belongs.
195
	 *
196
	 * @return string - Record Module Name
197
	 */
198
	public function getModuleName(): string
199
	{
200
		return $this->moduleName;
201
	}
202
203
	/**
204
	 * Get record module model instance.
205
	 *
206
	 * @return \YF\Modules\Base\Model\Module - Record module model instance
207
	 */
208
	public function getModuleModel(): Module
209
	{
210
		return $this->moduleModel;
211
	}
212
213
	/**
214
	 * Set record name.
215
	 *
216
	 * @param string $name
217
	 *
218
	 * @return string
219
	 */
220
	public function setName(string $name)
221
	{
222
		return $this->name = $name;
223
	}
224
225
	/**
226
	 * Record name.
227
	 *
228
	 * @return string
229
	 */
230
	public function getName()
231
	{
232
		return $this->name;
233
	}
234
235
	/**
236
	 * Get custom data.
237
	 *
238
	 * @return array
239
	 */
240
	public function getCustomData(): array
241
	{
242
		return $this->customData;
243
	}
244
245
	/**
246
	 * Function to get the raw value for a given key.
247
	 *
248
	 * @param string $key
249
	 *
250
	 * @return string
251
	 */
252
	public function getDisplayValue(string $key): string
253
	{
254
		return \App\Purifier::encodeHtml($this->get($key));
255
	}
256
257
	/**
258
	 * Get list display value.
259
	 *
260
	 * @param string $key
261
	 *
262
	 * @return string
263
	 */
264
	public function getListDisplayValue(string $key): string
265
	{
266
		$fieldModel = $this->getModuleModel()->getFieldModel($key);
267
		$value = '';
268
		if ($fieldModel->isViewable()) {
269
			if ($this->has($key)) {
270
				$fieldModel->setDisplayValue($this->get($key));
271
				if (isset($this->customData['rawData'][$key])) {
272
					$fieldModel->setRawValue($this->customData['rawData'][$key]);
273
				}
274
			}
275
			$value = $fieldModel->getListDisplayValue();
276
		}
277
		return $value;
278
	}
279
280
	/**
281
	 * Function to set the name of the module to which the record belongs.
282
	 *
283
	 * @param string $moduleName
284
	 *
285
	 * @return \self
0 ignored issues
show
Documentation introduced by
Should the return type not be Record?

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...
286
	 */
287
	public function setModuleName(string $moduleName): self
288
	{
289
		$this->moduleName = $moduleName;
290
		$this->moduleModel = \YF\Modules\Base\Model\Module::getInstance($moduleName);
0 ignored issues
show
Documentation Bug introduced by
It seems like \YF\Modules\Base\Model\M...etInstance($moduleName) of type object<self> is incompatible with the declared type object<YF\Modules\Base\Model\Module> of property $moduleModel.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
291
		return $this;
292
	}
293
294
	/**
295
	 * Function to get the list view actions for the record.
296
	 *
297
	 * @return string
298
	 */
299
	public function getRecordListViewActions(): string
300
	{
301
		$recordLinks = [];
302
		if ($this->isViewable()) {
303
			$recordLinks[] = [
304
				'label' => 'LBL_SHOW_COMPLETE_DETAILS',
305
				'moduleName' => $this->getModuleName(),
306
				'href' => $this->getDetailViewUrl(),
307
				'icon' => 'fas fa-th-list',
308
				'class' => 'btn-sm btn-info active js-popover-tooltip',
309
			];
310
		}
311
		if ($this->isEditable()) {
312
			$recordLinks[] = [
313
				'label' => 'BTN_EDIT',
314
				'moduleName' => $this->getModuleName(),
315
				'href' => $this->getEditViewUrl(),
316
				'icon' => 'fas fa-edit',
317
				'class' => 'btn-sm btn-success active js-popover-tooltip',
318
			];
319
		}
320
		if ($this->isDeletable()) {
321
			$recordLinks[] = [
322
				'label' => 'LBL_DELETE',
323
				'moduleName' => $this->getModuleName(),
324
				'data' => ['url' => $this->getDeleteUrl()],
325
				'icon' => 'fas fa-trash-alt',
326
				'class' => 'btn-sm btn-danger active js-delete-record js-popover-tooltip',
327
			];
328
		}
329
		return \App\Layout\Action::getListViewActions($recordLinks);
330
	}
331
332
	/**
333
	 * Function checks if there are permissions to preview record.
334
	 *
335
	 * @return bool
336
	 */
337
	public function isViewable()
338
	{
339
		return true;
340
	}
341
342
	/**
343
	 * Function to get the Detail View url for the record.
344
	 *
345
	 * @return string - Record Detail View Url
346
	 */
347
	public function getDetailViewUrl()
348
	{
349
		return 'index.php?module=' . $this->getModuleName() . '&view=DetailView&record=' . $this->getId();
350
	}
351
352
	/**
353
	 * Function checks if there are permissions to edit record.
354
	 *
355
	 * @return bool
356
	 */
357 View Code Duplication
	public function isEditable(): bool
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...
358
	{
359
		if (!isset($this->privileges['isEditable'])) {
360
			$this->privileges['isEditable'] = \YF\Modules\Base\Model\Module::isPermitted($this->getModuleName(), 'EditView', $this->getId());
0 ignored issues
show
Unused Code introduced by
The call to Module::isPermitted() has too many arguments starting with $this->getId().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
361
		}
362
		return $this->privileges['isEditable'];
363
	}
364
365
	/**
366
	 * Function to get the Edit View url for the record.
367
	 *
368
	 * @return string - Record Edit View Url
369
	 */
370
	public function getEditViewUrl()
371
	{
372
		return 'index.php?module=' . $this->getModuleName() . '&view=EditView&record=' . $this->getId();
373
	}
374
375
	/**
376
	 * Function checks if there are permissions to delete record.
377
	 *
378
	 * @return bool
379
	 */
380 View Code Duplication
	public function isDeletable(): bool
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...
381
	{
382
		if (!isset($this->privileges['moveToTrash'])) {
383
			$this->privileges['moveToTrash'] = \YF\Modules\Base\Model\Module::isPermitted($this->getModuleName(), 'MoveToTrash', $this->getId());
0 ignored issues
show
Unused Code introduced by
The call to Module::isPermitted() has too many arguments starting with $this->getId().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
384
		}
385
		return $this->privileges['moveToTrash'];
386
	}
387
388
	/**
389
	 * Function checks permissions to action.
390
	 *
391
	 * @param string $actionName
392
	 *
393
	 * @return bool
394
	 */
395
	public function isPermitted(string $actionName): bool
396
	{
397
		if (!isset($this->privileges[$actionName])) {
398
			$this->privileges[$actionName] = \YF\Modules\Base\Model\Module::isPermitted($this->getModuleName(), $actionName, $this->getId());
0 ignored issues
show
Unused Code introduced by
The call to Module::isPermitted() has too many arguments starting with $this->getId().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
399
		}
400
		return $this->privileges[$actionName];
401
	}
402
403
	/**
404
	 * Function to get the delete action url for the record.
405
	 *
406
	 * @return string
407
	 */
408
	public function getDeleteUrl()
409
	{
410
		return 'index.php?module=' . $this->getModuleName() . '&action=Delete&record=' . $this->getId();
411
	}
412
}
413