| Total Complexity | 5 |
| Total Lines | 61 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | <?php |
||
| 9 | class Operation implements DescribableOperationInterface |
||
| 10 | { |
||
| 11 | /** @var callable */ |
||
| 12 | private $operation; |
||
| 13 | |||
| 14 | /** @var callable|null */ |
||
| 15 | private $rollback; |
||
| 16 | |||
| 17 | /** @var string */ |
||
| 18 | private $description; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Constructor. |
||
| 22 | * |
||
| 23 | * @param callable $operation |
||
| 24 | * @param callable|null $rollback |
||
| 25 | * @param string|null $description |
||
| 26 | */ |
||
| 27 | 1 | public function __construct( |
|
| 28 | callable $operation, |
||
| 29 | callable $rollback = null, |
||
| 30 | string $description = null |
||
| 31 | ) { |
||
| 32 | 1 | $this->operation = $operation; |
|
| 33 | 1 | $this->rollback = $rollback; |
|
| 34 | 1 | $this->description = $description ?? sprintf( |
|
| 35 | 1 | 'Generic operation %s', |
|
| 36 | 1 | spl_object_hash($this) |
|
| 37 | ); |
||
| 38 | 1 | } |
|
| 39 | |||
| 40 | /** |
||
| 41 | * Execute the operation. |
||
| 42 | * |
||
| 43 | * @return void |
||
| 44 | */ |
||
| 45 | 1 | public function __invoke(): void |
|
| 46 | { |
||
| 47 | 1 | call_user_func($this->operation); |
|
| 48 | 1 | } |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Apply the rollback mechanism. |
||
| 52 | * |
||
| 53 | * @return void |
||
| 54 | */ |
||
| 55 | 1 | public function rollback(): void |
|
| 56 | { |
||
| 57 | 1 | if ($this->rollback !== null) { |
|
| 58 | 1 | call_user_func($this->rollback); |
|
| 59 | } |
||
| 60 | 1 | } |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Describe the current operation. |
||
| 64 | * |
||
| 65 | * @return string |
||
| 66 | */ |
||
| 67 | 1 | public function __toString(): string |
|
| 70 | } |
||
| 71 | } |
||
| 72 |