| Total Complexity | 40 |
| Total Lines | 417 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Torrent 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.
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 Torrent, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Torrent extends AbstractModel |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Statuses. |
||
| 19 | * |
||
| 20 | * Field: status |
||
| 21 | */ |
||
| 22 | const STATUS_STOPPED = 0; |
||
| 23 | const STATUS_CHECK_WAIT = 1; |
||
| 24 | const STATUS_CHECK = 2; |
||
| 25 | const STATUS_DOWNLOAD_WAIT = 3; |
||
| 26 | const STATUS_DOWNLOAD = 4; |
||
| 27 | const STATUS_SEED_WAIT = 5; |
||
| 28 | const STATUS_SEED = 6; |
||
| 29 | const STATUS_ISOLATED = 7; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Seed Ratio Modes. |
||
| 33 | * |
||
| 34 | * Field: seedRatioMode |
||
| 35 | */ |
||
| 36 | const RATIO_USE_GLOBAL = 0; |
||
| 37 | const RATIO_USE_LOCAL = 1; |
||
| 38 | const RATIO_UNLIMITED = 2; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Errors. |
||
| 42 | * |
||
| 43 | * Field: error |
||
| 44 | */ |
||
| 45 | const ERROR_NONE = 0; |
||
| 46 | const ERROR_TRACKER_WARNING = 1; |
||
| 47 | const ERROR_TRACKER_ERROR = 2; |
||
| 48 | const ERROR_LOCAL_ERROR = 3; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Tracker Stats. |
||
| 52 | * |
||
| 53 | * Field: trackerStats->announceState |
||
| 54 | */ |
||
| 55 | const TRACKER_INACTIVE = 0; |
||
| 56 | const TRACKER_WAITING = 1; |
||
| 57 | const TRACKER_QUEUED = 2; |
||
| 58 | const TRACKER_ACTIVE = 3; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Common Fields. |
||
| 62 | * |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | public static $fields = [ |
||
| 66 | 'default' => [ |
||
| 67 | 'id', |
||
| 68 | 'eta', |
||
| 69 | 'name', |
||
| 70 | 'status', |
||
| 71 | 'isFinished', |
||
| 72 | 'files', |
||
| 73 | 'hashString', |
||
| 74 | 'downloadDir', |
||
| 75 | 'percentDone', |
||
| 76 | 'haveValid', |
||
| 77 | 'haveUnchecked', |
||
| 78 | 'totalSize', |
||
| 79 | 'leftUntilDone', |
||
| 80 | 'addedDate', |
||
| 81 | 'doneDate', |
||
| 82 | 'activityDate', |
||
| 83 | ], |
||
| 84 | 'stats' => [ |
||
| 85 | 'error', |
||
| 86 | 'errorString', |
||
| 87 | 'eta', |
||
| 88 | 'isFinished', |
||
| 89 | 'isStalled', |
||
| 90 | 'leftUntilDone', |
||
| 91 | 'metadataPercentComplete', |
||
| 92 | 'peersConnected', |
||
| 93 | 'peersGettingFromUs', |
||
| 94 | 'peersSendingToUs', |
||
| 95 | 'percentDone', |
||
| 96 | 'queuePosition', |
||
| 97 | 'rateDownload', |
||
| 98 | 'rateUpload', |
||
| 99 | 'recheckProgress', |
||
| 100 | 'seedRatioMode', |
||
| 101 | 'seedRatioLimit', |
||
| 102 | 'sizeWhenDone', |
||
| 103 | 'status', |
||
| 104 | 'trackers', |
||
| 105 | 'downloadDir', |
||
| 106 | 'uploadedEver', |
||
| 107 | 'uploadRatio', |
||
| 108 | 'webseedsSendingToUs', |
||
| 109 | ], |
||
| 110 | 'statsExtra' => [ |
||
| 111 | 'activityDate', |
||
| 112 | 'corruptEver', |
||
| 113 | 'desiredAvailable', |
||
| 114 | 'downloadedEver', |
||
| 115 | 'fileStats', |
||
| 116 | 'haveUnchecked', |
||
| 117 | 'haveValid', |
||
| 118 | 'peers', |
||
| 119 | 'startDate', |
||
| 120 | 'trackerStats', |
||
| 121 | ], |
||
| 122 | 'infoExtra' => [ |
||
| 123 | 'comment', |
||
| 124 | 'creator', |
||
| 125 | 'dateCreated', |
||
| 126 | 'files', |
||
| 127 | 'hashString', |
||
| 128 | 'isPrivate', |
||
| 129 | 'pieceCount', |
||
| 130 | 'pieceSize', |
||
| 131 | ], |
||
| 132 | ]; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * The attributes that should be cast to native and other supported types. |
||
| 136 | * |
||
| 137 | * Casts only when formatting is enabled. |
||
| 138 | * |
||
| 139 | * @var array |
||
| 140 | */ |
||
| 141 | protected $casts = [ |
||
| 142 | 'doneDate' => 'datetime', |
||
| 143 | 'startDate' => 'datetime', |
||
| 144 | 'activityDate' => 'datetime', |
||
| 145 | 'addedDate' => 'datetime', |
||
| 146 | 'dateCreated' => 'datetime', |
||
| 147 | 'eta' => 'interval', |
||
| 148 | 'haveValid' => 'size', |
||
| 149 | 'haveUnchecked' => 'size', |
||
| 150 | 'totalDone' => 'size', // Custom |
||
| 151 | 'leftUntilDone' => 'size', |
||
| 152 | 'totalSize' => 'size', |
||
| 153 | 'sizeWhenDone' => 'size', |
||
| 154 | 'uploadedEver' => 'size', |
||
| 155 | 'rateDownload' => 'datarate', |
||
| 156 | 'rateUpload' => 'datarate', |
||
| 157 | ]; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Get Name. |
||
| 161 | * |
||
| 162 | * @return mixed |
||
| 163 | */ |
||
| 164 | public function getName() |
||
| 165 | { |
||
| 166 | return $this->get('name', 'Unknown'); |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Get Percent Done. |
||
| 171 | * |
||
| 172 | * @param bool $format |
||
| 173 | * |
||
| 174 | * @return int |
||
| 175 | */ |
||
| 176 | public function getPercentDone($format = false): int |
||
| 177 | { |
||
| 178 | $percentDone = $this->get('percentDone', 0); |
||
| 179 | |||
| 180 | return $format ? $percentDone * 100 : $percentDone; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Get Percent Done String. |
||
| 185 | * |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | public function getPercentDoneString(): string |
||
| 189 | { |
||
| 190 | return $this->getPercentDone(true) . '%'; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Get Metadata Percent Complete. |
||
| 195 | * |
||
| 196 | * @param bool $format |
||
| 197 | * |
||
| 198 | * @return int |
||
| 199 | */ |
||
| 200 | public function getMetadataPercentComplete($format = false): int |
||
| 201 | { |
||
| 202 | $percent = $this->get('metadataPercentComplete', 0); |
||
| 203 | |||
| 204 | return $format ? $percent * 100 : $percent; |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Get Recheck Progress Percent. |
||
| 209 | * |
||
| 210 | * @param bool $format |
||
| 211 | * |
||
| 212 | * @return int |
||
| 213 | */ |
||
| 214 | public function getRecheckProgress($format = false): int |
||
| 215 | { |
||
| 216 | $percent = $this->get('recheckProgress', 0); |
||
| 217 | |||
| 218 | return $format ? $percent * 100 : $percent; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Get Total Done. |
||
| 223 | * |
||
| 224 | * @param null|bool $castingEnabled |
||
| 225 | * |
||
| 226 | * @return mixed |
||
| 227 | */ |
||
| 228 | public function getTotalDone($castingEnabled = null) |
||
| 229 | { |
||
| 230 | $value = $this->getHaveValid(false) + $this->getHaveUnchecked(false); |
||
| 231 | |||
| 232 | return $this->castAttribute('totalDone', $value, $castingEnabled ?? $this->castingEnabled); |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Get Upload Speed. |
||
| 237 | * |
||
| 238 | * @param null|bool $castingEnabled |
||
| 239 | * |
||
| 240 | * @return mixed |
||
| 241 | */ |
||
| 242 | public function getUploadSpeed($castingEnabled = null) |
||
| 243 | { |
||
| 244 | return $this->get('rateUpload', 0, $castingEnabled); |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Get Download Speed. |
||
| 249 | * |
||
| 250 | * @param null|bool $castingEnabled |
||
| 251 | * |
||
| 252 | * @return mixed |
||
| 253 | */ |
||
| 254 | public function getDownloadSpeed($castingEnabled = null) |
||
| 255 | { |
||
| 256 | return $this->get('rateDownload', 0, $castingEnabled); |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Get File Count. |
||
| 261 | * |
||
| 262 | * @return mixed |
||
| 263 | */ |
||
| 264 | public function getFileCount() |
||
| 265 | { |
||
| 266 | return count($this->get('files', 0)); |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Get a File by ID. |
||
| 271 | * |
||
| 272 | * @param int $id |
||
| 273 | * |
||
| 274 | * @return mixed |
||
| 275 | */ |
||
| 276 | public function getFile(int $id) |
||
| 277 | { |
||
| 278 | return data_get($this->items, "files.$id"); |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Check if status is stopped. |
||
| 283 | * |
||
| 284 | * @return bool |
||
| 285 | */ |
||
| 286 | public function isStopped(): bool |
||
| 287 | { |
||
| 288 | return $this->isStatus(static::STATUS_STOPPED); |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Check if status is checking. |
||
| 293 | * |
||
| 294 | * @return bool |
||
| 295 | */ |
||
| 296 | public function isChecking(): bool |
||
| 297 | { |
||
| 298 | return $this->isStatus(static::STATUS_CHECK); |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Check if status is downloading. |
||
| 303 | * |
||
| 304 | * @return bool |
||
| 305 | */ |
||
| 306 | public function isDownloading(): bool |
||
| 307 | { |
||
| 308 | return $this->isStatus(static::STATUS_DOWNLOAD); |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Check if status is queued. |
||
| 313 | * |
||
| 314 | * @return bool |
||
| 315 | */ |
||
| 316 | public function isQueued(): bool |
||
| 317 | { |
||
| 318 | return $this->isStatus(static::STATUS_DOWNLOAD_WAIT) || $this->isStatus(static::STATUS_SEED_WAIT); |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Check if status is seeding. |
||
| 323 | * |
||
| 324 | * @return bool |
||
| 325 | */ |
||
| 326 | public function isSeeding(): bool |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Check if done downloading. |
||
| 333 | * |
||
| 334 | * @return bool |
||
| 335 | */ |
||
| 336 | public function isDone(): bool |
||
| 337 | { |
||
| 338 | return $this->getLeftUntilDone(false) < 1; |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Check if given status matches the current status. |
||
| 343 | * |
||
| 344 | * @param $status |
||
| 345 | * |
||
| 346 | * @return bool |
||
| 347 | */ |
||
| 348 | public function isStatus($status): bool |
||
| 349 | { |
||
| 350 | return $this->get('status') === $status; |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Check if meta data needs to be complete. |
||
| 355 | * |
||
| 356 | * @return bool |
||
| 357 | */ |
||
| 358 | public function needsMetaData(): bool |
||
| 359 | { |
||
| 360 | return $this->getMetadataPercentComplete() < 1; |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Get Status String. |
||
| 365 | * |
||
| 366 | * @return string |
||
| 367 | */ |
||
| 368 | public function getStatusString() |
||
| 369 | { |
||
| 370 | switch ($this->get('status')) { |
||
| 371 | case static::STATUS_STOPPED: |
||
| 372 | return $this->get('isFinished', false) ? 'Seeding complete' : 'Paused'; |
||
| 373 | case static::STATUS_CHECK_WAIT: |
||
| 374 | return 'Queued for verification'; |
||
| 375 | case static::STATUS_CHECK: |
||
| 376 | return 'Verifying local data'; |
||
| 377 | case static::STATUS_DOWNLOAD_WAIT: |
||
| 378 | return 'Queued for download'; |
||
| 379 | case static::STATUS_DOWNLOAD: |
||
| 380 | return 'Downloading'; |
||
| 381 | case static::STATUS_SEED_WAIT: |
||
| 382 | return 'Queued for seeding'; |
||
| 383 | case static::STATUS_SEED: |
||
| 384 | return 'Seeding'; |
||
| 385 | case null: |
||
| 386 | return 'Unknown'; |
||
| 387 | default: |
||
| 388 | return 'Error'; |
||
| 389 | |||
| 390 | } |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Get Seed Ratio Limit. |
||
| 395 | * |
||
| 396 | * @param int|Client $globalSeedRatioLimit Provide the global seed ratio limit if you already have cached. This is |
||
| 397 | * to prevent fetching on every request when looping through multiple |
||
| 398 | * torrents as it'll be very slow doing so. It's recommended to cache it |
||
| 399 | * once and pass to this method, otherwise provide Client instance. |
||
| 400 | * |
||
| 401 | * @return int|string |
||
| 402 | */ |
||
| 403 | public function seedRatioLimit($globalSeedRatioLimit) |
||
| 404 | { |
||
| 405 | switch ($this->get('seedRatioMode')) { |
||
| 406 | case static::RATIO_USE_GLOBAL: |
||
| 407 | return ($globalSeedRatioLimit instanceof Client) ? $globalSeedRatioLimit->seedRatioLimit() : $globalSeedRatioLimit; |
||
| 408 | case static::RATIO_USE_LOCAL: |
||
| 409 | return $this->get('seedRatioLimit'); |
||
| 410 | default: |
||
| 411 | return -1; |
||
| 412 | } |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Get Error Message. |
||
| 417 | * |
||
| 418 | * @return null|string |
||
| 419 | */ |
||
| 420 | public function getErrorMessage() |
||
| 432 | } |
||
| 433 | } |
||
| 434 | } |