Cli
1 year ago
Exporters
1 year ago
Importers
11 months ago
ResourceStorages
1 year ago
ResultFormatters
1 year ago
Schemas
1 year ago
Steps
1 year ago
docs
1 year ago
BuiltInExporters.php
1 year ago
BuiltInStepProcessors.php
1 year ago
ClassExtractor.php
1 year ago
Cli.php
1 year ago
ExportSchema.php
1 year ago
ImportSchema.php
1 year ago
ImportStep.php
1 year ago
Logger.php
1 year ago
ResourceStorages.php
1 year ago
StepProcessor.php
1 year ago
StepProcessorResult.php
1 year ago
UsePluginHelpers.php
1 year ago
UsePubSub.php
1 year ago
UseWPFunctions.php
1 year ago
Util.php
1 year ago
StepProcessorResult.php
180 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Blueprint; |
| 4 | |
| 5 | use InvalidArgumentException; |
| 6 | |
| 7 | /** |
| 8 | * A class returned by StepProcessor classes containing result of the process and messages. |
| 9 | */ |
| 10 | class StepProcessorResult { |
| 11 | const MESSAGE_TYPES = array( 'error', 'info', 'debug', 'warn' ); |
| 12 | |
| 13 | /** |
| 14 | * Messages |
| 15 | * |
| 16 | * @var array $messages |
| 17 | */ |
| 18 | private array $messages = array(); |
| 19 | |
| 20 | /** |
| 21 | * Indicate whether the process was success or not |
| 22 | * |
| 23 | * @var bool $success |
| 24 | */ |
| 25 | private bool $success; |
| 26 | |
| 27 | /** |
| 28 | * Step name |
| 29 | * |
| 30 | * @var string $step_name |
| 31 | */ |
| 32 | private string $step_name; |
| 33 | |
| 34 | /** |
| 35 | * Construct. |
| 36 | * |
| 37 | * @param bool $success Indicate whether the process was success or not. |
| 38 | * @param string $step_name The name of the step. |
| 39 | */ |
| 40 | public function __construct( bool $success, string $step_name ) { |
| 41 | $this->success = $success; |
| 42 | $this->step_name = $step_name; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Get messages. |
| 47 | * |
| 48 | * @param string $step_name The name of the step. |
| 49 | * |
| 50 | * @return void |
| 51 | */ |
| 52 | public function set_step_name( $step_name ) { |
| 53 | $this->step_name = $step_name; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Create a new instance with $success = true. |
| 58 | * |
| 59 | * @param string $stp_name The name of the step. |
| 60 | * |
| 61 | * @return StepProcessorResult |
| 62 | */ |
| 63 | public static function success( string $stp_name ): self { |
| 64 | return ( new self( true, $stp_name ) ); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | /** |
| 69 | * Add a new message. |
| 70 | * |
| 71 | * @param string $message message. |
| 72 | * @param string $type one of error, info. |
| 73 | * |
| 74 | * @throws InvalidArgumentException When incorrect type is given. |
| 75 | * @return void |
| 76 | */ |
| 77 | public function add_message( string $message, string $type = 'error' ) { |
| 78 | if ( ! in_array( $type, self::MESSAGE_TYPES, true ) ) { |
| 79 | // phpcs:ignore |
| 80 | throw new InvalidArgumentException( "{$type} is not allowed. Type must be one of " . implode( ',', self::MESSAGE_TYPES ) ); |
| 81 | } |
| 82 | |
| 83 | $this->messages[] = compact( 'message', 'type' ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Merge messages from another StepProcessorResult instance. |
| 88 | * |
| 89 | * @param StepProcessorResult $other The other StepProcessorResult instance. |
| 90 | * |
| 91 | * @return void |
| 92 | */ |
| 93 | public function merge_messages( StepProcessorResult $other ) { |
| 94 | $this->messages = array_merge( $this->messages, $other->get_messages() ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Add a new error message. |
| 99 | * |
| 100 | * @param string $message message. |
| 101 | * |
| 102 | * @return void |
| 103 | */ |
| 104 | public function add_error( string $message ) { |
| 105 | $this->add_message( $message ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Add a new debug message. |
| 110 | * |
| 111 | * @param string $message message. |
| 112 | * |
| 113 | * @return void |
| 114 | */ |
| 115 | public function add_debug( string $message ) { |
| 116 | $this->add_message( $message, 'debug' ); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | /** |
| 121 | * Add a new info message. |
| 122 | * |
| 123 | * @param string $message message. |
| 124 | * |
| 125 | * @return void |
| 126 | */ |
| 127 | public function add_info( string $message ) { |
| 128 | $this->add_message( $message, 'info' ); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Add a new warn message. |
| 133 | * |
| 134 | * @param string $message message. |
| 135 | * |
| 136 | * @return void |
| 137 | */ |
| 138 | public function add_warn( string $message ) { |
| 139 | $this->add_message( $message, 'warn' ); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Filter messages. |
| 144 | * |
| 145 | * @param string $type one of all, error, and info. |
| 146 | * |
| 147 | * @return array |
| 148 | */ |
| 149 | public function get_messages( string $type = 'all' ): array { |
| 150 | if ( 'all' === $type ) { |
| 151 | return $this->messages; |
| 152 | } |
| 153 | |
| 154 | return array_filter( |
| 155 | $this->messages, |
| 156 | function ( $message ) use ( $type ) { |
| 157 | return $type === $message['type']; |
| 158 | } |
| 159 | ); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Check to see if the result was success. |
| 164 | * |
| 165 | * @return bool |
| 166 | */ |
| 167 | public function is_success(): bool { |
| 168 | return true === $this->success && 0 === count( $this->get_messages( 'error' ) ); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Get the name of the step. |
| 173 | * |
| 174 | * @return string The name of the step. |
| 175 | */ |
| 176 | public function get_step_name() { |
| 177 | return $this->step_name; |
| 178 | } |
| 179 | } |
| 180 |