Passed
Pull Request — master (#41)
by Thomas
03:07
created

Error::getCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace ORM\Dbal;
4
5
use ORM\Dbal\Column;
6
use ORM\EntityManager;
7
use ORM\Namer;
8
9
class Error
10
{
11
    const ERROR_CODE = 'UNKNOWN';
12
13
    /** @var string[] */
14
    protected $params = [
15
        'code' => self::ERROR_CODE
16
    ];
17
18
    /** @var string */
19
    protected $message = 'ERROR(%code%) occurred';
20
21
    /**
22
     * Error constructor
23
     *
24
     * @param array $params
25
     * @param null $code
26
     * @param null $message
27
     */
28 39
    public function __construct(array $params = [], $code = null, $message = null)
29
    {
30
        // set code from concrete class
31 39
        $this->params['code'] = static::ERROR_CODE;
32
33
        // overwrite message from params
34 39
        if ($message) {
35 1
            $this->message = $message;
36
        }
37
38
        // overwrite code from params
39 39
        if ($code) {
40 1
            $this->params['code'] = $code;
41
        }
42
43 39
        $this->addParams($params);
44 39
    }
45
46
    /**
47
     * Add message parameters
48
     *
49
     * @param string[] $params
50
     */
51 39
    public function addParams($params)
52
    {
53 39
        $this->params = array_merge($this->params, $params);
54 39
    }
55
56
    /**
57
     * Build and return message
58
     *
59
     * @return string
60
     */
61 38
    public function getMessage()
62
    {
63 38
        return EntityManager::getInstance()->getNamer()->substitute($this->message, $this->params);
64
    }
65
66
    /**
67
     * @return string
68
     */
69 1
    public function getCode()
70
    {
71 1
        return $this->params['code'];
72
    }
73
}
74