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

RollbackFormatter::format()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1.0007

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 1
dl 0
loc 19
ccs 10
cts 11
cp 0.9091
crap 1.0007
rs 9.8333
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\TransactionRolledBackExceptionInterface;
10
use Johmanx10\Transaction\OperationFailureInterface;
11
12
class RollbackFormatter implements RollbackFormatterInterface
13
{
14
    private OperationFailureFormatterInterface $failureFormatter;
15
16
    /**
17
     * Constructor.
18
     *
19
     * @param OperationFailureFormatterInterface|null $failureFormatter
20
     */
21 1
    public function __construct(
22
        OperationFailureFormatterInterface $failureFormatter = null
23
    ) {
24 1
        $this->failureFormatter = (
25 1
            $failureFormatter ?? new OperationFailureFormatter()
26
        );
27 1
    }
28
29
    /**
30
     * Format the given rollback exception into a readable string.
31
     *
32
     * @param TransactionRolledBackExceptionInterface $rollback
33
     *
34
     * @return string
35
     */
36 2
    public function format(
37
        TransactionRolledBackExceptionInterface $rollback
38
    ): string {
39 2
        return implode(
40 2
            PHP_EOL,
41
            array_reduce(
42 2
                $rollback->getFailures(),
43
                function (
44
                    array $carry,
45
                    OperationFailureInterface $failure
46
                ): array {
47 1
                    $carry[] = $this->failureFormatter->format($failure);
48
49 1
                    return $carry;
50 2
                },
51
                [
52 2
                    $rollback->getMessage(),
53 2
                    '',
54 2
                    'Stacktrace:'
55
                ]
56
            )
57
        );
58
    }
59
}
60