Complex classes like Client 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 Client, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class Client |
||
24 | { |
||
25 | const CACHE_SUBDIRECTORY = 'BingAdsApiBundle'; |
||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | private $config = []; |
||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $fileName; |
||
34 | |||
35 | /** |
||
36 | * @var ClientProxy |
||
37 | */ |
||
38 | private $proxy; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | public $report; |
||
44 | |||
45 | /** |
||
46 | * @var string[] |
||
47 | */ |
||
48 | private $files; |
||
49 | |||
50 | /** |
||
51 | * @var OauthTokenService |
||
52 | */ |
||
53 | private $oauthTokenService; |
||
54 | |||
55 | /** |
||
56 | * @var ApiDetails |
||
57 | */ |
||
58 | private $apiDetails; |
||
59 | |||
60 | /** |
||
61 | * @var ClientProxy |
||
62 | */ |
||
63 | private $clientProxy; |
||
64 | |||
65 | /** |
||
66 | * @var File |
||
67 | */ |
||
68 | private $fileHelper; |
||
69 | |||
70 | /** |
||
71 | * @var Csv |
||
72 | */ |
||
73 | private $csvHelper; |
||
74 | |||
75 | /** |
||
76 | * @var Time |
||
77 | */ |
||
78 | private $timeHelper; |
||
79 | |||
80 | /** |
||
81 | * @var Filesystem |
||
82 | */ |
||
83 | private $filesystem; |
||
84 | |||
85 | /** |
||
86 | * @param OauthTokenService $oauthTokenService |
||
87 | * @param ApiDetails $apiDetails |
||
88 | * @param ClientProxy $clientProxy |
||
89 | 25 | * @param File $file |
|
90 | 1 | * @param Csv $csv |
|
91 | 25 | * @param Time $timeHelper |
|
92 | 25 | * @param Filesystem $fileSystem |
|
93 | 25 | */ |
|
94 | 25 | public function __construct(OauthTokenService $oauthTokenService, ApiDetails $apiDetails, ClientProxy $clientProxy, File $file, Csv $csv, Time $timeHelper, Filesystem $fileSystem) |
|
113 | |||
114 | public function setApiDetails(ApiDetails $apiDetails) |
||
118 | 24 | ||
119 | /** |
||
120 | 24 | * Sets the configuration |
|
121 | 24 | * |
|
122 | 24 | * @param $config |
|
123 | 24 | */ |
|
124 | public function setConfig($config) |
||
131 | |||
132 | public function getRefreshToken() |
||
136 | |||
137 | /** |
||
138 | 24 | * @param string $reportName |
|
139 | * @param array $columns |
||
140 | 24 | * @param $timePeriod |
|
141 | 24 | * @param null|string $fileLocation |
|
142 | 24 | */ |
|
143 | 24 | public function getReport($reportName, array $columns, $timePeriod = ReportTimePeriod::LastWeek, $fileLocation) |
|
159 | |||
160 | 1 | /** |
|
161 | * @return AccessToken |
||
162 | 2 | */ |
|
163 | protected function getOauthToken() |
||
172 | 24 | ||
173 | 24 | /** |
|
174 | * @param string $wsdl |
||
175 | * @param string $accessToken |
||
176 | */ |
||
177 | private function setProxy($wsdl, $accessToken) |
||
181 | 24 | ||
182 | 1 | /** |
|
183 | 1 | * @return string |
|
184 | */ |
||
185 | 24 | private function getCacheDir() |
|
191 | |||
192 | /** |
||
193 | * @param ReportRequest $reportRequest |
||
194 | * @param string $name |
||
195 | * @param string $downloadFile |
||
196 | * @param ReportInterface $report |
||
197 | * |
||
198 | 24 | * @throws Exception |
|
199 | * |
||
200 | 24 | * @return array|string |
|
201 | 6 | */ |
|
202 | 3 | private function getFilesFromReportRequest(ReportRequest $reportRequest, $name, $downloadFile, ReportInterface $report) |
|
216 | |||
217 | /** |
||
218 | * SubmitGenerateReport helper method calls the corresponding Bing Ads service operation |
||
219 | * to request the report identifier. The identifier is used to check report generation status |
||
220 | * before downloading the report. |
||
221 | * |
||
222 | 24 | * @param mixed $report |
|
223 | * @param string $name |
||
224 | 24 | * |
|
225 | * @return string ReportRequestId |
||
226 | 24 | */ |
|
227 | private function submitGenerateReport($report, $name) |
||
238 | |||
239 | /** |
||
240 | 24 | * @param mixed $report |
|
241 | * @param string $name |
||
242 | 24 | * |
|
243 | * @return SoapVar |
||
244 | 24 | */ |
|
245 | private function getReportRequest($report, $name) |
||
251 | |||
252 | /** |
||
253 | * Check if the report is ready for download |
||
254 | * if not wait 10 sec and retry. (up to 6,5 hour) |
||
255 | * After 30 tries check every 1 minute |
||
256 | * After 34 tries check every 5 minutes |
||
257 | * After 39 tries check every 15 minutes |
||
258 | * After 43 tries check every 30 minutes |
||
259 | * |
||
260 | * @param string $reportRequestId |
||
261 | * @param int $count |
||
262 | * @param int $maxCount |
||
263 | * @param int $sleep |
||
264 | * @param bool $incrementTime |
||
265 | * |
||
266 | 6 | * @throws Exceptions\ReportRequestErrorException |
|
267 | * @throws Exceptions\RequestTimeoutException |
||
268 | 6 | * |
|
269 | 1 | * @return string |
|
270 | */ |
||
271 | private function waitForStatus($reportRequestId, $count = 1, $maxCount = 48, $sleep = 10, $incrementTime = true) |
||
306 | |||
307 | /** |
||
308 | * Check the status of the report request. The guidance of how often to poll |
||
309 | * for status is from every five to 15 minutes depending on the amount |
||
310 | * of data being requested. For smaller reports, you can poll every couple |
||
311 | * of minutes. You should stop polling and try again later if the request |
||
312 | * is taking longer than an hour. |
||
313 | 6 | * |
|
314 | * @param string $reportRequestId |
||
315 | 6 | * |
|
316 | 6 | * @return string ReportRequestStatus |
|
317 | */ |
||
318 | 6 | private function pollGenerateReport($reportRequestId) |
|
328 | |||
329 | 3 | /** |
|
330 | * @param array|null $files |
||
331 | 3 | * |
|
332 | 3 | * @return string[] |
|
333 | 3 | */ |
|
334 | 3 | private function fixFile(ReportInterface $report, array $files) |
|
348 | |||
349 | /** |
||
350 | * @param array $files |
||
351 | * @param string $target |
||
352 | 1 | */ |
|
353 | private function moveFirstFile(array $files, $target) |
||
358 | |||
359 | /** |
||
360 | * Clear Bundle Cache directory |
||
361 | * |
||
362 | * @param bool $allFiles delete all files in bundles cache, if false deletes only extracted files ($this->files) |
||
363 | * |
||
364 | * @return self |
||
365 | * |
||
366 | * @codeCoverageIgnore |
||
367 | */ |
||
368 | public function clearCache($allFiles = false) |
||
385 | |||
386 | /** |
||
387 | * @param SoapFault $e |
||
388 | * |
||
389 | * @throws Exceptions\SoapInternalErrorException |
||
390 | * @throws Exceptions\SoapInvalidCredentialsException |
||
391 | * @throws Exceptions\SoapNoCompleteDataAvailableException |
||
392 | * @throws Exceptions\SoapReportingServiceInvalidReportIdException |
||
393 | * @throws Exceptions\SoapUnknownErrorException |
||
394 | * @throws Exceptions\SoapUserIsNotAuthorizedException |
||
395 | */ |
||
396 | private function parseSoapFault(SoapFault $e) |
||
427 | |||
428 | /** |
||
429 | * @param $reportName |
||
430 | * |
||
431 | * @throws InvalidReportNameException |
||
432 | */ |
||
433 | private function ensureValidReportName($reportName) |
||
439 | |||
440 | /** |
||
441 | * @return Filesystem |
||
442 | */ |
||
443 | private function getFileSystem() |
||
447 | |||
448 | private function createCacheDirIfNotExists() |
||
456 | } |
||
457 |
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.