Test Failed
Push — master ( 64e257...5218bb )
by
unknown
23:17 queued 13:24
created

ReminderListModule::createReminderFolder()   F

Complexity

Conditions 15
Paths 512

Size

Total Lines 121
Code Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 15
eloc 63
c 3
b 0
f 1
nc 512
nop 1
dl 0
loc 121
rs 2.4277

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
	 * Reminder Module.
5
	 *
6
	 * TODO: add description
7
	 */
8
	class ReminderListModule extends ListModule {
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
			$this->properties = $GLOBALS["properties"]->getReminderProperties();
19
		}
20
21
		public function execute() {
22
			foreach ($this->data as $actionType => $action) {
23
				$store = $GLOBALS["mapisession"]->getDefaultMessageStore();
24
				$this->reminderEntryId = $this->getReminderFolderEntryId($store);
0 ignored issues
show
Bug Best Practice introduced by
The property reminderEntryId does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
25
26
				if (isset($actionType)) {
27
					try {
28
						switch ($actionType) {
29
							case "list":
30
								$this->getReminders();
31
								break;
32
33
							default:
34
								$this->handleUnknownActionType($actionType);
35
						}
36
					}
37
					catch (MAPIException $e) {
38
						$this->processException($e, $actionType, $store, null, null, $action);
39
					}
40
				}
41
			}
42
		}
43
44
		public function getReminderFolderEntryId($store) {
45
			$root = mapi_msgstore_openentry($store, null);
46
			$rootProps = mapi_getprops($root, [PR_REM_ONLINE_ENTRYID]);
47
			if (isset($rootProps[PR_REM_ONLINE_ENTRYID])) {
48
				return $rootProps[PR_REM_ONLINE_ENTRYID];
49
			}
50
51
			// Reminder folder didn't exist, create one
52
			return $this->createReminderFolder($store);
53
		}
54
55
		public function createReminderFolder($store) {
56
			$storeProps = mapi_getprops($store, [PR_IPM_OUTBOX_ENTRYID, PR_IPM_WASTEBASKET_ENTRYID, PR_IPM_SUBTREE_ENTRYID]);
57
			$root = mapi_msgstore_openentry($store, null);
58
			$rootProps = mapi_getprops($root, [PR_ADDITIONAL_REN_ENTRYIDS, PR_IPM_DRAFTS_ENTRYID]);
59
60
			$folders = [];
61
			/* The list of forbidden folders (MS-OXORMDR v14 §1.1) */
62
			if (isset($storeProps[PR_IPM_WASTEBASKET_ENTRYID])) {
63
				$folders[] = $storeProps[PR_IPM_WASTEBASKET_ENTRYID];
64
			}
65
			if (isset($rootProps[PR_ADDITIONAL_REN_ENTRYIDS]) && !empty($rootProps[PR_ADDITIONAL_REN_ENTRYIDS][4])) {
66
				$folders[] = $rootProps[PR_ADDITIONAL_REN_ENTRYIDS][4];
67
			} // junk mail
68
			if (isset($rootProps[PR_IPM_DRAFTS_ENTRYID])) {
69
				$folders[] = $rootProps[PR_IPM_DRAFTS_ENTRYID];
70
			}
71
			if (isset($storeProps[PR_IPM_OUTBOX_ENTRYID])) {
72
				$folders[] = $storeProps[PR_IPM_OUTBOX_ENTRYID];
73
			}
74
			if (isset($rootProps[PR_ADDITIONAL_REN_ENTRYIDS]) && !empty($rootProps[PR_ADDITIONAL_REN_ENTRYIDS][0])) {
75
				$folders[] = $rootProps[PR_ADDITIONAL_REN_ENTRYIDS][0];
76
			} // conflicts
77
			if (isset($rootProps[PR_ADDITIONAL_REN_ENTRYIDS]) && !empty($rootProps[PR_ADDITIONAL_REN_ENTRYIDS][2])) {
78
				$folders[] = $rootProps[PR_ADDITIONAL_REN_ENTRYIDS][2];
79
			} // local failures
80
			if (isset($rootProps[PR_ADDITIONAL_REN_ENTRYIDS]) && !empty($rootProps[PR_ADDITIONAL_REN_ENTRYIDS][3])) {
81
				$folders[] = $rootProps[PR_ADDITIONAL_REN_ENTRYIDS][3];
82
			} // server failures
83
			if (isset($rootProps[PR_ADDITIONAL_REN_ENTRYIDS]) && !empty($rootProps[PR_ADDITIONAL_REN_ENTRYIDS][1])) {
84
				$folders[] = $rootProps[PR_ADDITIONAL_REN_ENTRYIDS][1];
85
			} // sync issues
86
87
			$folderRestriction = [];
88
			foreach ($folders as $folder) {
89
				$folderRestriction[] = [RES_PROPERTY,
90
					[
91
						RELOP => RELOP_NE,
92
						ULPROPTAG => $this->properties["parent_entryid"],
93
						VALUE => [$this->properties["parent_entryid"] => $folder],
94
					],
95
				];
96
			}
97
98
			$res =
99
				[RES_AND,
100
					[
101
						[RES_AND,
102
							$folderRestriction,
103
						],
104
						[RES_AND,
105
							[
106
								[RES_NOT,
107
									[
108
										[RES_AND,
109
											[
110
												[RES_EXIST,
111
													[
112
														ULPROPTAG => $this->properties["message_class"],
113
													],
114
												],
115
												[RES_CONTENT,
116
													[
117
														FUZZYLEVEL => FL_PREFIX,
118
														ULPROPTAG => $this->properties["message_class"],
119
														VALUE => [$this->properties["message_class"] => "IPM.Schedule"],
120
													],
121
												],
122
											],
123
										],
124
									],
125
								],
126
								[RES_BITMASK,
127
									[
128
										ULTYPE => BMR_EQZ,
129
										ULPROPTAG => $this->properties["message_flags"],
130
										ULMASK => MSGFLAG_SUBMIT,
131
									],
132
								],
133
								[RES_OR,
134
									[
135
										[RES_PROPERTY,
136
											[
137
												RELOP => RELOP_EQ,
138
												ULPROPTAG => $this->properties["reminder"],
139
												VALUE => [$this->properties["reminder"] => true],
140
											],
141
										],
142
										[RES_AND,
143
											[
144
												[RES_EXIST,
145
													[
146
														ULPROPTAG => $this->properties["recurring"],
147
													],
148
												],
149
												[RES_PROPERTY,
150
													[
151
														RELOP => RELOP_EQ,
152
														ULPROPTAG => $this->properties["recurring"],
153
														VALUE => [$this->properties["recurring"] => true],
154
													],
155
												],
156
											],
157
										],
158
									],
159
								],
160
							],
161
						],
162
					],
163
				];
164
165
			$folder = mapi_folder_createfolder($root, _("Reminders"), "", OPEN_IF_EXISTS, FOLDER_SEARCH);
166
			mapi_setprops($folder, [PR_CONTAINER_CLASS => "Outlook.Reminder"]);
167
			mapi_savechanges($folder);
168
169
			mapi_folder_setsearchcriteria($folder, $res, [$storeProps[PR_IPM_SUBTREE_ENTRYID]], RECURSIVE_SEARCH);
170
			$folderProps = mapi_getprops($folder, [PR_ENTRYID]);
171
172
			mapi_setprops($root, [PR_REM_ONLINE_ENTRYID => $folderProps[PR_ENTRYID]]);
173
			mapi_savechanges($root);
174
175
			return $folderProps[PR_ENTRYID];
176
		}
177
178
		public function getReminders() {
179
			$data = [];
180
181
			$store = $GLOBALS["mapisession"]->getDefaultMessageStore();
182
183
			$restriction = [RES_AND,
184
				[
185
					[RES_PROPERTY,
186
						[
187
							RELOP => RELOP_LT,
188
							ULPROPTAG => $this->properties["flagdueby"],
189
							VALUE => [$this->properties["flagdueby"] => time()],
190
						],
191
					],
192
					[RES_PROPERTY,
193
						[
194
							RELOP => RELOP_EQ,
195
							ULPROPTAG => $this->properties["reminder"],
196
							VALUE => true,
197
						],
198
					],
199
					[RES_PROPERTY,
200
						[
201
							RELOP => RELOP_NE,
202
							ULPROPTAG => $this->properties["message_class"],
203
							VALUE => "IPM.TaskRequest",
204
						],
205
					],
206
					[RES_PROPERTY,
207
						[
208
							RELOP => RELOP_NE,
209
							ULPROPTAG => $this->properties["message_class"],
210
							VALUE => "IPM.TaskRequest.Cancel",
211
						],
212
					],
213
					[RES_PROPERTY,
214
						[
215
							RELOP => RELOP_NE,
216
							ULPROPTAG => $this->properties["message_class"],
217
							VALUE => "IPM.TaskRequest.Accept",
218
						],
219
					],
220
					[RES_PROPERTY,
221
						[
222
							RELOP => RELOP_NE,
223
							ULPROPTAG => $this->properties["message_class"],
224
							VALUE => "IPM.TaskRequest.Update",
225
						],
226
					],
227
					[RES_PROPERTY,
228
						[
229
							RELOP => RELOP_NE,
230
							ULPROPTAG => $this->properties["message_class"],
231
							VALUE => "IPM.TaskRequest.Complete",
232
						],
233
					],
234
				],
235
			];
236
237
			try {
238
				$reminderfolder = mapi_msgstore_openentry($store, $this->reminderEntryId);
239
			}
240
			catch (MAPIException $e) {
241
				// if the reminder folder does not exist, try to recreate it.
242
				if ($e->getCode() == MAPI_E_NOT_FOUND) {
243
					$e->setHandled();
244
245
					$this->reminderEntryId = $this->createReminderFolder($store);
0 ignored issues
show
Bug Best Practice introduced by
The property reminderEntryId does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
246
					$reminderfolder = mapi_msgstore_openentry($store, $this->reminderEntryId);
247
				}
248
			}
249
250
			$remindertable = mapi_folder_getcontentstable($reminderfolder, MAPI_DEFERRED_ERRORS);
251
			if (!$remindertable) {
252
				return false;
253
			}
254
255
			mapi_table_restrict($remindertable, $restriction, TBL_BATCH);
256
			mapi_table_sort($remindertable, [$this->properties["flagdueby"] => TABLE_SORT_DESCEND], TBL_BATCH);
257
258
			// reminder store hold only 99 records as
259
			// we show 99 notification on client side.
260
			$rows = mapi_table_queryrows($remindertable, $this->properties, 0, MAX_NUM_REMINDERS);
261
			$data["item"] = [];
262
263
			foreach ($rows as $row) {
264
				if (isset($row[$this->properties["appointment_recurring"]]) && $row[$this->properties["appointment_recurring"]]) {
265
					$recur = new Recurrence($store, $row);
0 ignored issues
show
Bug introduced by
The type Recurrence 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...
266
267
					/**
268
					 * FlagDueBy == PidLidReminderSignalTime.
269
					 * FlagDueBy handles whether we should be showing the item; if now() is after FlagDueBy, then we should show a reminder
270
					 * for this recurrence. However, the item we will show is either the last passed occurrence (overdue), or the next occurrence, depending
271
					 * on whether we have reached the next occurrence yet (the reminder_time of the next item is ignored).
272
					 *
273
					 * The way we handle this is to get all occurrences between the 'flagdueby' moment and the current time. This will
274
					 * yield N items (may be a lot of it was not dismissed for a long time). We can then take the last item in this list, and this is the item
275
					 * we will show to the user. The idea here is:
276
					 *
277
					 * The item we want to show is the last item in that list (new occurrences that have started uptil now should override old ones)
278
					 *
279
					 * Add the reminder_minutes (default 15 minutes for calendar, 0 for tasks) to check over the gap between FlagDueBy and the start time of the
280
					 * occurrence, if "now" would be in between these values.
281
					 */
282
					$remindertimeinseconds = $row[$this->properties["reminder_minutes"]] * 60;
283
					$occurrences = $recur->getItems($row[$this->properties["flagdueby"]], time() + ($remindertimeinseconds), 0, true);
284
285
					if (empty($occurrences)) {
286
						continue;
287
					}
288
289
					// More than one occurrence, use the last one instead of the first one after flagdueby
290
					$occ = $occurrences[count($occurrences) - 1];
291
292
					// Bydefault, on occurrence reminder is true but if reminder value is set to false then we don't send popup reminder for this occurrence
293
					if (!(isset($occ[$this->properties['reminder']]) && $occ[$this->properties['reminder']] == 0)) {
294
						$row[$this->properties["reminder_time"]] = $occ[$this->properties["appointment_startdate"]];
295
						$row[$this->properties["appointment_startdate"]] = $occ[$this->properties["appointment_startdate"]];
296
						$row[$this->properties["appointment_enddate"]] = $occ[$this->properties["appointment_startdate"]];
297
					}
298
				}
299
300
				// Add the non-bogus rows
301
				array_push($data["item"], Conversion::mapMAPI2XML($this->properties, $row));
302
			}
303
304
			$this->addActionData("list", $data);
305
			$GLOBALS["bus"]->addData($this->getResponseData());
306
307
			// Trigger the newmailnotifier
308
			$GLOBALS["bus"]->notify(REQUEST_ENTRYID, HIERARCHY_UPDATE, ['', '']);
309
310
			return true;
311
		}
312
	}
313