Passed
Branch release/v2.0.0 (bebdea)
by Anatoly
04:02
created

AbstractException::getContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Fenric <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Fenric
8
 * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-router
10
 */
11
12
namespace Sunrise\Http\Router\Exception;
13
14
/**
15
 * Import classes
16
 */
17
use RuntimeException;
18
use Throwable;
19
20
/**
21
 * AbstractException
22
 */
23
abstract class AbstractException extends RuntimeException implements ExceptionInterface
24
{
25
26
    /**
27
     * Context of the exception
28
     *
29
     * @var array
30
     */
31
    private $context = [];
32
33
    /**
34
     * Constructor of the exception
35
     *
36
     * @param string    $message
37
     * @param array     $context
38
     * @param int       $code
39
     * @param Throwable $previous
40
     */
41 295
    public function __construct(string $message = '', array $context = [], int $code = 0, Throwable $previous = null)
42
    {
43 295
        $this->context = $context;
44
45 295
        parent::__construct($message, $code, $previous);
46 295
    }
47
48
    /**
49
     * Gets the exception context
50
     *
51
     * @return array
52
     */
53 17
    public function getContext() : array
54
    {
55 17
        return $this->context;
56
    }
57
58
    /**
59
     * Gets data from the exception context for the given key
60
     *
61
     * The default value will be returned if is no data
62
     * in the exception context for the given key.
63
     *
64
     * @param string $key
65
     * @param mixed $default
66
     *
67
     * @return mixed
68
     */
69 9
    public function fromContext(string $key, $default = null)
70
    {
71 9
        return $this->context[$key] ?? $default;
72
    }
73
}
74