Test Failed
Push — master ( cd42b5...841446 )
by
unknown
16:44 queued 06:09
created

ReminderItemModule::snoozeItem()   B

Complexity

Conditions 9
Paths 18

Size

Total Lines 52
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 26
c 1
b 0
f 0
nc 18
nop 3
dl 0
loc 52
rs 8.0555

How to fix   Long Method   

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
 * Reminder ItemModule
5
 * Module which saves an item and it
6
 * extends the CreateMailItemModule class.
7
 */
8
class ReminderItemModule extends ItemModule {
9
	/**
10
	 * Constructor.
11
	 *
12
	 * @param int   $id   unique id
13
	 * @param array $data list of all actions
14
	 */
15
	public function __construct($id, $data) {
16
		parent::__construct($id, $data);
17
	}
18
19
	/**
20
	 * Function is use to set message flags for flagged mail item.
21
	 *
22
	 * @param object $store         MAPI Message Store Object
23
	 * @param string $parententryid parent entryid of the message
24
	 * @param string $entryid       entryid of the message
25
	 * @param array  $action        the action data, sent by the client
26
	 *
27
	 * @return bool true on success or false on failure
28
	 */
29
	public function save($store, $parententryid, $entryid, $action) {
30
		$this->properties = $GLOBALS['properties']->getMailProperties();
0 ignored issues
show
Bug Best Practice introduced by
The property properties does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
31
		$result = false;
32
33
		if (!$store) {
0 ignored issues
show
introduced by
$store is of type object, thus it always evaluated to true.
Loading history...
34
			$store = $GLOBALS['mapisession']->getDefaultMessageStore();
35
		}
36
37
		if ($store) {
0 ignored issues
show
introduced by
$store is of type object, thus it always evaluated to true.
Loading history...
38
			// Reference to an array which will be filled with PR_ENTRYID, PR_STORE_ENTRYID and PR_PARENT_ENTRYID of the message
39
			$messageProps = [];
40
41
			// Set message flags
42
			if (isset($action['props'], $action['props']['message_flags']) && $entryid) {
43
				$msg_action = isset($action['message_action']) ? $action['message_action'] : false;
44
				$result = $GLOBALS['operations']->setMessageFlag($store, $entryid, $action['props']['message_flags'], $msg_action, $messageProps);
45
			}
46
47
			// Feedback for successful save
48
			if ($result) {
49
				$GLOBALS['bus']->notify(bin2hex($messageProps[PR_PARENT_ENTRYID]), TABLE_SAVE, $messageProps);
50
			}
51
52
			$this->sendFeedback($result ? true : false, [], true);
53
		}
54
	}
55
56
	/**
57
	 * Function which is use to dismiss or snooze the reminder item.
58
	 *
59
	 * @param object $store         MAPI Message Store Object
60
	 * @param string $parententryid parent entryid of the message
61
	 * @param string $entryid       entryid of the message
62
	 * @param array  $action        the action data, sent by the client
63
	 *
64
	 * @return bool true on success or false on failure
65
	 */
66
	public function delete($store, $parententryid, $entryid, $action) {
67
		$this->properties = $GLOBALS["properties"]->getReminderProperties();
0 ignored issues
show
Bug Best Practice introduced by
The property properties does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
68
69
		if (!$store) {
0 ignored issues
show
introduced by
$store is of type object, thus it always evaluated to true.
Loading history...
70
			$store = $GLOBALS["mapisession"]->getDefaultMessageStore();
71
		}
72
73
		$subActionType = false;
74
		if (isset($action["message_action"], $action["message_action"]["action_type"])) {
75
			$subActionType = $action["message_action"]["action_type"];
76
		}
77
78
		switch ($subActionType) {
79
			case "snooze":
80
				$entryid = $this->getActionEntryID($action);
81
				$this->snoozeItem($store, $entryid, $action);
0 ignored issues
show
Bug introduced by
$entryid of type object is incompatible with the type string expected by parameter $entryid of ReminderItemModule::snoozeItem(). ( Ignorable by Annotation )

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

81
				$this->snoozeItem($store, /** @scrutinizer ignore-type */ $entryid, $action);
Loading history...
82
				break;
83
84
			case "dismiss":
85
				$entryid = $this->getActionEntryID($action);
86
				$this->dismissItem($store, $entryid);
0 ignored issues
show
Bug introduced by
$entryid of type object is incompatible with the type string expected by parameter $entryid of ReminderItemModule::dismissItem(). ( Ignorable by Annotation )

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

86
				$this->dismissItem($store, /** @scrutinizer ignore-type */ $entryid);
Loading history...
87
				break;
88
89
			default:
90
				$this->handleUnknownActionType($subActionType);
91
		}
92
	}
93
94
	/**
95
	 * Function which is use to snooze the reminder for given time.
96
	 *
97
	 * @param object $store   MAPI Message Store Object
98
	 * @param string $entryid entryid of the message
99
	 * @param array  $action  the action data, sent by the client
100
	 */
101
	public function snoozeItem($store, $entryid, $action) {
102
		$result = false;
103
		$message = mapi_msgstore_openentry($store, $entryid);
104
		if ($message) {
105
			$newProps = [PR_ENTRYID => $entryid];
106
			$props = mapi_getprops($message, $this->properties);
107
108
			$snoozeTime = $GLOBALS["settings"]->get('zarafa/v1/main/reminder/default_snooze_time', 5);
109
			if (isset($action["message_action"]["snoozeTime"]) && is_numeric($action["message_action"]["snoozeTime"])) {
110
				$snoozeTime = $action["message_action"]["snoozeTime"];
111
			}
112
113
			$reminderTime = time() + ($snoozeTime * 60);
114
			if (stripos($props[$this->properties["message_class"]], "IPM.Appointment") === 0) {
115
				if (isset($props[$this->properties["appointment_recurring"]]) && $props[$this->properties["appointment_recurring"]]) {
116
					$recurrence = new Recurrence($store, $message);
0 ignored issues
show
Bug introduced by
$store of type object is incompatible with the type resource expected by parameter $store of Recurrence::__construct(). ( Ignorable by Annotation )

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

116
					$recurrence = new Recurrence(/** @scrutinizer ignore-type */ $store, $message);
Loading history...
117
					$nextReminder = $recurrence->getNextReminderTime(time());
118
119
					// flagdueby must be the snooze time or the time of the next instance, whichever is earlier
120
					if ($reminderTime < $nextReminder) {
121
						$newProps[$this->properties["flagdueby"]] = $reminderTime;
122
					}
123
					else {
124
						$newProps[$this->properties["flagdueby"]] = $nextReminder;
125
					}
126
				}
127
				else {
128
					$newProps[$this->properties["flagdueby"]] = $reminderTime;
129
				}
130
			}
131
			else {
132
				$newProps[$this->properties["flagdueby"]] = $reminderTime;
133
			}
134
135
			// save props
136
			mapi_setprops($message, $newProps);
137
			mapi_savechanges($message);
138
139
			$result = true;
140
		}
141
142
		if ($result) {
143
			/*
144
			 * @FIXME: Fix notifications for reminders.
145
			 * Notifications are currently disabled, because deleting multiple items will notify
146
			 * hierarchy multiple time but no data is changed with folder item in hierarchy.
147
			 * so it will push same data again which will lead to an error.
148
			 */
149
			// $props = mapi_getprops($message, array(PR_ENTRYID, PR_PARENT_ENTRYID, PR_STORE_ENTRYID));
150
			// $GLOBALS["bus"]->notify(bin2hex($props[PR_PARENT_ENTRYID]), TABLE_SAVE, $props);
151
		}
152
		$this->sendFeedback($result);
153
	}
154
155
	/**
156
	 * Function which is use to dismiss the reminder.
157
	 *
158
	 * @param object $store   MAPI Message Store Object
159
	 * @param string $entryid entryid of the message
160
	 */
161
	public function dismissItem($store, $entryid) {
162
		$result = false;
163
		$message = mapi_msgstore_openentry($store, $entryid);
164
		if ($message) {
165
			$newProps = [];
166
			$props = mapi_getprops($message, $this->properties);
167
168
			if (stripos($props[$this->properties["message_class"]], "IPM.Appointment") === 0) {
169
				if (isset($props[$this->properties["appointment_recurring"]]) && $props[$this->properties["appointment_recurring"]]) {
170
					$recurrence = new Recurrence($store, $message);
0 ignored issues
show
Bug introduced by
$store of type object is incompatible with the type resource expected by parameter $store of Recurrence::__construct(). ( Ignorable by Annotation )

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

170
					$recurrence = new Recurrence(/** @scrutinizer ignore-type */ $store, $message);
Loading history...
171
					// check for next reminder after "now" for the next instance
172
					$nextReminder = $recurrence->getNextReminderTime(time());
173
					if ($nextReminder) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $nextReminder of type false|integer is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
174
						$newProps[$this->properties["flagdueby"]] = $nextReminder;
175
					}
176
					else {
177
						$newProps[$this->properties["reminder"]] = false;
178
					}
179
				}
180
				else {
181
					$newProps[$this->properties["reminder"]] = false;
182
				}
183
			}
184
			elseif (stripos($props[$this->properties["message_class"]], "IPM.Task") === 0) {
185
				$newProps[$this->properties["reminder"]] = false;
186
187
				if (isset($props[$this->properties['task_recurring']]) && $props[$this->properties['task_recurring']] == 1) {
188
					$newProps[$this->properties['task_resetreminder']] = true;
189
				}
190
			}
191
			else {
192
				$newProps[$this->properties["reminder"]] = false;
193
			}
194
195
			// save props
196
			mapi_setprops($message, $newProps);
197
			mapi_savechanges($message);
198
199
			$result = true;
200
		}
201
202
		if ($result) {
203
			/*
204
			 * @FIXME: Fix notifications for reminders.
205
			 * Notifications are currently disabled, because deleting multiple items will notify
206
			 * hierarchy multiple time but no data is changed with folder item in hierarchy.
207
			 * so it will push same data again which will lead to an error.
208
			 */
209
			// $props = mapi_getprops($message, array(PR_ENTRYID, PR_PARENT_ENTRYID, PR_STORE_ENTRYID));
210
			// $GLOBALS["bus"]->notify(bin2hex($props[PR_PARENT_ENTRYID]), TABLE_SAVE, $props);
211
		}
212
		$this->sendFeedback($result);
213
	}
214
215
	/**
216
	 * Function does customization of exception based on module data.
217
	 * like, here it will generate display message based on actionType
218
	 * for particular exception.
219
	 *
220
	 * @param object     $e             Exception object
221
	 * @param string     $actionType    the action type, sent by the client
222
	 * @param MAPIobject $store         store object of message
0 ignored issues
show
Bug introduced by
The type MAPIobject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
223
	 * @param string     $parententryid parent entryid of the message
224
	 * @param string     $entryid       entryid of the message
225
	 * @param array      $action        the action data, sent by the client
226
	 */
227
	public function handleException(&$e, $actionType = null, $store = null, $parententryid = null, $entryid = null, $action = null) {
228
		if (is_null($e->displayMessage)) {
229
			switch ($actionType) {
230
				case 'delete':
231
					if (!empty($action['message_action']['action_type'])) {
232
						switch ($action['message_action']['action_type']) {
233
							case 'snooze':
234
								if ($e->getCode() == MAPI_E_STORE_FULL) {
235
									$e->setDisplayMessage(_('Cannot snooze the reminder. You may be reminded again.') . '<br />' . $this->getOverQuotaMessage($store));
236
								}
237
								else {
238
									$e->setDisplayMessage(_('Cannot snooze the reminder. You may be reminded again.'));
239
								}
240
								break;
241
242
							case 'dismiss':
243
								if ($e->getCode() == MAPI_E_STORE_FULL) {
244
									$e->setDisplayMessage(_('Cannot dismiss the reminder. You may be reminded again.') . '<br />' . $this->getOverQuotaMessage($store));
245
								}
246
								else {
247
									$e->setDisplayMessage(_('Cannot dismiss the reminder. You may be reminded again.'));
248
								}
249
								break;
250
						}
251
					}
252
					break;
253
			}
254
		}
255
256
		parent::handleException($e, $actionType, $store, $parententryid, $entryid, $action);
257
	}
258
}
259