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

OperationFailureFormatter::format()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2.0094

Importance

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