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 | * @var Filesystem |
||
| 83 | */ |
||
| 84 | private $filesystem; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param OauthTokenService $oauthTokenService |
||
| 88 | * @param ApiDetails $apiDetails |
||
| 89 | 25 | * @param ClientProxy $clientProxy |
|
| 90 | 1 | * @param File $file |
|
| 91 | 25 | * @param Csv $csv |
|
| 92 | 25 | * @param Time $timeHelper |
|
| 93 | 25 | * @param Filesystem $fileSystem |
|
| 94 | 25 | */ |
|
| 95 | 25 | public function __construct(OauthTokenService $oauthTokenService, ApiDetails $apiDetails, ClientProxy $clientProxy, File $file, Csv $csv, Time $timeHelper, Filesystem $fileSystem) |
|
| 114 | |||
| 115 | public function setApiDetails(ApiDetails $apiDetails) |
||
| 119 | |||
| 120 | 24 | /** |
|
| 121 | 24 | * Sets the configuration |
|
| 122 | 24 | * |
|
| 123 | 24 | * @param $config |
|
| 124 | */ |
||
| 125 | 1 | public function setConfig($config) |
|
| 132 | |||
| 133 | public function getRefreshToken() |
||
| 137 | |||
| 138 | 24 | /** |
|
| 139 | * @param string $reportName |
||
| 140 | 24 | * @param array $columns |
|
| 141 | 24 | * @param $timePeriod |
|
| 142 | 24 | * @param null|string $fileLocation |
|
| 143 | 24 | */ |
|
| 144 | 24 | public function getReport($reportName, array $columns, $timePeriod = ReportTimePeriod::LastWeek, $fileLocation) |
|
| 160 | 1 | ||
| 161 | /** |
||
| 162 | 2 | * @return AccessToken |
|
| 163 | */ |
||
| 164 | protected function getOauthToken() |
||
| 173 | 24 | ||
| 174 | /** |
||
| 175 | * @param string $wsdl |
||
| 176 | * @param string $accessToken |
||
| 177 | */ |
||
| 178 | 24 | private function setProxy($wsdl, $accessToken) |
|
| 182 | 1 | ||
| 183 | 1 | /** |
|
| 184 | * @return string |
||
| 185 | 24 | */ |
|
| 186 | private function getCacheDir() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param ReportRequest $reportRequest |
||
| 195 | * @param string $name |
||
| 196 | * @param string $downloadFile |
||
| 197 | * @param ReportInterface $report |
||
| 198 | 24 | * |
|
| 199 | * @throws Exception |
||
| 200 | 24 | * |
|
| 201 | 6 | * @return array|string |
|
| 202 | 3 | */ |
|
| 203 | 3 | private function getFilesFromReportRequest(ReportRequest $reportRequest, $name, $downloadFile, ReportInterface $report) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * SubmitGenerateReport helper method calls the corresponding Bing Ads service operation |
||
| 221 | * to request the report identifier. The identifier is used to check report generation status |
||
| 222 | 24 | * before downloading the report. |
|
| 223 | * |
||
| 224 | 24 | * @param mixed $report |
|
| 225 | * @param string $name |
||
| 226 | 24 | * |
|
| 227 | * @return string ReportRequestId |
||
| 228 | 24 | */ |
|
| 229 | 18 | private function submitGenerateReport($report, $name) |
|
| 240 | 24 | ||
| 241 | /** |
||
| 242 | 24 | * @param mixed $report |
|
| 243 | * @param string $name |
||
| 244 | 24 | * |
|
| 245 | * @return SoapVar |
||
| 246 | */ |
||
| 247 | private function getReportRequest($report, $name) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Check if the report is ready for download |
||
| 256 | * if not wait 10 sec and retry. (up to 6,5 hour) |
||
| 257 | * After 30 tries check every 1 minute |
||
| 258 | * After 34 tries check every 5 minutes |
||
| 259 | * After 39 tries check every 15 minutes |
||
| 260 | * After 43 tries check every 30 minutes |
||
| 261 | * |
||
| 262 | * @param string $reportRequestId |
||
| 263 | * @param int $count |
||
| 264 | * @param int $maxCount |
||
| 265 | * @param int $sleep |
||
| 266 | 6 | * @param bool $incrementTime |
|
| 267 | * |
||
| 268 | 6 | * @throws Exceptions\ReportRequestErrorException |
|
| 269 | 1 | * @throws Exceptions\RequestTimeoutException |
|
| 270 | * |
||
| 271 | * @return string |
||
| 272 | 6 | */ |
|
| 273 | 5 | private function waitForStatus($reportRequestId, $count = 1, $maxCount = 48, $sleep = 10, $incrementTime = true) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Check the status of the report request. The guidance of how often to poll |
||
| 311 | * for status is from every five to 15 minutes depending on the amount |
||
| 312 | * of data being requested. For smaller reports, you can poll every couple |
||
| 313 | 6 | * of minutes. You should stop polling and try again later if the request |
|
| 314 | * is taking longer than an hour. |
||
| 315 | 6 | * |
|
| 316 | 6 | * @param string $reportRequestId |
|
| 317 | * |
||
| 318 | 6 | * @return string ReportRequestStatus |
|
| 319 | 1 | */ |
|
| 320 | 1 | private function pollGenerateReport($reportRequestId) |
|
| 330 | |||
| 331 | 3 | /** |
|
| 332 | 3 | * @param array|null $files |
|
| 333 | 3 | * |
|
| 334 | 3 | * @return string[] |
|
| 335 | 3 | */ |
|
| 336 | 3 | private function fixFile(ReportInterface $report, array $files) |
|
| 350 | |||
| 351 | /** |
||
| 352 | 1 | * @param string[] $files |
|
| 353 | * @param string $target |
||
| 354 | 1 | */ |
|
| 355 | 1 | private function moveFirstFile($files, $target) |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Clear Bundle Cache directory |
||
| 363 | * |
||
| 364 | * @param bool $allFiles delete all files in bundles cache, if false deletes only extracted files ($this->files) |
||
| 365 | * |
||
| 366 | * @return self |
||
| 367 | * |
||
| 368 | * @codeCoverageIgnore |
||
| 369 | */ |
||
| 370 | public function clearCache($allFiles = false) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @param SoapFault $e |
||
| 390 | * |
||
| 391 | * @throws Exceptions\SoapInternalErrorException |
||
| 392 | * @throws Exceptions\SoapInvalidCredentialsException |
||
| 393 | * @throws Exceptions\SoapNoCompleteDataAvailableException |
||
| 394 | * @throws Exceptions\SoapReportingServiceInvalidReportIdException |
||
| 395 | * @throws Exceptions\SoapUnknownErrorException |
||
| 396 | * @throws Exceptions\SoapUserIsNotAuthorizedException |
||
| 397 | 19 | */ |
|
| 398 | private function parseSoapFault(SoapFault $e) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @param $reportName |
||
| 432 | * |
||
| 433 | * @throws InvalidReportNameException |
||
| 434 | */ |
||
| 435 | private function ensureValidReportName($reportName) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @return Filesystem |
||
| 444 | */ |
||
| 445 | private function getFileSystem() |
||
| 449 | |||
| 450 | private function createCacheDirIfNotExists() |
||
| 458 | } |
||
| 459 |
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.