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\OperationFailureInterface; |
10
|
|
|
|
11
|
|
|
class OperationFailureFormatter implements OperationFailureFormatterInterface |
12
|
|
|
{ |
13
|
|
|
/** @var OperationFormatterInterface */ |
14
|
|
|
private $operationFormatter; |
15
|
|
|
|
16
|
|
|
/** @var ExceptionFormatterInterface */ |
17
|
|
|
private $exceptionFormatter; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Constructor. |
21
|
|
|
* |
22
|
|
|
* @param OperationFormatterInterface|null $operationFormatter |
23
|
|
|
* @param ExceptionFormatterInterface|null $exceptionFormatter |
24
|
|
|
*/ |
25
|
4 |
|
public function __construct( |
26
|
|
|
OperationFormatterInterface $operationFormatter = null, |
27
|
|
|
ExceptionFormatterInterface $exceptionFormatter = null |
28
|
|
|
) { |
29
|
4 |
|
$this->operationFormatter = ( |
30
|
4 |
|
$operationFormatter ?? new OperationFormatter() |
31
|
|
|
); |
32
|
4 |
|
$this->exceptionFormatter = ( |
33
|
4 |
|
$exceptionFormatter ?? new ExceptionFormatter() |
34
|
|
|
); |
35
|
4 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Format the operation failure into a readable string. |
39
|
|
|
* |
40
|
|
|
* @param OperationFailureInterface $failure |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
3 |
|
public function format(OperationFailureInterface $failure): string |
45
|
|
|
{ |
46
|
3 |
|
$exception = $failure->getException(); |
47
|
3 |
|
$operation = $failure->getOperation(); |
48
|
3 |
|
$identifier = str_pad( |
49
|
3 |
|
sprintf('(%d)', spl_object_id($operation)), |
50
|
3 |
|
8, |
51
|
3 |
|
' ', |
52
|
3 |
|
STR_PAD_RIGHT |
53
|
|
|
); |
54
|
|
|
|
55
|
3 |
|
return $exception !== null |
56
|
2 |
|
? sprintf( |
57
|
2 |
|
'%s ∴ %s', |
58
|
2 |
|
$identifier, |
59
|
2 |
|
$this->exceptionFormatter->format($exception) |
60
|
|
|
) |
61
|
1 |
|
: sprintf( |
62
|
1 |
|
'%s ✔ %s', |
63
|
1 |
|
$identifier, |
64
|
3 |
|
$this->operationFormatter->format($operation) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|