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:
Complex classes like API 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 API, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class API |
||
19 | { |
||
20 | protected static $defaultClientOptions = array( |
||
21 | 'version' => ExchangeWebServices::VERSION_2010 |
||
22 | ); |
||
23 | |||
24 | 34 | public function __construct(ExchangeWebServices $client = null) |
|
25 | { |
||
26 | 34 | if ($client) { |
|
27 | 34 | $this->setClient($client); |
|
28 | 34 | } |
|
29 | 34 | } |
|
30 | |||
31 | /** |
||
32 | * @return Type\EmailAddressType |
||
33 | */ |
||
34 | 26 | public function getPrimarySmtpMailbox() |
|
35 | { |
||
36 | 26 | return $this->getClient()->getPrimarySmtpMailbox(); |
|
37 | } |
||
38 | |||
39 | /** |
||
40 | * Storing the API client |
||
41 | * @var ExchangeWebServices |
||
42 | */ |
||
43 | private $client; |
||
44 | |||
45 | /** |
||
46 | * Get a calendar item |
||
47 | * |
||
48 | * @param string $name |
||
49 | * @return CalendarAPI |
||
50 | */ |
||
51 | 6 | public function getCalendar($name = null) |
|
52 | { |
||
53 | 6 | $calendar = new CalendarAPI(); |
|
54 | 6 | $calendar->setClient($this->getClient()); |
|
55 | 6 | $calendar->pickCalendar($name); |
|
56 | |||
57 | 6 | return $calendar; |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param string $folderName |
||
62 | * @return MailAPI |
||
63 | */ |
||
64 | 6 | public function getMailbox($folderName = null) |
|
65 | { |
||
66 | 6 | $mailApi = new MailAPI(); |
|
67 | 6 | $mailApi->setClient($this->getClient()); |
|
68 | 6 | $mailApi->pickMailFolder($folderName); |
|
69 | |||
70 | 6 | return $mailApi; |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * Set the API client |
||
75 | * |
||
76 | * @param ExchangeWebServices $client |
||
77 | * @return $this |
||
78 | */ |
||
79 | 34 | public function setClient($client) |
|
80 | { |
||
81 | 34 | $this->client = $client; |
|
82 | |||
83 | 34 | return $this; |
|
84 | } |
||
85 | |||
86 | /** |
||
87 | * Get the API client |
||
88 | * |
||
89 | * @return ExchangeWebServices |
||
90 | */ |
||
91 | 33 | public function getClient() |
|
92 | { |
||
93 | 33 | return $this->client; |
|
94 | } |
||
95 | |||
96 | 33 | public static function withUsernameAndPassword($server, $username, $password, $options = []) |
|
97 | { |
||
98 | 33 | return new static(ExchangeWebServices::fromUsernameAndPassword( |
|
99 | 33 | $server, |
|
100 | 33 | $username, |
|
101 | 33 | $password, |
|
102 | 33 | array_replace_recursive(self::$defaultClientOptions, $options) |
|
103 | 33 | )); |
|
104 | } |
||
105 | |||
106 | 1 | public static function withCallbackToken($server, $token, $options = []) |
|
107 | { |
||
108 | 1 | return new static(ExchangeWebServices::fromCallbackToken( |
|
109 | 1 | $server, |
|
110 | 1 | $token, |
|
111 | 1 | array_replace_recursive(self::$defaultClientOptions, $options) |
|
112 | 1 | )); |
|
113 | } |
||
114 | |||
115 | 1 | public function getPrimarySmptEmailAddress() |
|
116 | { |
||
117 | 1 | if ($this->getPrimarySmtpMailbox() == null) { |
|
118 | 1 | return null; |
|
119 | } |
||
120 | |||
121 | 1 | return $this->getPrimarySmtpMailbox()->getEmailAddress(); |
|
122 | } |
||
123 | |||
124 | 1 | public function setPrimarySmtpEmailAddress($emailAddress) |
|
125 | { |
||
126 | 1 | $this->getClient()->setPrimarySmtpEmailAddress($emailAddress); |
|
127 | |||
128 | 1 | return $this; |
|
129 | } |
||
130 | |||
131 | /** |
||
132 | * Create items through the API client |
||
133 | * |
||
134 | * @param $items |
||
135 | * @param array $options |
||
136 | * @return Type |
||
137 | */ |
||
138 | 16 | public function createItems($items, $options = array()) |
|
139 | { |
||
140 | 16 | $items = Utilities\ensureIsArray($items); |
|
141 | $request = array( |
||
142 | 'Items' => $items |
||
143 | 16 | ); |
|
144 | |||
145 | 16 | $request = array_replace_recursive($request, $options); |
|
146 | 16 | $request = Type::buildFromArray($request); |
|
147 | |||
148 | 16 | $response = $this->getClient()->CreateItem($request); |
|
149 | |||
150 | 16 | return $response; |
|
151 | } |
||
152 | |||
153 | 4 | public function updateItems($items, $options = array()) |
|
154 | { |
||
155 | $request = array( |
||
156 | 4 | 'ItemChanges' => $items, |
|
157 | 4 | 'MessageDisposition' => 'SaveOnly', |
|
158 | 'ConflictResolution' => 'AlwaysOverwrite' |
||
159 | 4 | ); |
|
160 | |||
161 | 4 | $request = array_replace_recursive($request, $options); |
|
162 | |||
163 | 4 | $request = Type::buildFromArray($request); |
|
164 | |||
165 | 4 | $response = $this->getClient()->UpdateItem($request); |
|
166 | 4 | if ($response instanceof UpdateItemResponseMessageType) { |
|
167 | 4 | return $response->getItems(); |
|
168 | } |
||
169 | |||
170 | return Utilities\ensureIsArray($response); |
||
171 | } |
||
172 | |||
173 | 1 | public function createCalendars($names, Type\FolderIdType $parentFolder = null, $options = array()) |
|
174 | { |
||
175 | 1 | if ($parentFolder == null) { |
|
176 | 1 | $parentFolder = $this->getFolderByDistinguishedId('calendar')->getFolderId(); |
|
177 | 1 | } |
|
178 | |||
179 | 1 | return $this->createFolders($names, $parentFolder, $options, 'IPF.Appointment'); |
|
180 | } |
||
181 | |||
182 | public function createContactsFolder($names, Type\FolderIdType $parentFolder = null, $options = array()) |
||
183 | { |
||
184 | if ($parentFolder == null) { |
||
185 | $parentFolder = $this->getFolderByDistinguishedId('contacts')->getFolderId(); |
||
186 | } |
||
187 | |||
188 | return $this->createFolders($names, $parentFolder, $options, 'IPF.Contact'); |
||
189 | } |
||
190 | |||
191 | 4 | public function createFolders($names, Type\FolderIdType $parentFolder, $options = array(), $folderClass = null) |
|
192 | { |
||
193 | 4 | $names = Utilities\ensureIsArray($names); |
|
194 | $names = array_map(function ($name) use ($folderClass) { |
||
195 | 4 | return ['DisplayName' => $name, 'FolderClass' => $folderClass]; |
|
196 | 4 | }, $names); |
|
197 | |||
198 | $request = [ |
||
199 | 4 | 'Folders' => ['Folder' => $names] |
|
200 | 4 | ]; |
|
201 | |||
202 | 4 | if (!empty($parentFolder)) { |
|
203 | 4 | $request['ParentFolderId'] = array('FolderId' => $parentFolder->toArray()); |
|
204 | 4 | } |
|
205 | |||
206 | 4 | $request = array_merge_recursive($request, $options); |
|
207 | |||
208 | 4 | $this->client->CreateFolder($request); |
|
209 | 4 | return true; |
|
210 | } |
||
211 | |||
212 | /** |
||
213 | * @deprecated Please use API::deleteFolders() instead |
||
214 | * |
||
215 | * @param Type\FolderIdType $folderId |
||
216 | * @param array $options |
||
217 | * @return Type |
||
218 | */ |
||
219 | 3 | public function deleteFolder(Type\FolderIdType $folderId, $options = array()) |
|
220 | { |
||
221 | 3 | return $this->deleteFolders($folderId, $options); |
|
222 | } |
||
223 | |||
224 | 4 | public function deleteFolders($folders, $options = array()) |
|
225 | { |
||
226 | 4 | $folders = Utilities\ensureIsArray($folders); |
|
227 | |||
228 | $folderIds = array_map(function ($folderId) { |
||
229 | 4 | return $folderId->toArray(); |
|
230 | 4 | }, $folders); |
|
231 | |||
232 | $request = [ |
||
233 | 4 | 'DeleteType' => 'HardDelete', |
|
234 | 'FolderIds' => array( |
||
235 | 'FolderId' => $folderIds |
||
236 | 4 | ) |
|
237 | 4 | ]; |
|
238 | |||
239 | 4 | $request = array_merge_recursive($request, $options); |
|
240 | 4 | return $this->client->DeleteFolder($request); |
|
241 | } |
||
242 | |||
243 | public function moveItem(Type\ItemIdType $itemId, Type\FolderIdType $folderId, $options = array()) |
||
244 | { |
||
245 | $request = array( |
||
246 | 'ToFolderId' => array('FolderId' => $folderId->toArray()), |
||
247 | 'ItemIds' => array('ItemId' => $itemId->toArray()) |
||
248 | ); |
||
249 | |||
250 | $request = array_merge_recursive($request, $options); |
||
251 | |||
252 | return $this->client->MoveItem($request); |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * @param $items Type\ItemIdType|Type\ItemIdType[] |
||
257 | * @param array $options |
||
258 | * @return bool |
||
259 | */ |
||
260 | 16 | public function deleteItems($items, $options = array()) |
|
261 | { |
||
262 | 16 | $items = Utilities\ensureIsArray($items); |
|
263 | |||
264 | 16 | $items = array_map(function ($item) { |
|
265 | 16 | $item = Type\ItemIdType::buildFromArray($item); |
|
266 | |||
267 | 16 | return $item->toArray(); |
|
268 | 16 | }, $items); |
|
269 | |||
270 | $request = array( |
||
271 | 16 | 'ItemIds' => array('ItemId' => $items), |
|
272 | 'DeleteType' => 'MoveToDeletedItems' |
||
273 | 16 | ); |
|
274 | |||
275 | 16 | $request = array_replace_recursive($request, $options); |
|
276 | 16 | $request = Type::buildFromArray($request); |
|
277 | 16 | $this->getClient()->DeleteItem($request); |
|
278 | |||
279 | //If the delete fails, an Exception will be thrown in processResponse before it gets here |
||
280 | 16 | return true; |
|
281 | } |
||
282 | |||
283 | /** |
||
284 | * @param $identifier |
||
285 | * @return Type\BaseFolderType |
||
286 | */ |
||
287 | 25 | public function getFolder($identifier) |
|
288 | { |
||
289 | $request = array( |
||
290 | 'FolderShape' => array( |
||
291 | 25 | 'BaseShape' => array('_' => 'Default') |
|
292 | 25 | ), |
|
293 | 'FolderIds' => $identifier |
||
294 | 25 | ); |
|
295 | 25 | $request = Type::buildFromArray($request); |
|
296 | |||
297 | 25 | $response = $this->getClient()->GetFolder($request); |
|
298 | |||
299 | 25 | return $response; |
|
300 | } |
||
301 | |||
302 | /** |
||
303 | * Get a folder by it's distinguishedId |
||
304 | * |
||
305 | * @param string $distinguishedId |
||
306 | * @return Type\BaseFolderType |
||
307 | */ |
||
308 | 25 | public function getFolderByDistinguishedId($distinguishedId) |
|
309 | { |
||
310 | 25 | return $this->getFolder(array( |
|
311 | 'DistinguishedFolderId' => array( |
||
312 | 25 | 'Id' => $distinguishedId, |
|
313 | 25 | 'Mailbox' => $this->getPrimarySmtpMailbox() |
|
314 | 25 | ) |
|
315 | 25 | )); |
|
316 | } |
||
317 | |||
318 | /** |
||
319 | * @param $folderId |
||
320 | * @return Type\BaseFolderType |
||
321 | */ |
||
322 | 4 | public function getFolderByFolderId($folderId) |
|
323 | { |
||
324 | 4 | return $this->getFolder(array( |
|
325 | 4 | 'FolderId' => array('Id' => $folderId, 'Mailbox' => $this->getPrimarySmtpMailbox()) |
|
326 | 4 | )); |
|
327 | } |
||
328 | |||
329 | /** |
||
330 | * @param string|Type\FolderIdType $parentFolderId |
||
331 | * @param array $options |
||
332 | * @return Type\BaseFolderType[] |
||
333 | */ |
||
334 | 24 | View Code Duplication | public function getChildrenFolders($parentFolderId = 'root', $options = array()) |
335 | { |
||
336 | 24 | if (is_string($parentFolderId)) { |
|
337 | 16 | $parentFolderId = $this->getFolderByDistinguishedId($parentFolderId)->getFolderId(); |
|
338 | 16 | } |
|
339 | |||
340 | $request = array( |
||
341 | 24 | 'Traversal' => 'Shallow', |
|
342 | 'FolderShape' => array( |
||
343 | 'BaseShape' => 'AllProperties' |
||
344 | 24 | ), |
|
345 | 'ParentFolderIds' => array( |
||
346 | 24 | 'FolderId' => $parentFolderId->toArray() |
|
347 | 24 | ) |
|
348 | 24 | ); |
|
349 | |||
350 | 24 | $request = array_replace_recursive($request, $options); |
|
351 | |||
352 | 24 | $request = Type::buildFromArray($request); |
|
353 | |||
354 | /** @var \garethp\ews\API\Message\FindFolderResponseMessageType $folders */ |
||
355 | 24 | return $this->getClient()->FindFolder($request); |
|
356 | } |
||
357 | |||
358 | /** |
||
359 | * @param string $folderName |
||
360 | * @param string|Type\FolderIdType $parentFolderId |
||
361 | * @param array $options |
||
362 | * @return bool|Type\BaseFolderType |
||
363 | */ |
||
364 | 24 | public function getFolderByDisplayName($folderName, $parentFolderId = 'root', $options = array()) |
|
376 | |||
377 | /** |
||
378 | * @param $itemId array|Type\ItemIdType |
||
379 | * @param array $options |
||
380 | * @return Type |
||
381 | */ |
||
382 | 5 | View Code Duplication | public function getItem($itemId, $options = array()) |
397 | |||
398 | /** |
||
399 | * Get a list of sync changes on a folder |
||
400 | * |
||
401 | * @param Type\FolderIdType $folderId |
||
402 | * @param null $syncState |
||
403 | * @param array $options |
||
404 | * @return SyncFolderItemsResponseMessageType |
||
405 | */ |
||
406 | 2 | public function listItemChanges($folderId, $syncState = null, $options = array()) |
|
407 | { |
||
408 | $request = array( |
||
409 | 2 | 'ItemShape' => array('BaseShape' => 'AllProperties'), |
|
410 | 2 | 'SyncFolderId' => array('FolderId' => $folderId->toXmlObject()), |
|
411 | 2 | 'SyncScope' => 'NormalItems', |
|
412 | 'MaxChangesReturned' => '100' |
||
413 | 2 | ); |
|
414 | |||
415 | 2 | if ($syncState != null) { |
|
416 | 1 | $request['SyncState'] = $syncState; |
|
417 | 1 | $request['ItemShape']['BaseShape'] = 'AllProperties'; |
|
418 | 1 | } |
|
419 | |||
420 | 2 | $request = array_replace_recursive($request, $options); |
|
427 | |||
428 | public function getServerTimezones($timezoneIDs = array(), $fullTimezoneData = false) |
||
443 | |||
444 | /** |
||
445 | * @param Type\ItemIdType $itemId |
||
446 | * @param $fromType |
||
447 | * @param $destinationType |
||
448 | * @param $mailbox |
||
449 | * |
||
450 | * @return Type\ItemIdType |
||
451 | */ |
||
452 | public function convertIdFormat(Type\ItemIdType $itemId, $fromType, $destinationType, $mailbox) |
||
469 | |||
470 | /** |
||
471 | * @param Type\FindItemParentType|Type\FindFolderParentType $result |
||
472 | * |
||
473 | * @return Type\FindItemParentType|Type\FindFolderParentType |
||
474 | */ |
||
475 | 2 | public function getNextPage($result) |
|
493 | |||
494 | /** |
||
495 | * @param Type\FolderIdType $folderId |
||
496 | * @param string $deleteType |
||
497 | * @param bool $deleteSubFolders |
||
498 | * @param array $options |
||
499 | * @return EmptyFolderResponseType |
||
500 | */ |
||
501 | public function emptyFolder( |
||
517 | } |
||
518 |