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 |
||
| 21 | class Client |
||
| 22 | { |
||
| 23 | const CACHE_SUBDIRECTORY = 'BingAdsApiBundle'; |
||
| 24 | /** |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | private $config = []; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | private $fileName; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var ClientProxy |
||
| 36 | */ |
||
| 37 | private $proxy; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | public $report; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string[] |
||
| 46 | */ |
||
| 47 | private $files; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var OauthTokenService |
||
| 51 | */ |
||
| 52 | private $oauthTokenService; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var ApiDetails |
||
| 56 | */ |
||
| 57 | private $apiDetails; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var ClientProxy |
||
| 61 | */ |
||
| 62 | private $clientProxy; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var File |
||
| 66 | */ |
||
| 67 | private $fileHelper; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var Csv |
||
| 71 | */ |
||
| 72 | private $csvHelper; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var Time |
||
| 76 | */ |
||
| 77 | private $timeHelper; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param OauthTokenService $oauthTokenService |
||
| 81 | * @param ApiDetails $apiDetails |
||
| 82 | * @param ClientProxy $clientProxy |
||
| 83 | * @param File $file |
||
| 84 | * @param Csv $csv |
||
| 85 | * @param Time $timeHelper |
||
| 86 | */ |
||
| 87 | public function __construct(OauthTokenService $oauthTokenService, ApiDetails $apiDetails, ClientProxy $clientProxy, File $file, Csv $csv, Time $timeHelper) |
||
| 105 | |||
| 106 | 25 | public function setApiDetails(ApiDetails $apiDetails) |
|
| 110 | 1 | ||
| 111 | 1 | /** |
|
| 112 | * Sets the configuration |
||
| 113 | * |
||
| 114 | * @param $config |
||
| 115 | */ |
||
| 116 | public function setConfig($config) |
||
| 123 | 24 | ||
| 124 | public function getRefreshToken() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param string $reportName |
||
| 131 | * @param array $columns |
||
| 132 | * @param $timePeriod |
||
| 133 | * @param null|string $fileLocation |
||
| 134 | */ |
||
| 135 | public function getReport($reportName, array $columns, $timePeriod = ReportTimePeriod::LastWeek, $fileLocation = null) |
||
| 136 | { |
||
| 137 | $this->ensureValidReportName($reportName); |
||
| 138 | 24 | $oauthToken = $this->getOauthToken(); |
|
| 139 | $this->apiDetails->setRefreshToken($oauthToken->getRefreshToken()); |
||
| 140 | 24 | ||
| 141 | 24 | $report = $this->report[$reportName]; |
|
| 142 | 24 | $report->setTimePeriod($timePeriod); |
|
| 143 | 24 | $report->setColumns($columns); |
|
| 144 | 24 | $reportRequest = $report->getRequest(); |
|
| 145 | 24 | $this->setProxy($report::WSDL, $oauthToken->getAccessToken()); |
|
| 146 | $files = $this->getFilesFromReportRequest($reportRequest, $reportName, "{$this->getCacheDir()}/{$this->fileName}", $report); |
||
|
|
|||
| 147 | 24 | if ($fileLocation !== null) { |
|
| 148 | 24 | $this->fileHelper->moveFirstFile($files, $fileLocation); |
|
| 149 | } |
||
| 150 | 24 | $this->files = $files; |
|
| 151 | 24 | } |
|
| 152 | 24 | ||
| 153 | 24 | /** |
|
| 154 | 24 | * @return AccessToken |
|
| 155 | 24 | */ |
|
| 156 | protected function getOauthToken() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param string $wsdl |
||
| 168 | * @param string $accessToken |
||
| 169 | */ |
||
| 170 | 24 | private function setProxy($wsdl, $accessToken) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * @return string |
||
| 177 | */ |
||
| 178 | 24 | private function getCacheDir() |
|
| 184 | |||
| 185 | 24 | /** |
|
| 186 | * @param ReportRequest $reportRequest |
||
| 187 | * @param string $name |
||
| 188 | * @param string $downloadFile |
||
| 189 | * @param ReportInterface $report |
||
| 190 | * |
||
| 191 | * @throws Exception |
||
| 192 | * |
||
| 193 | * @return string[] |
||
| 194 | */ |
||
| 195 | private function getFilesFromReportRequest(ReportRequest $reportRequest, $name, $downloadFile, ReportInterface $report) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * SubmitGenerateReport helper method calls the corresponding Bing Ads service operation |
||
| 213 | * to request the report identifier. The identifier is used to check report generation status |
||
| 214 | * before downloading the report. |
||
| 215 | * |
||
| 216 | * @param mixed $report |
||
| 217 | * @param string $name |
||
| 218 | * |
||
| 219 | * @return string ReportRequestId |
||
| 220 | */ |
||
| 221 | private function submitGenerateReport($report, $name) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param mixed $report |
||
| 235 | * @param string $name |
||
| 236 | * |
||
| 237 | * @return SoapVar |
||
| 238 | */ |
||
| 239 | private function getReportRequest($report, $name) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Check if the report is ready for download |
||
| 248 | * if not wait 10 sec and retry. (up to 6,5 hour) |
||
| 249 | * After 30 tries check every 1 minute |
||
| 250 | * After 34 tries check every 5 minutes |
||
| 251 | * After 39 tries check every 15 minutes |
||
| 252 | * After 43 tries check every 30 minutes |
||
| 253 | * |
||
| 254 | * @param string $reportRequestId |
||
| 255 | * @param int $count |
||
| 256 | * @param int $maxCount |
||
| 257 | * @param int $sleep |
||
| 258 | * @param bool $incrementTime |
||
| 259 | * |
||
| 260 | * @throws Exceptions\ReportRequestErrorException |
||
| 261 | * @throws Exceptions\RequestTimeoutException |
||
| 262 | * |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | private function waitForStatus($reportRequestId, $count = 1, $maxCount = 48, $sleep = 10, $incrementTime = true) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Check the status of the report request. The guidance of how often to poll |
||
| 303 | * for status is from every five to 15 minutes depending on the amount |
||
| 304 | * of data being requested. For smaller reports, you can poll every couple |
||
| 305 | * of minutes. You should stop polling and try again later if the request |
||
| 306 | * is taking longer than an hour. |
||
| 307 | * |
||
| 308 | * @param string $reportRequestId |
||
| 309 | * |
||
| 310 | * @return string ReportRequestStatus |
||
| 311 | */ |
||
| 312 | private function pollGenerateReport($reportRequestId) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @param array|null $files |
||
| 325 | * |
||
| 326 | * @return string[] |
||
| 327 | */ |
||
| 328 | private function fixFile(ReportInterface $report, array $files) |
||
| 342 | 3 | ||
| 343 | |||
| 344 | /** |
||
| 345 | * @param bool $allFiles delete all files in bundles cache, if false deletes only extracted files ($this->files) |
||
| 346 | * |
||
| 347 | * @return self |
||
| 348 | */ |
||
| 349 | public function clearCache($allFiles = false) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @param SoapFault $e |
||
| 362 | * |
||
| 363 | * @throws Exceptions\SoapInternalErrorException |
||
| 364 | * @throws Exceptions\SoapInvalidCredentialsException |
||
| 365 | * @throws Exceptions\SoapNoCompleteDataAvailableException |
||
| 366 | * @throws Exceptions\SoapReportingServiceInvalidReportIdException |
||
| 367 | * @throws Exceptions\SoapUnknownErrorException |
||
| 368 | * @throws Exceptions\SoapUserIsNotAuthorizedException |
||
| 369 | */ |
||
| 370 | private function parseSoapFault(SoapFault $e) |
||
| 401 | 7 | ||
| 402 | 19 | /** |
|
| 403 | 12 | * @param $reportName |
|
| 404 | 6 | * |
|
| 405 | 12 | * @throws InvalidReportNameException |
|
| 406 | 6 | */ |
|
| 407 | 6 | private function ensureValidReportName($reportName) |
|
| 413 | } |
||
| 414 |
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.