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 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | private $fileName; |
||
35 | |||
36 | /** |
||
37 | * @var ClientProxy |
||
38 | */ |
||
39 | private $proxy; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | public $report; |
||
45 | |||
46 | /** |
||
47 | * @var string|string[] |
||
48 | */ |
||
49 | private $files; |
||
50 | |||
51 | /** |
||
52 | * @var OauthTokenService |
||
53 | */ |
||
54 | private $oauthTokenService; |
||
55 | |||
56 | /** |
||
57 | * @var ApiDetails |
||
58 | */ |
||
59 | private $apiDetails; |
||
60 | |||
61 | /** |
||
62 | * @var ClientProxy |
||
63 | */ |
||
64 | private $clientProxy; |
||
65 | |||
66 | /** |
||
67 | * @var File |
||
68 | */ |
||
69 | private $fileHelper; |
||
70 | |||
71 | /** |
||
72 | * @var Csv |
||
73 | */ |
||
74 | private $csvHelper; |
||
75 | |||
76 | /** |
||
77 | * @var Time |
||
78 | */ |
||
79 | private $timeHelper; |
||
80 | |||
81 | /** |
||
82 | * @param OauthTokenService $oauthTokenService |
||
83 | * @param ApiDetails $apiDetails |
||
84 | * @param ClientProxy $clientProxy |
||
85 | * @param File $file |
||
86 | * @param Csv $csv |
||
87 | * @param Time $timeHelper |
||
88 | */ |
||
89 | 25 | public function __construct(OauthTokenService $oauthTokenService, ApiDetails $apiDetails, ClientProxy $clientProxy, File $file, Csv $csv, Time $timeHelper) |
|
90 | 1 | { |
|
91 | 25 | $this->oauthTokenService = $oauthTokenService; |
|
92 | 25 | $this->apiDetails = $apiDetails; |
|
93 | 25 | $this->clientProxy = $clientProxy; |
|
94 | 25 | $this->fileHelper = $file; |
|
95 | 25 | $this->csvHelper = $csv; |
|
96 | 25 | $this->timeHelper = $timeHelper; |
|
97 | |||
98 | 25 | ini_set('soap.wsdl_cache_enabled', '0'); |
|
99 | 25 | ini_set('soap.wsdl_cache_ttl', '0'); |
|
100 | |||
101 | 25 | $this->fileName = 'report.zip'; |
|
102 | |||
103 | 25 | $this->report = [ |
|
104 | 25 | 'GeoLocationPerformanceReport' => new Report\GeoLocationPerformanceReport(), |
|
105 | ]; |
||
106 | 25 | } |
|
107 | |||
108 | 1 | public function setApiDetails(ApiDetails $apiDetails) |
|
112 | |||
113 | /** |
||
114 | * Sets the configuration |
||
115 | * |
||
116 | * @param $config |
||
117 | */ |
||
118 | 24 | public function setConfig($config) |
|
125 | 1 | ||
126 | public function getRefreshToken() |
||
130 | |||
131 | /** |
||
132 | * @param string $reportName |
||
133 | * @param array $columns |
||
134 | * @param $timePeriod |
||
135 | * @param null|string $fileLocation |
||
136 | */ |
||
137 | public function getReport($reportName, array $columns, $timePeriod = ReportTimePeriod::LastWeek, $fileLocation) |
||
154 | 24 | ||
155 | 24 | /** |
|
156 | * @return AccessToken |
||
157 | 3 | */ |
|
158 | 1 | protected function getOauthToken() |
|
167 | |||
168 | /** |
||
169 | * @param string $wsdl |
||
170 | 24 | * @param string $accessToken |
|
171 | */ |
||
172 | 24 | private function setProxy($wsdl, $accessToken) |
|
176 | |||
177 | /** |
||
178 | 24 | * @return string |
|
179 | */ |
||
180 | 24 | private function getCacheDir() |
|
186 | |||
187 | /** |
||
188 | * @param ReportRequest $reportRequest |
||
189 | * @param string $name |
||
190 | * @param string $downloadFile |
||
191 | * @param ReportInterface $report |
||
192 | * |
||
193 | * @throws Exception |
||
194 | * |
||
195 | * @return array|string |
||
196 | */ |
||
197 | private function getFilesFromReportRequest(ReportRequest $reportRequest, $name, $downloadFile, ReportInterface $report) |
||
212 | |||
213 | /** |
||
214 | * SubmitGenerateReport helper method calls the corresponding Bing Ads service operation |
||
215 | * to request the report identifier. The identifier is used to check report generation status |
||
216 | * before downloading the report. |
||
217 | * |
||
218 | * @param mixed $report |
||
219 | * @param string $name |
||
220 | * |
||
221 | * @return string ReportRequestId |
||
222 | 24 | */ |
|
223 | private function submitGenerateReport($report, $name) |
||
234 | |||
235 | /** |
||
236 | * @param mixed $report |
||
237 | * @param string $name |
||
238 | * |
||
239 | * @return SoapVar |
||
240 | 24 | */ |
|
241 | private function getReportRequest($report, $name) |
||
247 | |||
248 | /** |
||
249 | * Check if the report is ready for download |
||
250 | * if not wait 10 sec and retry. (up to 6,5 hour) |
||
251 | * After 30 tries check every 1 minute |
||
252 | * After 34 tries check every 5 minutes |
||
253 | * After 39 tries check every 15 minutes |
||
254 | * After 43 tries check every 30 minutes |
||
255 | * |
||
256 | * @param string $reportRequestId |
||
257 | * @param int $count |
||
258 | * @param int $maxCount |
||
259 | * @param int $sleep |
||
260 | * @param bool $incrementTime |
||
261 | * |
||
262 | * @throws Exceptions\ReportRequestErrorException |
||
263 | * @throws Exceptions\RequestTimeoutException |
||
264 | * |
||
265 | * @return string |
||
266 | 6 | */ |
|
267 | private function waitForStatus($reportRequestId, $count = 1, $maxCount = 48, $sleep = 10, $incrementTime = true) |
||
302 | |||
303 | /** |
||
304 | * Check the status of the report request. The guidance of how often to poll |
||
305 | * for status is from every five to 15 minutes depending on the amount |
||
306 | * of data being requested. For smaller reports, you can poll every couple |
||
307 | * of minutes. You should stop polling and try again later if the request |
||
308 | * is taking longer than an hour. |
||
309 | * |
||
310 | * @param string $reportRequestId |
||
311 | * |
||
312 | * @return string ReportRequestStatus |
||
313 | 6 | */ |
|
314 | private function pollGenerateReport($reportRequestId) |
||
324 | |||
325 | /** |
||
326 | * @param array|null $files |
||
327 | * |
||
328 | * @return string[] |
||
329 | 3 | */ |
|
330 | private function fixFile(ReportInterface $report, array $files) |
||
344 | |||
345 | |||
346 | /** |
||
347 | * @param bool $allFiles delete all files in bundles cache, if false deletes only extracted files ($this->files) |
||
348 | * |
||
349 | * @return self |
||
350 | */ |
||
351 | public function clearCache($allFiles = false) |
||
361 | |||
362 | /** |
||
363 | * @param SoapFault $e |
||
364 | * |
||
365 | * @throws Exceptions\SoapInternalErrorException |
||
366 | * @throws Exceptions\SoapInvalidCredentialsException |
||
367 | * @throws Exceptions\SoapNoCompleteDataAvailableException |
||
368 | * @throws Exceptions\SoapReportingServiceInvalidReportIdException |
||
369 | * @throws Exceptions\SoapUnknownErrorException |
||
370 | * @throws Exceptions\SoapUserIsNotAuthorizedException |
||
371 | */ |
||
372 | private function parseSoapFault(SoapFault $e) |
||
403 | 12 | ||
404 | 6 | /** |
|
405 | 12 | * @param $reportName |
|
406 | 6 | * |
|
407 | 6 | * @throws InvalidReportNameException |
|
408 | 12 | */ |
|
409 | 19 | private function ensureValidReportName($reportName) |
|
415 | } |
||
416 |
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.