Passed
Pull Request — master (#5)
by Jan-Marten
05:15
created

OperationExceptionFactory::createFromThrowable()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 17
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 27
ccs 18
cts 18
cp 1
crap 4
rs 9.7
1
<?php
2
/**
3
 * Copyright MediaCT. All rights reserved.
4
 * https://www.mediact.nl
5
 */
6
7
namespace Johmanx10\Transaction\Exception;
8
9
use Johmanx10\Transaction\Formatter\ExceptionFormatter;
10
use Johmanx10\Transaction\Formatter\ExceptionFormatterInterface;
11
use Johmanx10\Transaction\Formatter\FailedRollbackFormatter;
12
use Johmanx10\Transaction\Formatter\FailedRollbackFormatterInterface;
13
use Johmanx10\Transaction\Formatter\RollbackFormatter;
14
use Johmanx10\Transaction\Formatter\RollbackFormatterInterface;
15
use Throwable;
16
17
class OperationExceptionFactory implements OperationExceptionFactoryInterface
18
{
19
    /** @var RollbackFormatterInterface */
20
    private $rollbackFormatter;
21
22
    /** @var FailedRollbackFormatterInterface */
23
    private $failedFormatter;
24
25
    /** @var ExceptionFormatterInterface */
26
    private $exceptionFormatter;
27
28
    /**
29
     * Constructor.
30
     *
31
     * @param RollbackFormatterInterface|null       $rollbackFormatter
32
     * @param FailedRollbackFormatterInterface|null $failedFormatter
33
     * @param ExceptionFormatterInterface|null      $exceptionFormatter
34
     */
35 1
    public function __construct(
36
        RollbackFormatterInterface $rollbackFormatter = null,
37
        FailedRollbackFormatterInterface $failedFormatter = null,
38
        ExceptionFormatterInterface $exceptionFormatter = null
39
    ) {
40 1
        $this->rollbackFormatter  = $rollbackFormatter ?? new RollbackFormatter();
41 1
        $this->failedFormatter    = $failedFormatter ?? new FailedRollbackFormatter();
42 1
        $this->exceptionFormatter = $exceptionFormatter ?? new ExceptionFormatter();
43 1
    }
44
45
    /**
46
     * Create an operation exception from the given throwable.
47
     *
48
     * @param Throwable $subject
49
     *
50
     * @return OperationExceptionInterface
51
     */
52 3
    public function createFromThrowable(
53
        Throwable $subject
54
    ): OperationExceptionInterface {
55 3
        if ($subject instanceof TransactionRolledBackExceptionInterface) {
56 1
            [$failure] = $subject->getFailures() + [null];
57
58 1
            return new OperationException(
59 1
                $this->rollbackFormatter->format($subject),
60 1
                $failure
61 1
                    ? $failure->getOperation()
62 1
                    : null,
63 1
                $subject
64
            );
65
        }
66
67 2
        if ($subject instanceof FailedRollbackExceptionInterface) {
68 1
            return new OperationException(
69 1
                $this->failedFormatter->format($subject),
70 1
                $subject->getOperation(),
71 1
                $subject
72
            );
73
        }
74
75 1
        return new OperationException(
76 1
            $this->exceptionFormatter->format($subject),
77 1
            null,
78 1
            $subject
79
        );
80
    }
81
}
82