Complex classes like ExchangeWebServices 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 ExchangeWebServices, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
86 | class ExchangeWebServices |
||
87 | { |
||
88 | const VERSION_2007 = 'Exchange2007'; |
||
89 | |||
90 | const VERSION_2007_SP1 = 'Exchange2007_SP1'; |
||
91 | |||
92 | const VERSION_2010 = 'Exchange2010'; |
||
93 | |||
94 | const VERSION_2010_SP1 = 'Exchange2010_SP1'; |
||
95 | |||
96 | const VERSION_2010_SP2 = 'Exchange2010_SP2'; |
||
97 | |||
98 | const VERSION_2013 = 'Exchange2013'; |
||
99 | |||
100 | const VERSION_2013_SP1 = 'Exchange2013_SP1'; |
||
101 | |||
102 | const VERSION_2016 = 'Exchange2016'; |
||
103 | |||
104 | /** |
||
105 | * Password to use when connecting to the Exchange server. |
||
106 | * |
||
107 | * @var string |
||
108 | */ |
||
109 | protected $password = null; |
||
110 | |||
111 | /** |
||
112 | * Location of the Exchange server. |
||
113 | * |
||
114 | * @var string |
||
115 | */ |
||
116 | protected $server = null; |
||
117 | |||
118 | /** |
||
119 | * SOAP client used to make the request |
||
120 | * |
||
121 | * @var NTLMSoapClient |
||
122 | */ |
||
123 | protected $soap = null; |
||
124 | |||
125 | /** |
||
126 | * Username to use when connecting to the Exchange server. |
||
127 | * |
||
128 | * @var string |
||
129 | */ |
||
130 | protected $username = null; |
||
131 | |||
132 | /** |
||
133 | * @var EmailAddressType |
||
134 | */ |
||
135 | protected $primarySmtpMailbox = null; |
||
136 | |||
137 | /** |
||
138 | * @var Callable[] |
||
139 | */ |
||
140 | protected static $middlewareStack = false; |
||
141 | |||
142 | /** |
||
143 | * A setting to check whether or not responses should be drilled down before being |
||
144 | * returned. Setting this to false |
||
145 | * will return the raw responses without any filtering |
||
146 | * |
||
147 | * @var bool |
||
148 | */ |
||
149 | protected $drillDownResponses = true; |
||
150 | |||
151 | /** |
||
152 | * Miscrosoft Exchange version that we are going to connect to |
||
153 | * |
||
154 | * @var string |
||
155 | */ |
||
156 | protected $version = null; |
||
157 | |||
158 | protected $options = null; |
||
159 | |||
160 | /** |
||
161 | * The timezone for the client |
||
162 | * |
||
163 | * @var bool |
||
164 | */ |
||
165 | protected $timezone = false; |
||
166 | |||
167 | /** |
||
168 | * @return EmailAddressType |
||
169 | 28 | */ |
|
170 | public function getPrimarySmtpMailbox() |
||
171 | 28 | { |
|
172 | return $this->primarySmtpMailbox; |
||
173 | } |
||
174 | 1 | ||
175 | public function getPrimarySmtpEmailAddress() |
||
176 | 1 | { |
|
177 | 1 | if ($this->primarySmtpMailbox == null) { |
|
178 | return null; |
||
179 | } |
||
180 | 1 | ||
181 | return $this->primarySmtpMailbox->getEmailAddress(); |
||
182 | } |
||
183 | 2 | ||
184 | public function setPrimarySmtpEmailAddress($emailAddress) |
||
185 | 2 | { |
|
186 | 2 | $mailbox = new EmailAddressType(); |
|
187 | 2 | $mailbox->setEmailAddress($emailAddress); |
|
188 | $this->primarySmtpMailbox = $mailbox; |
||
189 | 2 | ||
190 | return $this; |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * @param boolean $timezone |
||
195 | */ |
||
196 | public function setTimezone($timezone) |
||
197 | { |
||
198 | $this->timezone = $timezone; |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * @return string |
||
203 | */ |
||
204 | public function getVersion() |
||
205 | { |
||
206 | return $this->version; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * @return string |
||
211 | */ |
||
212 | public function getServer() |
||
213 | { |
||
214 | return $this->server; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Constructor for the ExchangeWebServices class |
||
219 | * |
||
220 | * @param string $server |
||
221 | * @param string $username |
||
222 | * @param string $password |
||
223 | * @param array $options |
||
224 | 36 | */ |
|
225 | protected function __construct($server = null, $username = null, $password = null, $options = array()) |
||
226 | 36 | { |
|
227 | if ($server !== null) { |
||
228 | $this->createClient( |
||
229 | $server, |
||
230 | ExchangeWebServicesAuth::fromUsernameAndPassword($username, $password), |
||
231 | $options |
||
232 | ); |
||
233 | } |
||
234 | 36 | ||
235 | 36 | $this->buildMiddlewareStack(); |
|
236 | } |
||
237 | 35 | ||
238 | public static function fromUsernameAndPassword($server, $username, $password, $options) |
||
239 | 35 | { |
|
240 | 35 | $self = new self(); |
|
241 | 35 | $self->createClient($server, ExchangeWebServicesAuth::fromUsernameAndPassword($username, $password), $options); |
|
242 | $self->options = $options; |
||
243 | 35 | ||
244 | return $self; |
||
245 | } |
||
246 | 1 | ||
247 | public static function fromCallbackToken($server, $token, $options) |
||
248 | 1 | { |
|
249 | 1 | $self = new self(); |
|
250 | 1 | $self->createClient($server, ExchangeWebServicesAuth::fromCallbackToken($token), $options); |
|
251 | $self->options = $options; |
||
252 | 1 | ||
253 | return $self; |
||
254 | } |
||
255 | 36 | ||
256 | protected function createClient($server, $auth, $options) |
||
257 | 36 | { |
|
258 | $location = 'https://' . $this->cleanServerUrl($server) . '/EWS/Exchange.asmx'; |
||
259 | 36 | ||
260 | 36 | $options = array_replace_recursive([ |
|
261 | 36 | 'version' => self::VERSION_2007, |
|
262 | 36 | 'trace' => 1, |
|
263 | 36 | 'exceptions' => true, |
|
264 | 'classmap' => ClassMap::getClassMap(), |
||
265 | 36 | 'drillDownResponses' => true |
|
266 | ], $options); |
||
267 | 36 | ||
268 | 36 | $this->server = $server; |
|
269 | $this->version = $options['version']; |
||
270 | 36 | ||
271 | 36 | $backup = libxml_disable_entity_loader(false); |
|
272 | 36 | $this->soap = new NTLMSoapClient( |
|
273 | 36 | $location, |
|
274 | 36 | $auth, |
|
275 | dirname(__FILE__) . '/../../Resources/wsdl/services.wsdl', |
||
276 | 36 | $options |
|
277 | 36 | ); |
|
278 | libxml_disable_entity_loader($backup); |
||
279 | 36 | ||
280 | 1 | if (isset($options['primarySmtpEmailAddress'])) { |
|
281 | 1 | $this->setPrimarySmtpEmailAddress($options['primarySmtpEmailAddress']); |
|
282 | } |
||
283 | 36 | ||
284 | 1 | if (isset($options['impersonation'])) { |
|
285 | 1 | $this->setPrimarySmtpEmailAddress($options['impersonation']); |
|
286 | } |
||
287 | 36 | ||
288 | 36 | $this->drillDownResponses = $options['drillDownResponses']; |
|
289 | } |
||
290 | |||
291 | /** |
||
292 | * @codeCoverageIgnore |
||
293 | * |
||
294 | * @param $name |
||
295 | * @param $arguments |
||
296 | * @return Type |
||
297 | * @throws \garethp\ews\API\Exception |
||
298 | */ |
||
299 | public function __call($name, $arguments) |
||
306 | |||
307 | /** |
||
308 | * Returns the SOAP Client that may be used to make calls against the server |
||
309 | * |
||
310 | * @return NTLMSoapClient |
||
311 | 32 | */ |
|
312 | public function getClient() |
||
313 | 32 | { |
|
314 | return $this->soap; |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Cleans the server URL for usage |
||
319 | * |
||
320 | * @param $server |
||
321 | * @return string |
||
322 | 43 | */ |
|
323 | public function cleanServerUrl($server) |
||
344 | |||
345 | /** |
||
346 | * Process a response to verify that it succeeded and take the appropriate |
||
347 | * action |
||
348 | * |
||
349 | * @param \garethp\ews\API\Message\BaseResponseMessageType $response |
||
350 | * @return Type[] |
||
351 | * @throws \garethp\ews\API\Exception |
||
352 | 30 | */ |
|
353 | protected function processResponse($response) |
||
372 | |||
373 | /** |
||
374 | * @param $response |
||
375 | * @return array |
||
376 | * @throws \garethp\ews\API\Exception |
||
377 | 30 | */ |
|
378 | public static function drillDownResponseLevels($response) |
||
399 | |||
400 | /** |
||
401 | * @param $response |
||
402 | * @return array |
||
403 | * @throws ExchangeException |
||
404 | 30 | */ |
|
405 | protected static function getItemsFromResponse($response) |
||
427 | |||
428 | /** |
||
429 | * @param Message\BaseResponseMessageType $response |
||
430 | * @param $code |
||
431 | * @throws ExchangeException |
||
432 | * @throws NoResponseReturnedException |
||
433 | * @throws ServiceUnavailableException |
||
434 | * @throws UnauthorizedException |
||
435 | 30 | */ |
|
436 | protected function handleNonSuccessfulResponses($response, $code) |
||
458 | |||
459 | 1 | protected function buildMiddlewareStack() |
|
482 | |||
483 | 30 | /** |
|
484 | * @param array $middlewareStack |
||
485 | 30 | * @param MiddlewareRequest $request |
|
486 | 30 | * @return MiddlewareResponse |
|
487 | */ |
||
488 | protected function executeMiddlewareStack(array $middlewareStack, MiddlewareRequest $request) |
||
514 | } |
||
515 |