Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 18 | class CalendarAPI extends API |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var Type\FolderIdType |
||
| 22 | */ |
||
| 23 | protected $folderId; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Pick a Calendar based on it's name |
||
| 27 | * |
||
| 28 | * @param string|null $displayName |
||
| 29 | * @return $this |
||
| 30 | */ |
||
| 31 | 6 | public function pickCalendar($displayName = null) |
|
| 32 | { |
||
| 33 | 6 | if ($displayName == 'default.calendar' || $displayName == null) { |
|
|
|
|||
| 34 | $folder = $this->getFolderByDistinguishedId('calendar'); |
||
| 35 | } else { |
||
| 36 | 6 | $folder = $this->getFolderByDisplayName($displayName, 'calendar'); |
|
| 37 | } |
||
| 38 | |||
| 39 | $this->folderId = $folder->getFolderId(); |
||
| 40 | |||
| 41 | return $this; |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @return Type\FolderIdType |
||
| 46 | */ |
||
| 47 | public function getFolderId() |
||
| 48 | { |
||
| 49 | if ($this->folderId === null) { |
||
| 50 | $this->pickCalendar(); |
||
| 51 | } |
||
| 52 | |||
| 53 | return $this->folderId; |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param Type\FolderIdType $folderId |
||
| 58 | * @return $this |
||
| 59 | */ |
||
| 60 | public function setFolderId($folderId) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Create one or more calendar items |
||
| 69 | * |
||
| 70 | * @param $items CalendarItemType[]|CalendarItemType|array or more calendar items to create |
||
| 71 | * @param $options array Options to merge in to the request |
||
| 72 | * @return Type\ItemIdType[] |
||
| 73 | */ |
||
| 74 | public function createCalendarItems($items, $options = array()) |
||
| 75 | { |
||
| 76 | //If the item passed in is an object, or if it's an associative] |
||
| 77 | // array waiting to be an object, let's put it in to an array |
||
| 78 | if (!is_array($items) || Type::arrayIsAssoc($items)) { |
||
| 79 | $items = array($items); |
||
| 80 | } |
||
| 81 | |||
| 82 | $item = array('CalendarItem' => $items); |
||
| 83 | $defaultOptions = array( |
||
| 84 | 'SendMeetingInvitations' => Enumeration\CalendarItemCreateOrDeleteOperationType::SEND_TO_NONE, |
||
| 85 | 'SavedItemFolderId' => array( |
||
| 86 | 'FolderId' => $this->getFolderId()->toXmlObject() |
||
| 87 | ) |
||
| 88 | ); |
||
| 89 | |||
| 90 | $options = array_replace_recursive($defaultOptions, $options); |
||
| 91 | |||
| 92 | $items = $this->createItems($item, $options); |
||
| 93 | |||
| 94 | if (!is_array($items)) { |
||
| 95 | $items = array($items); |
||
| 96 | } |
||
| 97 | |||
| 98 | return $items; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Get a list of calendar items between two dates/times |
||
| 103 | * |
||
| 104 | * @param string|DateTime $start |
||
| 105 | * @param string|DateTime $end |
||
| 106 | * @param array $options |
||
| 107 | * @return CalendarItemType[]|Type\FindItemParentType |
||
| 108 | */ |
||
| 109 | public function getCalendarItems($start = '12:00 AM', $end = '11:59 PM', $options = array()) |
||
| 110 | { |
||
| 111 | if (!($start instanceof DateTime)) { |
||
| 112 | $start = new DateTime($start); |
||
| 113 | } |
||
| 114 | |||
| 115 | if (!($end instanceof DateTime)) { |
||
| 116 | $end = new DateTime($end); |
||
| 117 | } |
||
| 118 | |||
| 119 | $request = array( |
||
| 120 | 'Traversal' => 'Shallow', |
||
| 121 | 'ItemShape' => array( |
||
| 122 | 'BaseShape' => 'AllProperties' |
||
| 123 | ), |
||
| 124 | 'CalendarView' => array( |
||
| 125 | 'MaxEntriesReturned' => 100, |
||
| 126 | 'StartDate' => $start->format('c'), |
||
| 127 | 'EndDate' => $end->format('c') |
||
| 128 | ), |
||
| 129 | 'ParentFolderIds' => array( |
||
| 130 | 'FolderId' => $this->getFolderId()->toXmlObject() |
||
| 131 | ) |
||
| 132 | ); |
||
| 133 | |||
| 134 | $request = array_replace_recursive($request, $options); |
||
| 135 | |||
| 136 | $request = Type::buildFromArray($request); |
||
| 137 | $response = $this->getClient()->FindItem($request); |
||
| 138 | $items = $response; |
||
| 139 | |||
| 140 | return $items; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param $id |
||
| 145 | * @param $changeKey |
||
| 146 | * @return Type\CalendarItemType |
||
| 147 | */ |
||
| 148 | public function getCalendarItem($id, $changeKey) |
||
| 149 | { |
||
| 150 | return $this->getItem(['Id' => $id, 'ChangeKey' => $changeKey]); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Updates a calendar item with changes |
||
| 155 | * |
||
| 156 | * @param $itemId Type\ItemIdType |
||
| 157 | * @param $changes |
||
| 158 | * @return Type\CalendarItemType[] |
||
| 159 | */ |
||
| 160 | View Code Duplication | public function updateCalendarItem(Type\ItemIdType $itemId, $changes) |
|
| 161 | { |
||
| 162 | //Create the request |
||
| 163 | $request = array( |
||
| 164 | 'ItemChange' => array( |
||
| 165 | 'ItemId' => $itemId->toArray(), |
||
| 166 | 'Updates' => $this->buildUpdateItemChanges('CalendarItem', 'calendar', $changes) |
||
| 167 | ) |
||
| 168 | ); |
||
| 169 | |||
| 170 | $options = array( |
||
| 171 | 'SendMeetingInvitationsOrCancellations' => 'SendToNone' |
||
| 172 | ); |
||
| 173 | |||
| 174 | $items = $this->updateItems($request, $options)->getCalendarItem(); |
||
| 175 | |||
| 176 | if (!is_array($items)) { |
||
| 177 | $items = array($items); |
||
| 178 | } |
||
| 179 | |||
| 180 | return $items; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param Type\ItemIdType $itemId |
||
| 185 | * @param array $options |
||
| 186 | * @return bool |
||
| 187 | */ |
||
| 188 | public function deleteCalendarItem(Type\ItemIdType $itemId, $options = array()) |
||
| 189 | { |
||
| 190 | $defaultOptions = array( |
||
| 191 | 'SendMeetingCancellations' => 'SendToNone' |
||
| 192 | ); |
||
| 193 | |||
| 194 | $options = array_replace_recursive($defaultOptions, $options); |
||
| 195 | return $this->deleteItems($itemId, $options); |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param string $start |
||
| 200 | * @param string $end |
||
| 201 | * @param array $options |
||
| 202 | */ |
||
| 203 | public function deleteAllCalendarItems($start = '12:00 AM', $end = '11:59 PM', $options = array()) |
||
| 204 | { |
||
| 205 | $items = $this->getCalendarItems($start, $end, $options); |
||
| 206 | foreach ($items as $item) { |
||
| 207 | $this->deleteCalendarItem($item->getItemId()); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Get a list of changes on the calendar items |
||
| 213 | * |
||
| 214 | * @param null $syncState |
||
| 215 | * @param array $options |
||
| 216 | * @return API\Message\SyncFolderItemsResponseMessageType |
||
| 217 | */ |
||
| 218 | public function listChanges($syncState = null, $options = array()) |
||
| 219 | { |
||
| 220 | return parent::listItemChanges($this->getFolderId(), $syncState, $options); |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param Type\ItemIdType $itemId |
||
| 225 | * @param string $message |
||
| 226 | * @param string $sensitivity |
||
| 227 | * @param array $options |
||
| 228 | * |
||
| 229 | * @return Type\ItemIdType[] |
||
| 230 | */ |
||
| 231 | View Code Duplication | public function acceptMeeting($itemId, $message, $sensitivity = 'Private', $options = array()) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * @param $itemId |
||
| 254 | * @param $message |
||
| 255 | * @param string $sensitivity |
||
| 256 | * @param array $options |
||
| 257 | * @return Type\ItemIdType[] |
||
| 258 | */ |
||
| 259 | View Code Duplication | public function declineMeeting($itemId, $message, $sensitivity = 'Private', $options = array()) |
|
| 279 | } |
||
| 280 |