Passed
Push — developer ( 6cca19...a1733f )
by Radosław
47:59 queued 12:43
created

RelatedList   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 3
dl 0
loc 103
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 7 1
A setRecordId() 0 5 1
A getFromApi() 0 6 1
A getRecordsTree() 0 16 5
A getRecordById() 0 16 5
1
<?php
2
/**
3
 * Related list view model file.
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
 */
11
12
namespace YF\Modules\ModComments\Model;
13
14
/**
15
 * Related list view model class.
16
 */
17
class RelatedList extends \YF\Modules\Base\Model\RelatedList
18
{
19
	/** {@inheritdoc} */
20
	protected $order = 'DESC';
21
22
	/** {@inheritdoc} */
23
	protected $orderField = 'createdtime';
24
25
	/** {@inheritdoc} */
26
	protected $rawData = true;
27
28
	/** {@inheritdoc} */
29
	protected $relatedModuleName = 'ModComments';
30
31
	/** {@inheritdoc} */
32
	protected $actionName = 'RecordRelatedList';
33
34
	/** {@inheritdoc} */
35
	protected $fields = ['parent_comments', 'createdtime', 'modifiedtime', 'related_to', 'id',
36
		'assigned_user_id', 'commentcontent', 'creator', 'customer', 'reasontoedit', 'userid', 'parents'];
37
38
	/** @var int Record ID */
39
	protected $recordId;
40
41
	/** @var array Records Models */
42
	protected $recordsModel = [];
43
44
	/** {@inheritdoc} */
45
	public static function getInstance(string $moduleName, string $viewName = 'ListView')
46
	{
47
		$self = new self();
48
		$self->moduleName = $moduleName;
49
		$self->limit = 100;
50
		return $self;
51
	}
52
53
	/**
54
	 * Set record ID.
55
	 *
56
	 * @param int $recordId
57
	 *
58
	 * @return $this
59
	 */
60
	public function setRecordId(int $recordId)
61
	{
62
		$this->recordId = $recordId;
63
		return $this;
64
	}
65
66
	/** {@inheritdoc} */
67
	protected function getFromApi(array $headers): array
68
	{
69
		$api = \App\Api::getInstance();
70
		$api->setCustomHeaders($headers);
71
		return $api->call("{$this->getModuleName()}/RecordRelatedList/{$this->recordId}/{$this->relatedModuleName}");
72
	}
73
74
	/**
75
	 * Gets records tree.
76
	 *
77
	 * @return array
78
	 */
79
	public function getRecordsTree(): array
80
	{
81
		$recordsModel = [];
82
		if (!empty($this->recordsList['records'])) {
83
			foreach ($this->recordsList['records'] as $id => $value) {
84
				$recordModel = $this->getRecordById($id);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $recordModel is correct as $this->getRecordById($id) (which targets YF\Modules\ModComments\M...edList::getRecordById()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
85
86
				if (!($parentId = $recordModel->getRawValue('parent_comments'))) {
0 ignored issues
show
Bug introduced by
The method getRawValue cannot be called on $recordModel (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
87
					$recordsModel[$id] = $recordModel;
88
				} elseif ($parentRecord = $this->getRecordById($parentId)) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $parentRecord is correct as $this->getRecordById($parentId) (which targets YF\Modules\ModComments\M...edList::getRecordById()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
89
					$parentRecord->setChild($recordModel);
90
				}
91
			}
92
		}
93
		return $recordsModel;
94
	}
95
96
	/**
97
	 * Gets record model.
98
	 *
99
	 * @param int $recordId
100
	 *
101
	 * @return \YF\Modules\Base\Model\Record|null
102
	 */
103
	public function getRecordById(int $recordId)
104
	{
105
		if (!isset($this->recordModels[$recordId]) && ($value = $this->recordsList['records'][$recordId] ?? null)) {
0 ignored issues
show
Bug introduced by
The property recordModels does not seem to exist. Did you mean recordsModel?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
106
			$recordModel = Record::getInstance($this->relatedModuleName);
107
			if (isset($value['recordLabel'])) {
108
				$recordModel->setName($value['recordLabel']);
109
				unset($value['recordLabel']);
110
			}
111
			if (isset($this->recordsList['rawData'][$recordId])) {
112
				$recordModel->setRawData($this->recordsList['rawData'][$recordId]);
113
			}
114
			$recordModel->setData($value)->setId($recordId)->setPrivileges($this->recordsList['permissions'][$recordId]);
115
			$this->recordModels[$recordId] = $recordModel;
0 ignored issues
show
Bug introduced by
The property recordModels does not seem to exist. Did you mean recordsModel?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
116
		}
117
		return $this->recordModels[$recordId] ?? null;
0 ignored issues
show
Bug introduced by
The property recordModels does not seem to exist. Did you mean recordsModel?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
118
	}
119
}
120