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

RollbackFormatter   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 44
ccs 14
cts 15
cp 0.9333
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A format() 0 19 1
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