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