File
2 years ago
Interfaces
2 years ago
Job
2 years ago
Service
2 years ago
Task
2 years ago
Traits
1 year ago
AbstractDto.php
3 years ago
AbstractTaskDto.php
2 years ago
JobDataDto.php
2 years ago
StepsDto.php
2 years ago
TaskResponseDto.php
2 years ago
StepsDto.php
106 lines
| 1 | <?php |
| 2 | |
| 3 | // TODO PHP7.x declare(strict_types=1); |
| 4 | // TODO PHP7.x type-hints & return types |
| 5 | |
| 6 | namespace WPStaging\Backup\Dto; |
| 7 | |
| 8 | class StepsDto extends AbstractDto |
| 9 | { |
| 10 | /** @var int */ |
| 11 | private $total; |
| 12 | |
| 13 | /** @var int */ |
| 14 | private $current; |
| 15 | |
| 16 | /** @var int */ |
| 17 | private $manualPercentage; |
| 18 | |
| 19 | /** |
| 20 | * @return int |
| 21 | */ |
| 22 | public function getTotal() |
| 23 | { |
| 24 | return $this->total; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * @param int $total |
| 29 | */ |
| 30 | public function setTotal($total) |
| 31 | { |
| 32 | $this->total = (int) $total; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @return int |
| 37 | */ |
| 38 | public function getCurrent() |
| 39 | { |
| 40 | return $this->current; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @param int $current |
| 45 | */ |
| 46 | public function setCurrent($current) |
| 47 | { |
| 48 | $this->current = (int) $current; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Sometimes we can't know how many steps there will be in total, |
| 53 | * so we can mimic an percentage using this method. |
| 54 | * |
| 55 | * For instance: FileScannerTask doesn't know how many total |
| 56 | * steps it will have to process, so we can set a manual estimate. |
| 57 | * |
| 58 | * @param int $manualPercentage |
| 59 | */ |
| 60 | public function setManualPercentage($manualPercentage) |
| 61 | { |
| 62 | $this->manualPercentage = (int)$manualPercentage; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @return int |
| 67 | */ |
| 68 | public function getPercentage() |
| 69 | { |
| 70 | if (!empty($this->manualPercentage)) { |
| 71 | return $this->manualPercentage; |
| 72 | } |
| 73 | |
| 74 | if ($this->total < 1) { |
| 75 | return 100; |
| 76 | } |
| 77 | |
| 78 | $percentage = (int) round(($this->current / $this->total) * 100); |
| 79 | return max(0, min(100, $percentage)); |
| 80 | } |
| 81 | |
| 82 | public function incrementCurrentStep() |
| 83 | { |
| 84 | if ($this->current < $this->total) { |
| 85 | $this->current++; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | public function decreaseCurrentStep() |
| 90 | { |
| 91 | if ($this->current > 0) { |
| 92 | $this->current--; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | public function isFinished() |
| 97 | { |
| 98 | return $this->total <= $this->current; |
| 99 | } |
| 100 | |
| 101 | public function finish() |
| 102 | { |
| 103 | $this->current = $this->total; |
| 104 | } |
| 105 | } |
| 106 |