Issues (33)

src/Exception/OperationExceptionFactory.php (1 issue)

1
<?php
0 ignored issues
show
Header blocks must be separated by a single blank line
Loading history...
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