Passed
Push — master ( fd1df3...32651b )
by Maike
04:28 queued 02:09
created

Error::instance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace MocOrm\Model;
4
5
class Error
6
{
7
    private static $_instance;
8
9
    const ACCEPTED_ERROR = [
10
        'Exception',
11
        'InvalidArgumentException',
12
    ];
13
14
    private $errors = [];
15
    private $burst;
16
17 1
    public function __construct()
18
    {
19 1
        return $this;
20
    }
21
22 2
    public static function create($message, $code, $type = 'Exception')
23
    {
24 2
        $instance = self::instance();
25
26 2
        if ($instance->burst) $instance->burst($message, $code, $type);
27
28 2
        $errors['message'] = $message;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$errors was never initialized. Although not strictly required by PHP, it is generally a good practice to add $errors = array(); before regardless.
Loading history...
29 2
        $errors['code'] = $code;
30 2
        $errors['type'] = $type;
31
32 2
        $instance->errors[] = $errors;
33
34 2
        return $instance;
35
    }
36
37 2
    public static function instance()
38
    {
39 2
        if (!self::$_instance) self::$_instance = new self();
40 2
        return self::$_instance;
41
    }
42
43 1
    public function getAll()
44
    {
45 1
        return $this->errors;
46
    }
47
48 1
    public function getLast()
49
    {
50 1
        return end($this->errors);
51
    }
52
53 1
    public function getFirst()
54
    {
55 1
        return current($this->errors);
56
    }
57
58
    private function burst($message, $code, $type)
59
    {
60
//        if(!array_search($type, ERRORS)) throw new \InvalidArgumentException('Type of burst not found.');
61
        $type = '\\' . $type;
62
        throw new $type($message, $code);
63
    }
64
}