BaseException   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 84
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getArgs() 0 4 1
A setArgs() 0 10 2
A __construct() 0 7 1
A getItemFromVariableArray() 0 21 4
A getClassName() 0 4 1
1
<?php namespace CMPayments\Exception;
2
3
/**
4
 * Class BaseException
5
 *
6
 * @package CMPayments\SchemaValidator\Exceptions
7
 * @Author  Boy Wijnmaalen <[email protected]>
8
 */
9
class BaseException extends \ErrorException
10
{
11
    private $args = [];
12
13
    private $defaultMessage = 'This service is temporarily unavailable';
14
15
    /**
16
     * @return mixed
17
     */
18
    public function getArgs()
19
    {
20
        return $this->args;
21
    }
22
23
    /**
24
     * @param mixed $args
25
     */
26
    public function setArgs($args)
27
    {
28
        // check is $args is an array, if not, cast it into an array
29
        if (!is_array($args)) {
30
31
            $args = [$args];
32
        }
33
34
        $this->args = $args;
35
    }
36
37
    /**
38
     * BaseException constructor.
39
     *
40
     * @param string $code
41
     * @param array  $args
42
     * @param null   $message
43
     */
44
    public function __construct($code, $args = [], $message = null)
45
    {
46
        $this->setArgs($args);
47
48
        // parent constructor
49
        parent::__construct($this->getItemFromVariableArray($code, $message), $code);
50
    }
51
52
    /**
53
     * Retrieves a specific array key from a class constant
54
     *
55
     * @param int    $code
56
     * @param null   $default
57
     * @param string $msgArray
58
     *
59
     * @return null|string
60
     */
61
    public function getItemFromVariableArray($code, $default = null, $msgArray = 'messages')
62
    {
63
        $messages = [];
64
        if (isset($this->$msgArray)) {
65
66
            $messages = $this->$msgArray;
67
        }
68
69
        if (empty($default)) {
70
71
            if (!isset($messages[$code])) {
72
73
                $default = $this->defaultMessage;
74
            } else {
75
76
                $default = vsprintf($messages[$code], $this->getArgs());
77
            }
78
        }
79
80
        return $default;
81
    }
82
83
    /**
84
     * PHP 5.4 workaround to something like this
85
     *
86
     * @return string
87
     */
88
    public static function getClassName(){
89
90
        return get_called_class();
91
    }
92
}
93