DiException   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 10
dl 0
loc 38
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getData() 0 2 1
A buildMessage() 0 6 2
A __construct() 0 3 1
A getId() 0 2 1
1
<?php
2
3
namespace evelikto\di;
4
5
/** Base class for all di exceptions */
6
class DiException extends \Exception
7
{
8
    /**
9
     * Initializes underlying standard PHP Exception with expanded user message.
10
     *
11
     * @param   array   $data   Arbitrary data used to describe the exception.
12
     */
13
    public function __construct(array $data = []) {
14
        $this->data = $data;
15
        parent::__construct($this->buildMessage($data));
16
    }
17
18
    /**
19
     * Returns the exception ID which could be used for an alternative, user-friendly error message.
20
     * Exception ID is the fully qualified classname, where backslashes are replaces with dots.
21
     *
22
     * @return  string  Exception ID
23
     */
24
    public function getId() : string {
25
        return str_replace('\\', '.', get_class($this));
26
    }
27
28
    /**  @return  array  arbitrary additional data for message expanding */
29
    public function getData() : array {
30
        return $this->data;
31
    }
32
33
    /**  @return  string  default exception message */
34
    public function buildMessage(array $vars) {
35
        $message = $this->getId() . ':';
36
        foreach ($vars as $key => $value)
37
            $message .= " $key = $value;";
38
39
        return $message;
40
    }
41
42
    /**  @var  array  $data arbitrary additional data for message expanding */
43
    protected $data;
44
}