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) |
|
| 161 | |||
| 162 | 2 | /** |
|
| 163 | * @return AccessToken |
||
| 164 | */ |
||
| 165 | protected function getOauthToken() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param string $wsdl |
||
| 177 | * @param string $accessToken |
||
| 178 | 24 | */ |
|
| 179 | private function setProxy($wsdl, $accessToken) |
||
| 183 | 1 | ||
| 184 | /** |
||
| 185 | 24 | * @return string |
|
| 186 | */ |
||
| 187 | private function getCacheDir() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param ReportRequest $reportRequest |
||
| 196 | * @param string $name |
||
| 197 | * @param string $downloadFile |
||
| 198 | 24 | * @param ReportInterface $report |
|
| 199 | * |
||
| 200 | 24 | * @throws Exception |
|
| 201 | 6 | * |
|
| 202 | 3 | * @return array|string |
|
| 203 | 3 | */ |
|
| 204 | 3 | private function getFilesFromReportRequest(ReportRequest $reportRequest, $name, $downloadFile, ReportInterface $report) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * SubmitGenerateReport helper method calls the corresponding Bing Ads service operation |
||
| 222 | 24 | * to request the report identifier. The identifier is used to check report generation status |
|
| 223 | * before downloading the report. |
||
| 224 | 24 | * |
|
| 225 | * @param mixed $report |
||
| 226 | 24 | * @param string $name |
|
| 227 | * |
||
| 228 | 24 | * @return string ReportRequestId |
|
| 229 | 18 | */ |
|
| 230 | 18 | private function submitGenerateReport($report, $name) |
|
| 241 | |||
| 242 | 24 | /** |
|
| 243 | * @param mixed $report |
||
| 244 | 24 | * @param string $name |
|
| 245 | * |
||
| 246 | * @return SoapVar |
||
| 247 | */ |
||
| 248 | private function getReportRequest($report, $name) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Check if the report is ready for download |
||
| 257 | * if not wait 10 sec and retry. (up to 6,5 hour) |
||
| 258 | * After 30 tries check every 1 minute |
||
| 259 | * After 34 tries check every 5 minutes |
||
| 260 | * After 39 tries check every 15 minutes |
||
| 261 | * After 43 tries check every 30 minutes |
||
| 262 | * |
||
| 263 | * @param string $reportRequestId |
||
| 264 | * @param int $count |
||
| 265 | * @param int $maxCount |
||
| 266 | 6 | * @param int $sleep |
|
| 267 | * @param bool $incrementTime |
||
| 268 | 6 | * |
|
| 269 | 1 | * @throws Exceptions\ReportRequestErrorException |
|
| 270 | * @throws Exceptions\RequestTimeoutException |
||
| 271 | * |
||
| 272 | 6 | * @return string |
|
| 273 | 5 | */ |
|
| 274 | 1 | private function waitForStatus($reportRequestId, $count = 1, $maxCount = 48, $sleep = 10, $incrementTime = true) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Check the status of the report request. The guidance of how often to poll |
||
| 312 | * for status is from every five to 15 minutes depending on the amount |
||
| 313 | 6 | * of data being requested. For smaller reports, you can poll every couple |
|
| 314 | * of minutes. You should stop polling and try again later if the request |
||
| 315 | 6 | * is taking longer than an hour. |
|
| 316 | 6 | * |
|
| 317 | * @param string $reportRequestId |
||
| 318 | 6 | * |
|
| 319 | 1 | * @return string ReportRequestStatus |
|
| 320 | 1 | */ |
|
| 321 | private function pollGenerateReport($reportRequestId) |
||
| 331 | 3 | ||
| 332 | 3 | /** |
|
| 333 | 3 | * @param array|null $files |
|
| 334 | 3 | * |
|
| 335 | 3 | * @return string[] |
|
| 336 | 3 | */ |
|
| 337 | 3 | private function fixFile(ReportInterface $report, array $files) |
|
| 351 | |||
| 352 | 1 | ||
| 353 | /** |
||
| 354 | 1 | * @param bool $allFiles delete all files in bundles cache, if false deletes only extracted files ($this->files) |
|
| 355 | 1 | * |
|
| 356 | * @return self |
||
| 357 | 1 | */ |
|
| 358 | public function clearCache($allFiles = false) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @param SoapFault $e |
||
| 371 | * |
||
| 372 | * @throws Exceptions\SoapInternalErrorException |
||
| 373 | * @throws Exceptions\SoapInvalidCredentialsException |
||
| 374 | * @throws Exceptions\SoapNoCompleteDataAvailableException |
||
| 375 | * @throws Exceptions\SoapReportingServiceInvalidReportIdException |
||
| 376 | * @throws Exceptions\SoapUnknownErrorException |
||
| 377 | * @throws Exceptions\SoapUserIsNotAuthorizedException |
||
| 378 | */ |
||
| 379 | private function parseSoapFault(SoapFault $e) |
||
| 410 | 19 | ||
| 411 | 19 | /** |
|
| 412 | 19 | * @param $reportName |
|
| 413 | 4 | * |
|
| 414 | 15 | * @throws InvalidReportNameException |
|
| 415 | 3 | */ |
|
| 416 | 12 | private function ensureValidReportName($reportName) |
|
| 422 | 3 | ||
| 423 | 3 | /** |
|
| 424 | 3 | * @return Filesystem |
|
| 425 | 3 | */ |
|
| 426 | private function getFileSystem() |
||
| 430 | |||
| 431 | private function createCacheDirIfNotExists() |
||
| 439 | } |
||
| 440 |
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.