PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / trunk
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! vtrunk
3.8.0 3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 All 112 releases
templately / modules / utilities / Cleanup / TaskResult.php

TaskResult.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! trunk, at modules/utilities/Cleanup/TaskResult.php

118 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * What one cleanup task reports from an estimate or a run.
4 *
5 * @package Templately
6 */
7
8 namespace Templately\Modules\Utilities\Cleanup;
9
10 /**
11 * One shape for both, deliberately: estimation and execution are the same code
12 * path with `dry_run` flipped, so the number a user confirms against is produced
13 * by the logic that would do the work.
14 */
15 final class TaskResult {
16
17 /** @var int Records and/or FILES affected — never directory nodes. */
18 public $items = 0;
19
20 /**
21 * @var int Directories removed.
22 *
23 * Counted apart from `items` deliberately: a user reading "12 items
24 * removed" means twelve files or records, not four files plus the eight
25 * folders that happened to contain them.
26 */
27 public $dirs = 0;
28
29 /** @var int Bytes reclaimed, or reclaimable when the run was a dry run. */
30 public $bytes = 0;
31
32 /** @var string[] Per-item failures. A non-empty list does NOT abort the run. */
33 public $errors = [];
34
35 /** @var bool True when the task stopped on budget with work left to do. */
36 public $incomplete = false;
37
38 /** @var bool True when the task could not run at all (e.g. uploads unwritable). */
39 public $unavailable = false;
40
41 public static function empty(): self {
42 return new self();
43 }
44
45 /**
46 * A task that could not run. Distinct from a task that ran and found
47 * nothing — the UI must be able to explain the difference rather than
48 * showing "0 bytes" for both.
49 */
50 public static function unavailable( string $reason ): self {
51 $result = new self();
52 $result->unavailable = true;
53 $result->errors[] = $reason;
54
55 return $result;
56 }
57
58 /**
59 * @param int $bytes
60 * @return $this
61 */
62 public function add( int $items, int $bytes ): self {
63 $this->items += $items;
64 $this->bytes += $bytes;
65
66 return $this;
67 }
68
69 /**
70 * @return $this
71 */
72 public function add_dirs( int $dirs ): self {
73 $this->dirs += $dirs;
74
75 return $this;
76 }
77
78 /**
79 * @return $this
80 */
81 public function fail( string $message ): self {
82 $this->errors[] = $message;
83
84 return $this;
85 }
86
87 /**
88 * @return $this
89 */
90 public function stopped_on_budget(): self {
91 $this->incomplete = true;
92
93 return $this;
94 }
95
96 public function merge( TaskResult $other ): self {
97 $this->items += $other->items;
98 $this->dirs += $other->dirs;
99 $this->bytes += $other->bytes;
100 $this->errors = array_merge( $this->errors, $other->errors );
101 $this->incomplete = $this->incomplete || $other->incomplete;
102 $this->unavailable = $this->unavailable || $other->unavailable;
103
104 return $this;
105 }
106
107 public function to_array(): array {
108 return [
109 'items' => $this->items,
110 'dirs' => $this->dirs,
111 'bytes' => $this->bytes,
112 'errors' => array_values( $this->errors ),
113 'incomplete' => $this->incomplete,
114 'unavailable' => $this->unavailable,
115 ];
116 }
117 }
118