1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* Copyright MediaCT. All rights reserved. |
4
|
|
|
* https://www.mediact.nl |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Johmanx10\Transaction\Formatter; |
8
|
|
|
|
9
|
|
|
use Johmanx10\Transaction\Exception\FailedRollbackExceptionInterface; |
10
|
|
|
use Johmanx10\Transaction\OperationFailureInterface; |
11
|
|
|
|
12
|
|
|
class FailedRollbackFormatter implements FailedRollbackFormatterInterface |
13
|
|
|
{ |
14
|
|
|
private OperationFormatterInterface $operationFormatter; |
15
|
|
|
|
16
|
|
|
private ExceptionFormatterInterface $exceptionFormatter; |
17
|
|
|
|
18
|
|
|
private OperationFailureFormatterInterface $failureFormatter; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Constructor. |
22
|
|
|
* |
23
|
|
|
* @param OperationFormatterInterface|null $operationFormatter |
24
|
|
|
* @param ExceptionFormatterInterface|null $exceptionFormatter |
25
|
|
|
* @param OperationFailureFormatterInterface|null $failureFormatter |
26
|
|
|
*/ |
27
|
6 |
|
public function __construct( |
28
|
|
|
OperationFormatterInterface $operationFormatter = null, |
29
|
|
|
ExceptionFormatterInterface $exceptionFormatter = null, |
30
|
|
|
OperationFailureFormatterInterface $failureFormatter = null |
31
|
|
|
) { |
32
|
6 |
|
$this->operationFormatter = ( |
33
|
6 |
|
$operationFormatter ?? new OperationFormatter() |
34
|
|
|
); |
35
|
6 |
|
$this->exceptionFormatter = ( |
36
|
6 |
|
$exceptionFormatter ?? new ExceptionFormatter() |
37
|
|
|
); |
38
|
6 |
|
$this->failureFormatter = ( |
39
|
6 |
|
$failureFormatter ?? new OperationFailureFormatter( |
40
|
2 |
|
$this->operationFormatter, |
41
|
2 |
|
$this->exceptionFormatter |
42
|
|
|
) |
43
|
|
|
); |
44
|
6 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Format the failed rollback to a readable string. |
48
|
|
|
* |
49
|
|
|
* @param FailedRollbackExceptionInterface $rollback |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
3 |
|
public function format(FailedRollbackExceptionInterface $rollback): string |
54
|
|
|
{ |
55
|
3 |
|
$previous = $rollback->getPreviousRollbacks(); |
56
|
|
|
$header = [ |
57
|
3 |
|
$rollback->getMessage(), |
58
|
3 |
|
$this->operationFormatter->format($rollback->getOperation()), |
59
|
3 |
|
$this->exceptionFormatter->format($rollback->getPrevious()), |
60
|
|
|
]; |
61
|
|
|
|
62
|
3 |
|
if (!empty($previous)) { |
63
|
2 |
|
$header[] = ''; |
64
|
2 |
|
$header[] = 'Previous rollbacks:'; |
65
|
|
|
} |
66
|
|
|
|
67
|
3 |
|
return implode( |
68
|
3 |
|
PHP_EOL, |
69
|
|
|
array_reduce( |
70
|
3 |
|
$previous, |
71
|
|
|
function ( |
72
|
|
|
array $carry, |
73
|
|
|
OperationFailureInterface $failure |
74
|
|
|
): array { |
75
|
2 |
|
$carry[] = $this->failureFormatter->format($failure); |
76
|
|
|
|
77
|
2 |
|
return $carry; |
78
|
3 |
|
}, |
79
|
|
|
$header |
80
|
|
|
) |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|