| Total Complexity | 40 |
| Total Lines | 243 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like MailListModule often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MailListModule, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class MailListModule extends ListModule |
||
| 6 | { |
||
| 7 | // Temporary var to store the inbox entryid of the processed store |
||
| 8 | private $_inboxEntryId; |
||
| 9 | |||
| 10 | private $_inbox = NULL; |
||
| 11 | |||
| 12 | private $_inboxTotal = NULL; |
||
| 13 | |||
| 14 | private $_inboxTotalUnread = NULL; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Constructor |
||
| 18 | * @param int $id unique id. |
||
| 19 | * @param array $data list of all actions. |
||
| 20 | */ |
||
| 21 | function __construct($id, $data) |
||
| 22 | { |
||
| 23 | parent::__construct($id, $data); |
||
| 24 | $this->properties = $GLOBALS["properties"]->getMailListProperties(); |
||
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Creates the notifiers for this module, |
||
| 29 | * and register them to the Bus. |
||
| 30 | */ |
||
| 31 | function createNotifiers() |
||
| 32 | { |
||
| 33 | $entryid = $this->getEntryID(); |
||
| 34 | $GLOBALS["bus"]->registerNotifier('maillistnotifier', $entryid); |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Executes all the actions in the $data variable. |
||
| 39 | * @return boolean true on success of false on fialure. |
||
| 40 | */ |
||
| 41 | function execute() |
||
| 42 | { |
||
| 43 | $GLOBALS['PluginManager']->triggerHook("server.module.maillistmodule.execute.before", array('moduleObject' =>& $this)); |
||
| 44 | |||
| 45 | foreach($this->data as $actionType => $action) |
||
| 46 | { |
||
| 47 | if(isset($actionType)) { |
||
| 48 | try { |
||
| 49 | $this->store = $this->getActionStore($action); |
||
|
|
|||
| 50 | $entryid = $this->getActionEntryID($action); |
||
| 51 | |||
| 52 | // Reset variables |
||
| 53 | $this->_inbox = NULL; |
||
| 54 | $this->_inboxEntryId = NULL; |
||
| 55 | $this->_inboxTotal = NULL; |
||
| 56 | $this->_inboxTotalUnread = NULL; |
||
| 57 | |||
| 58 | $this->currentActionData = array( |
||
| 59 | 'store' => $this->store, |
||
| 60 | 'entryid' => $entryid, |
||
| 61 | 'actionType' => $actionType, |
||
| 62 | 'action' => $action, |
||
| 63 | ); |
||
| 64 | |||
| 65 | switch($actionType) |
||
| 66 | { |
||
| 67 | case "list": |
||
| 68 | case "updatelist": |
||
| 69 | $this->getDelegateFolderInfo($this->store); |
||
| 70 | $this->messageList($this->store, $entryid, $action, $actionType); |
||
| 71 | break; |
||
| 72 | case "search": |
||
| 73 | // @FIXME add handling for private items |
||
| 74 | $this->search($this->store, $entryid, $action, $actionType); |
||
| 75 | break; |
||
| 76 | case "updatesearch": |
||
| 77 | $this->updatesearch($this->store, $entryid, $action); |
||
| 78 | break; |
||
| 79 | case "stopsearch": |
||
| 80 | $this->stopSearch($this->store, $entryid, $action); |
||
| 81 | break; |
||
| 82 | default: |
||
| 83 | $this->handleUnknownActionType($actionType); |
||
| 84 | } |
||
| 85 | } catch (MAPIException $e) { |
||
| 86 | $this->processException($e, $actionType); |
||
| 87 | } catch (SearchException $e) { |
||
| 88 | $this->processException($e, $actionType); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | } |
||
| 92 | $GLOBALS['PluginManager']->triggerHook("server.module.maillistmodule.execute.after", array('moduleObject' =>& $this)); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Returns the Inbox folder of the currently used store if found, NULL otherwise |
||
| 97 | * |
||
| 98 | * @return Resource The inbox folder of the currently used store |
||
| 99 | */ |
||
| 100 | function getInbox() { |
||
| 101 | if ($this->_inbox === NULL) { |
||
| 102 | try { |
||
| 103 | $this->_inbox = mapi_msgstore_getreceivefolder($this->store); |
||
| 104 | } catch (MAPIException $e) { |
||
| 105 | // don't propagate this error to parent handlers, if store doesn't support it |
||
| 106 | if($e->getCode() === MAPI_E_NO_SUPPORT) { |
||
| 107 | $e->setHandled(); |
||
| 108 | return NULL; |
||
| 109 | } |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | return $this->_inbox; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Returns the entryid of the Inbox folder of the currently used store if found, false otherwise |
||
| 118 | * |
||
| 119 | * @return String hexamdecimal representation of the entryid of the Inbox |
||
| 120 | */ |
||
| 121 | function getInboxEntryId() { |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Returns the total number of items in the Inbox of the currently used store |
||
| 141 | * |
||
| 142 | * @return Integer the number if items in the Inbox folder |
||
| 143 | */ |
||
| 144 | function getInboxTotal($force = false) { |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Returns the number of unread items in the Inbox of the currently used store. |
||
| 157 | * |
||
| 158 | * @return Integer the numer of unread items in the Inbox folder |
||
| 159 | */ |
||
| 160 | function getInboxTotalUnread($force = false) { |
||
| 161 | if ($this->_inboxTotalUnread === NULL || $force) { |
||
| 162 | $this->getIboxTotal($force); |
||
| 163 | } |
||
| 164 | |||
| 165 | return $this->_inboxTotalUnread; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Function does customization of exception based on module data. |
||
| 170 | * like, here it will generate display message based on actionType |
||
| 171 | * for particular exception. |
||
| 172 | * |
||
| 173 | * @param object $e Exception object |
||
| 174 | * @param string $actionType the action type, sent by the client |
||
| 175 | * @param MAPIobject $store Store object of the current user. |
||
| 176 | * @param string $parententryid parent entryid of the message. |
||
| 177 | * @param string $entryid entryid of the message. |
||
| 178 | * @param array $action the action data, sent by the client |
||
| 179 | */ |
||
| 180 | function handleException(&$e, $actionType = null, $store = null, $parententryid = null, $entryid = null, $action = null) |
||
| 181 | { |
||
| 182 | if(is_null($e->displayMessage)) { |
||
| 183 | switch($actionType) |
||
| 184 | { |
||
| 185 | case "list": |
||
| 186 | if($e->getCode() == MAPI_E_NO_ACCESS) |
||
| 187 | $e->setDisplayMessage(_("You have insufficient privileges to see the contents of this folder.")); |
||
| 188 | else |
||
| 189 | $e->setDisplayMessage(_("Could not load the contents of this folder.")); |
||
| 190 | break; |
||
| 191 | |||
| 192 | case "search": |
||
| 193 | if($e->getCode() == MAPI_E_NO_ACCESS) |
||
| 194 | $e->setDisplayMessage(_("You have insufficient privileges to perform search operation in this folder.")); |
||
| 195 | else |
||
| 196 | $e->setDisplayMessage(_("Error in search, please try again")); |
||
| 197 | break; |
||
| 198 | |||
| 199 | case "updatesearch": |
||
| 200 | $e->setDisplayMessage(_("Could not update search results.")); |
||
| 201 | break; |
||
| 202 | |||
| 203 | case "stopsearch": |
||
| 204 | $e->setDisplayMessage(_("Could not stop search operation.")); |
||
| 205 | break; |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | parent::handleException($e, $actionType, $store, $parententryid, $entryid, $action); |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Parses the incoming sort request and builds a MAPI sort order. |
||
| 214 | * Overridden to rewrite the sorting for flags. (because the flags that are shown in grommunio Web |
||
| 215 | * are a combination of several properties) |
||
| 216 | * |
||
| 217 | * @param array $action the action data, sent by the client |
||
| 218 | * @param array|bool $map Normally properties are mapped from the XML to MAPI by the standard |
||
| 219 | * $this->properties mapping. However, if you want other mappings, you can specify them in this parameter. |
||
| 220 | * @param bool $allow_multi_instance Sort as multi-value instance (every value a different row) |
||
| 221 | * @param array|bool a custom set of properties to use instead of the properties stored in module |
||
| 222 | */ |
||
| 223 | function parseSortOrder($action, $map = false, $allow_multi_instance = false, $properties = false) |
||
| 248 | } |
||
| 249 | } |
||
| 250 | ?> |
||
| 251 |