AmqpAgentException::rethrow()   B
last analyzed

Complexity

Conditions 8
Paths 64

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 8

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
eloc 15
c 2
b 0
f 0
nc 64
nop 3
dl 0
loc 24
ccs 15
cts 15
cp 1
crap 8
rs 8.4444
1
<?php
2
3
/**
4
 * @author Marwan Al-Soltany <[email protected]>
5
 * @copyright Marwan Al-Soltany 2020
6
 * For the full copyright and license information, please view
7
 * the LICENSE file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace MAKS\AmqpAgent\Exception;
13
14
use Exception as CoreException;
15
use MAKS\AmqpAgent\Helper\Utility;
16
17
/**
18
 * AMQP Agent base exception class.
19
 * @since 1.0.0
20
 */
21
class AmqpAgentException extends CoreException
22
{
23
    /**
24
     * Redefine the exception so message is not an optional parameter.
25
     * @param string $message
26
     * @param int $code
27
     * @param CoreException|null $previous
28
     */
29 42
    public function __construct(string $message, int $code = 0, CoreException $previous = null)
30
    {
31 42
        parent::__construct($message, $code, $previous);
32 42
    }
33
34
    /**
35
     * String representation of the object.
36
     * @return string
37
     */
38 1
    public function __toString()
39
    {
40 1
        return static::class . ": [{$this->code}]: {$this->message}\n{$this->getTraceAsString()}\n";
41
    }
42
43
    /**
44
     * Rethrows an exception with an additional message.
45
     * @param CoreException $exception The exception to rethrow.
46
     * @param string|null $message [optional] An additional message to add to the wrapping exception before the message of the passed exception.
47
     * @param string|bool $wrap [optional] Whether to throw the exception using the passed class (FQN), in the same exception type (true), or wrap it with the class this method was called on (false). Any other value will be translated to false. Defaults to true.
48
     * @return void
49
     * @throws CoreException
50
     */
51 14
    public static function rethrow(CoreException $exception, ?string $message = null, $wrap = true): void
52
    {
53 14
        if (null === $message) {
54 7
            $trace = Utility::backtrace(['file', 'line', 'class', 'function']);
55 7
            $prefix = (isset($trace['class']) ? "{$trace['class']}::" : "{$trace['file']}({$trace['line']}): ");
56 7
            $suffix = "{$trace['function']}() failed!";
57 7
            $message = 'Rethrown Exception: ' . $prefix . $suffix . ' ';
58
        } else {
59 7
            $message = strlen($message) ? $message . ' ' : $message;
60
        }
61
62 14
        $error = is_string($wrap)
63
            ? (
64 1
                class_exists($wrap) && is_subclass_of($wrap, 'Exception')
65 1
                    ? $wrap
66 1
                    : static::class
67
            )
68
            : (
69 13
                boolval($wrap)
70 5
                    ? get_class($exception)
71 14
                    : static::class
72
            );
73
74 14
        throw new $error($message . (string)$exception->getMessage(), (int)$exception->getCode(), $exception);
75
    }
76
77
    /**
78
     * Rethrows an exception with an additional message.
79
     * @deprecated 1.2.0 Use `self::rethrow()` instead.
80
     * @param CoreException $exception The exception to rethrow.
81
     * @param string|null $message [optional] An additional message to add to the wrapping exception before the message of the passed exception.
82
     * @param string|bool $wrap [optional] Whether to throw the exception using the passed class (FQN), in the same exception type (true), or wrap it with the class this method was called on (false). Any other value will be translated to false.
83
     * @return void
84
     * @throws CoreException
85
     */
86 1
    public static function rethrowException(CoreException $exception, ?string $message = null, $wrap = true): void
87
    {
88 1
        static::rethrow($exception, $message, $wrap);
89
    }
90
}
91