Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 28 | class GoogleDrive implements Simulator |
||
| 29 | { |
||
| 30 | use Cleanable; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Google drive service. |
||
| 34 | * |
||
| 35 | * @var \Google_Service_Drive |
||
| 36 | */ |
||
| 37 | private $service; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Google json secret file. |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | private $secret; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Google json credentials file. |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | private $access; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Google drive parent folder id. |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | private $parent; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Upload chunk size. |
||
| 62 | * |
||
| 63 | * @var int |
||
| 64 | */ |
||
| 65 | private $chunkSize = 1 * 1024 * 1024; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * (non-PHPDoc) |
||
| 69 | * |
||
| 70 | * @see \phpbu\App\Backup\Sync::setup() |
||
| 71 | * @param array $config |
||
| 72 | * @throws \phpbu\App\Exception |
||
| 73 | */ |
||
| 74 | 9 | public function setup(array $config) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Make sure both google authentication files exist and determine absolute path to them. |
||
| 93 | * |
||
| 94 | * @param array $config |
||
| 95 | * @throws \phpbu\App\Backup\Sync\Exception |
||
| 96 | */ |
||
| 97 | 7 | private function setupAuthFiles(array $config) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Execute the Sync |
||
| 113 | * |
||
| 114 | * @see \phpbu\App\Backup\Sync::sync() |
||
| 115 | * @param \phpbu\App\Backup\Target $target |
||
| 116 | * @param \phpbu\App\Result $result |
||
| 117 | * @throws \phpbu\App\Backup\Sync\Exception |
||
| 118 | */ |
||
| 119 | 3 | public function sync(Target $target, Result $result) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Simulate the sync execution. |
||
| 153 | * |
||
| 154 | * @param \phpbu\App\Backup\Target $target |
||
| 155 | * @param \phpbu\App\Result $result |
||
| 156 | */ |
||
| 157 | 1 | View Code Duplication | public function simulate(Target $target, Result $result) |
| 158 | { |
||
| 159 | 1 | $result->debug('sync backup to google drive' . PHP_EOL); |
|
| 160 | |||
| 161 | 1 | $this->isSimulation = true; |
|
| 162 | 1 | $this->simulateRemoteCleanup($target, $result); |
|
| 163 | 1 | } |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Setup google api client and google drive service. |
||
| 167 | * |
||
| 168 | * @throws \Google_Exception |
||
| 169 | */ |
||
| 170 | protected function createDriveService() : GDriveService |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Create google api file. |
||
| 191 | * |
||
| 192 | * @param \phpbu\App\Backup\Target $target |
||
| 193 | * @return \Google_Service_Drive_DriveFile |
||
| 194 | */ |
||
| 195 | 2 | protected function createFile(Target $target) : GFile |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Create google api file deferred upload. |
||
| 206 | * |
||
| 207 | * @param \Google_Client $client |
||
| 208 | * @param \Psr\Http\Message\RequestInterface $request |
||
| 209 | * @param \phpbu\App\Backup\Target $target |
||
| 210 | * @return \Google_Http_MediaFileUpload |
||
| 211 | * @throws \phpbu\App\Exception |
||
| 212 | */ |
||
| 213 | protected function createUploadStream(GClient $client, RequestInterface $request, Target $target) : GStream |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Return google credentials. |
||
| 230 | * |
||
| 231 | * @return array |
||
| 232 | */ |
||
| 233 | private function getAccessToken() : array |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Update the access token in the google credentials file. |
||
| 240 | * |
||
| 241 | * @param array $accessToken |
||
| 242 | * @return void |
||
| 243 | */ |
||
| 244 | private function updateAccessToken(array $accessToken) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Creates collector for remote cleanup. |
||
| 251 | * |
||
| 252 | * @param \phpbu\App\Backup\Target $target |
||
| 253 | * @return \phpbu\App\Backup\Collector |
||
| 254 | * @throws \Google_Exception |
||
| 255 | */ |
||
| 256 | 1 | protected function createCollector(Target $target): Collector |
|
| 260 | } |
||
| 261 |