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