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
|
|
|
/** @var OperationFormatterInterface */ |
15
|
|
|
private $operationFormatter; |
16
|
|
|
|
17
|
|
|
/** @var ExceptionFormatterInterface */ |
18
|
|
|
private $exceptionFormatter; |
19
|
|
|
|
20
|
|
|
/** @var OperationFailureFormatterInterface */ |
21
|
|
|
private $failureFormatter; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Constructor. |
25
|
|
|
* |
26
|
|
|
* @param OperationFormatterInterface|null $operationFormatter |
27
|
|
|
* @param ExceptionFormatterInterface|null $exceptionFormatter |
28
|
|
|
* @param OperationFailureFormatterInterface|null $failureFormatter |
29
|
|
|
*/ |
30
|
6 |
|
public function __construct( |
31
|
|
|
OperationFormatterInterface $operationFormatter = null, |
32
|
|
|
ExceptionFormatterInterface $exceptionFormatter = null, |
33
|
|
|
OperationFailureFormatterInterface $failureFormatter = null |
34
|
|
|
) { |
35
|
6 |
|
$this->operationFormatter = ( |
36
|
6 |
|
$operationFormatter ?? new OperationFormatter() |
37
|
|
|
); |
38
|
6 |
|
$this->exceptionFormatter = ( |
39
|
6 |
|
$exceptionFormatter ?? new ExceptionFormatter() |
40
|
|
|
); |
41
|
6 |
|
$this->failureFormatter = ( |
42
|
6 |
|
$failureFormatter ?? new OperationFailureFormatter( |
43
|
2 |
|
$this->operationFormatter, |
44
|
2 |
|
$this->exceptionFormatter |
45
|
|
|
) |
46
|
|
|
); |
47
|
6 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Format the failed rollback to a readable string. |
51
|
|
|
* |
52
|
|
|
* @param FailedRollbackExceptionInterface $rollback |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
3 |
|
public function format(FailedRollbackExceptionInterface $rollback): string |
57
|
|
|
{ |
58
|
3 |
|
$previous = $rollback->getPreviousRollbacks(); |
59
|
|
|
$header = [ |
60
|
3 |
|
$rollback->getMessage(), |
61
|
3 |
|
$this->operationFormatter->format($rollback->getOperation()), |
62
|
3 |
|
$this->exceptionFormatter->format($rollback->getPrevious()), |
63
|
|
|
]; |
64
|
|
|
|
65
|
3 |
|
if (!empty($previous)) { |
66
|
2 |
|
$header[] = ''; |
67
|
2 |
|
$header[] = 'Previous rollbacks:'; |
68
|
|
|
} |
69
|
|
|
|
70
|
3 |
|
return implode( |
71
|
3 |
|
PHP_EOL, |
72
|
3 |
|
array_reduce( |
73
|
3 |
|
$previous, |
74
|
|
|
function ( |
75
|
|
|
array $carry, |
76
|
|
|
OperationFailureInterface $failure |
77
|
|
|
): array { |
78
|
2 |
|
$carry[] = $this->failureFormatter->format($failure); |
79
|
|
|
|
80
|
2 |
|
return $carry; |
81
|
3 |
|
}, |
82
|
3 |
|
$header |
83
|
|
|
) |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|