OSSMailView_Graf_Dashboard::process()   F
last analyzed

Complexity

Conditions 17
Paths 217

Size

Total Lines 104
Code Lines 89

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 306

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 89
c 1
b 0
f 0
dl 0
loc 104
ccs 0
cts 78
cp 0
rs 3.5766
cc 17
nc 217
nop 1
crap 306

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * OSSMailView graf dashboard class.
5
 *
6
 * @copyright YetiForce S.A.
7
 * @license   YetiForce Public License 6.5 (licenses/LicenseEN.txt or yetiforce.com)
8
 */
9
class OSSMailView_Graf_Dashboard extends Vtiger_IndexAjax_View
10
{
11
	/**
12
	 * Return params to search.
13
	 *
14
	 * @param string $stage
15
	 * @param int    $assignedTo
16
	 * @param string $dates
17
	 *
18
	 * @return void
19
	 */
20
	public function getSearchParams($stage, $assignedTo, $dates)
21
	{
22
		$conditions = [];
23
		$conditions[] = ['ossmailview_sendtype', 'e', $stage];
24
		if ('all' !== $assignedTo) {
0 ignored issues
show
introduced by
The condition 'all' !== $assignedTo is always true.
Loading history...
25
			$conditions[] = ['assigned_user_id', 'e', $assignedTo];
26
		}
27
		if (!empty($dates)) {
28
			$conditions[] = ['createdtime', 'bw', \App\Fields\DateTime::formatToDisplay($dates['start']) . ',' . \App\Fields\DateTime::formatToDisplay($dates['end'])];
29
		}
30
31
		return '&search_params=' . json_encode([$conditions]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return '&search_params='...ode(array($conditions)) returns the type string which is incompatible with the documented return type void.
Loading history...
32
	}
33
34
	/** {@inheritdoc} */
35
	public function process(App\Request $request)
36
	{
37
		$userId = \App\User::getCurrentUserId();
38
		$viewer = $this->getViewer($request);
39
		$moduleName = $request->getModule();
40
		$linkId = $request->getInteger('linkid');
41
		$owner = $request->getByType('owner', 'Alnum');
42
		$dates = $request->getByType('dateFilter', 'Text');
43
		$userList = \App\Fields\Owner::getInstance($moduleName, $userId)->getAccessibleUsersForModule();
44
		$groupList = \App\Fields\Owner::getInstance($moduleName, $userId)->getAccessibleGroupForModule();
45
		if (!$owner) {
46
			$owner = $userId;
47
		} elseif (is_numeric($owner) && !isset($userList[$owner]) && !isset($groupList[$owner])) {
48
			throw new \App\Exceptions\NoPermitted('LBL_PERMISSION_DENIED', 406);
49
		}
50
51
		$today = date('Y-m-d');
52
		if ('Yesterday' === $dates) {
53
			$data = strtotime('-1 day', strtotime($today));
54
			$dateFilter['start'] = date('Y-m-d', $data);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$dateFilter was never initialized. Although not strictly required by PHP, it is generally a good practice to add $dateFilter = array(); before regardless.
Loading history...
55
			$dateFilter['end'] = date('Y-m-d', $data);
56
		} elseif ('Current week' === $dates) {
57
			if ('Mon' == date('D')) {
58
				$dateFilter['start'] = date('Y-m-d');
59
			} else {
60
				$data = strtotime('last Monday', strtotime($today));
61
				$dateFilter['start'] = date('Y-m-d', $data);
62
			}
63
			$dateFilter['end'] = date('Y-m-d');
64
		} elseif ('Previous week' === $dates) {
65
			$data = strtotime('last Monday', strtotime($today));
66
			if ('Mon' != date('D')) {
67
				$data = strtotime('last Monday', strtotime(date('Y-m-d', $data)));
68
			}
69
			$dateFilter['start'] = date('Y-m-d', $data);
70
			$data = strtotime('last Sunday', strtotime($today));
71
			$dateFilter['end'] = date('Y-m-d', $data);
72
		} elseif ('Current month' === $dates) {
73
			$dateFilter['start'] = date('Y-m-d', mktime(0, 0, 0, date('m'), 1, date('Y')));
0 ignored issues
show
Bug introduced by
date('Y') of type string is incompatible with the type integer expected by parameter $year of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
			$dateFilter['start'] = date('Y-m-d', mktime(0, 0, 0, date('m'), 1, /** @scrutinizer ignore-type */ date('Y')));
Loading history...
Bug introduced by
date('m') of type string is incompatible with the type integer expected by parameter $month of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
			$dateFilter['start'] = date('Y-m-d', mktime(0, 0, 0, /** @scrutinizer ignore-type */ date('m'), 1, date('Y')));
Loading history...
74
			$dateFilter['end'] = $today;
75
		} elseif ('Previous month' === $dates) {
76
			$dateFilter['start'] = date('Y-m-d', mktime(0, 0, 0, date('m') - 1, 1, date('Y')));
77
			$dateFilter['end'] = date('Y-m-d', mktime(23, 59, 59, date('m'), 0, date('Y')));
78
		} elseif ('All' === $dates) {
79
			$dateFilter = '';
80
		} else {
81
			$dateFilter['start'] = $today;
82
			$dateFilter['end'] = $today;
83
		}
84
		$dateFilter['start'] .= ' 00:00:00';
85
		$dateFilter['end'] .= ' 23:59:59';
86
87
		$dataSets = [[
88
			'data' => [],
89
			'backgroundColor' => [],
90
			'borderColor' => [],
91
			'tooltips' => [],
92
			'names' => [],
93
			'links' => []
94
		]];
95
		$data = [
96
			'labels' => [],
97
			'datasets' => &$dataSets,
98
			'show_chart' => false,
99
		];
100
101
		$fieldName = 'ossmailview_sendtype';
102
		$colors = \App\Fields\Picklist::getValues($fieldName);
103
		$colors = array_column($colors, 'color', $fieldName);
104
		$listViewUrl = Vtiger_Module_Model::getInstance($moduleName)->getListViewUrl();
105
106
		$queryGenerator = (new \App\QueryGenerator($moduleName))
107
			->setFields([$fieldName])
108
			->setCustomColumn(['count' => new \yii\db\Expression('COUNT(*)')])
109
			->setGroup($fieldName)
110
			->addCondition($fieldName, '', 'ny')
111
			->addCondition('createdtime', $dateFilter['start'] . ',' . $dateFilter['end'], 'bw');
112
		if ('all' !== $owner) {
113
			$queryGenerator->addCondition('assigned_user_id', $owner, 'e');
114
		}
115
		$i = 0;
116
		$dataReader = $queryGenerator->createQuery()->createCommand()->query();
117
		while ($row = $dataReader->read()) {
118
			$color = !empty($colors[$row[$fieldName]]) ? '#' . $colors[$row[$fieldName]] : \App\Colors::getRandomColor($i);
119
			$data['labels'][$i] = \App\Language::translate($row[$fieldName], $moduleName);
120
			$dataSets[0]['data'][$i] = $row['count'];
121
			$dataSets[0]['names'][$i] = $row[$fieldName];
122
			$dataSets[0]['backgroundColor'][$i] = $color;
123
			$dataSets[0]['borderColor'][$i] = $color;
124
			$dataSets[0]['links'][$i] = $listViewUrl . $this->getSearchParams($row[$fieldName], $owner, $dateFilter);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getSearchParams($...], $owner, $dateFilter) targeting OSSMailView_Graf_Dashboard::getSearchParams() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
Bug introduced by
Are you sure $this->getSearchParams($...], $owner, $dateFilter) of type void can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

124
			$dataSets[0]['links'][$i] = $listViewUrl . /** @scrutinizer ignore-type */ $this->getSearchParams($row[$fieldName], $owner, $dateFilter);
Loading history...
125
			$data['show_chart'] = true;
126
			++$i;
127
		}
128
		$dataReader->close();
129
130
		$viewer->assign('MODULE_NAME', $moduleName);
131
		$viewer->assign('WIDGET', Vtiger_Widget_Model::getInstance($linkId, $userId));
132
		$viewer->assign('ACCESSIBLE_USERS', $userList);
133
		$viewer->assign('ACCESSIBLE_GROUPS', $groupList);
134
		$viewer->assign('DATA', $data);
135
		if ($request->has('content')) {
136
			$viewer->view('dashboards/DashBoardWidgetContents.tpl', $moduleName);
137
		} else {
138
			$viewer->view('dashboards/Graf.tpl', $moduleName);
139
		}
140
	}
141
}
142