Test Failed
Pull Request — master (#5)
by Jan-Marten
04:30
created

FailedRollbackFormatter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 3
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
introduced by
An error occurred during processing; checking has been aborted. The error message was: implode(): Passing glue string after array is deprecated. Swap the parameters in /home/scrutinizer/build/vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php on line 468
Loading history...
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