Test Failed
Branch master (1ac602)
by Mike
02:45
created

FreeBusy::getLocalFreeBusyMessage()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 54
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 28
c 1
b 0
f 0
nop 1
dl 0
loc 54
rs 8.5386
nc 6

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
 * This class is just static class and will not be instantiate and
5
 * It contains the functionality to get freebusy message and folder.
6
 */
7
class FreeBusy {
8
	/**
9
	 *  PR_FREEBUSY_ENTRYIDS contains 4 entryids
10
	 *	PR_FREEBUSY_ENTRYIDS[0] gives associated freebusy folder in calendar
11
	 *	PR_FREEBUSY_ENTRYIDS[1] Localfreebusy (used for delegate properties)
12
	 *	PR_FREEBUSY_ENTRYIDS[2] global Freebusydata in public store
13
	 *	PR_FREEBUSY_ENTRYIDS[3] Freebusydata in IPM_SUBTREE.
14
	 */
15
	public const ASSOCIATED_FREEBUSY_FOLDER = 0;
16
	public const DELEGATE_PROPERTIES = 1;
17
	public const GLOBAL_FREEBUSYDATA = 2;
18
	public const FREEBUSYDATA_IPM_SUBTREE = 3;
19
20
	/**
21
	 * Function will return resource of the local freebusy message of the user's store.
22
	 *
23
	 * @param {MAPIStore} $store (optional) user's store
0 ignored issues
show
Documentation Bug introduced by
The doc comment {MAPIStore} at position 0 could not be parsed: Unknown type name '{' at position 0 in {MAPIStore}.
Loading history...
24
	 *
25
	 * @return {MAPIMessage/Boolean} local freebusy message, otherwise false if message not found
0 ignored issues
show
Documentation Bug introduced by
The doc comment {MAPIMessage/Boolean} at position 0 could not be parsed: Unknown type name '{' at position 0 in {MAPIMessage/Boolean}.
Loading history...
26
	 */
27
	public static function getLocalFreeBusyMessage($store = false) {
28
		if (!$store) {
29
			error_log("getLocalFreeBusyMessage: store not available");
30
31
			return false;
32
		}
33
34
		// Check for mapi_freebusy_openmsg function,
35
		// If yes then use mapi function to get freebusy message.
36
		if (function_exists('mapi_freebusy_openmsg')) {
37
			return mapi_freebusy_openmsg($store);
38
		}
39
40
		// Get 'LocalFreeBusy' message from FreeBusy Store
41
		$root = mapi_msgstore_openentry($store, null);
42
		$storeProps = mapi_getprops($root, [PR_FREEBUSY_ENTRYIDS]);
0 ignored issues
show
Bug introduced by
The constant PR_FREEBUSY_ENTRYIDS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
43
		$localFreeBusyEntryids = $storeProps[PR_FREEBUSY_ENTRYIDS];
44
45
		try {
46
			return mapi_msgstore_openentry($store, $localFreeBusyEntryids[self::DELEGATE_PROPERTIES]);
47
		}
48
		catch (MAPIException $e) {
49
			// Either user store have malformed entryid in PR_FREEBUSY_ENTRYIDS or
50
			// No message found of given entryid in 'Freebusy Data' folder.
51
			if ($e->getCode() == MAPI_E_NOT_FOUND || $e->getCode() == MAPI_E_INVALID_ENTRYID) {
52
				$freeBusyFolder = mapi_msgstore_openentry($store, $localFreeBusyEntryids[self::FREEBUSYDATA_IPM_SUBTREE]);
53
				$table = mapi_folder_getcontentstable($freeBusyFolder);
54
				mapi_table_restrict(
55
					$table,
56
					[
57
						RES_CONTENT,
58
						[
59
							FUZZYLEVEL => FL_PREFIX,
60
							ULPROPTAG => PR_MESSAGE_CLASS,
0 ignored issues
show
Bug introduced by
The constant PR_MESSAGE_CLASS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
61
							VALUE => [PR_MESSAGE_CLASS => "IPM.Microsoft.ScheduleData.FreeBusy"],
62
						],
63
					]
64
				);
65
66
				$items = mapi_table_queryallrows($table, [PR_ENTRYID]);
67
				if (empty($items)) {
68
					// FIXME recreate local freebusy message in 'Freebusy Data' folder.
69
					error_log("Unable to find local free busy message in 'Freebusy Data' folder");
70
71
					return false;
72
				}
73
74
				$localFreeBusyEntryids[1] = $items[0][PR_ENTRYID];
75
76
				// Updating the entryid in the PR_FREEBUSY_ENTRYIDS property of user store.
77
				mapi_setprops($root, [PR_FREEBUSY_ENTRYIDS => $localFreeBusyEntryids]);
78
				mapi_savechanges($root);
79
80
				return mapi_msgstore_openentry($store, $localFreeBusyEntryids[self::DELEGATE_PROPERTIES]);
81
			}
82
		}
83
	}
84
85
	/**
86
	 * Function will return resource of the freebusy folder of the user's store.
87
	 *
88
	 * @param {MAPIStore} $store (optional) user's store
0 ignored issues
show
Documentation Bug introduced by
The doc comment {MAPIStore} at position 0 could not be parsed: Unknown type name '{' at position 0 in {MAPIStore}.
Loading history...
89
	 *
90
	 * @return {MAPIFolder} freebusy folder
0 ignored issues
show
Documentation Bug introduced by
The doc comment {MAPIFolder} at position 0 could not be parsed: Unknown type name '{' at position 0 in {MAPIFolder}.
Loading history...
91
	 */
92
	public static function getLocalFreeBusyFolder($store = false) {
93
		if (!$store) {
94
			error_log("getLocalFreeBusyFolder: store not available");
95
96
			return false;
97
		}
98
		// Get 'LocalFreeBusy' message from FreeBusy Store
99
		$root = mapi_msgstore_openentry($store, null);
100
		$storeProps = mapi_getprops($root, [PR_FREEBUSY_ENTRYIDS]);
0 ignored issues
show
Bug introduced by
The constant PR_FREEBUSY_ENTRYIDS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
101
102
		return mapi_msgstore_openentry($store, $storeProps[PR_FREEBUSY_ENTRYIDS][self::FREEBUSYDATA_IPM_SUBTREE]);
103
	}
104
}
105