Test Failed
Pull Request — master (#5)
by Jan-Marten
08:20
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
    /** @var OperationFailureFormatterInterface */
15
    private $failureFormatter;
16
17
    /**
18
     * Constructor.
19
     *
20
     * @param OperationFailureFormatterInterface|null $failureFormatter
21
     */
22 1
    public function __construct(
23
        OperationFailureFormatterInterface $failureFormatter = null
24
    ) {
25 1
        $this->failureFormatter = (
26 1
            $failureFormatter ?? new OperationFailureFormatter()
27
        );
28 1
    }
29
30
    /**
31
     * Format the given rollback exception into a readable string.
32
     *
33
     * @param TransactionRolledBackExceptionInterface $rollback
34
     *
35
     * @return string
36
     */
37 2
    public function format(
38
        TransactionRolledBackExceptionInterface $rollback
39
    ): string {
40 2
        return implode(
41 2
            PHP_EOL,
42
            array_reduce(
43 2
                $rollback->getFailures(),
44
                function (
45
                    array $carry,
46
                    OperationFailureInterface $failure
47
                ): array {
48 1
                    $carry[] = $this->failureFormatter->format($failure);
49
50 1
                    return $carry;
51 2
                },
52
                [
53 2
                    $rollback->getMessage(),
54 2
                    '',
55 2
                    'Stacktrace:'
56
                ]
57
            )
58
        );
59
    }
60
}
61