ValidationException   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 68
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getStatusCode() 0 4 1
A getErrors() 0 4 1
A __toString() 0 13 2
1
<?php
2
3
namespace Moip\Exceptions;
4
5
use RuntimeException;
6
7
/**
8
 * Class ValidationException.
9
 */
10
class ValidationException extends RuntimeException
11
{
12
    /**
13
     * @var int
14
     */
15
    private $statusCode;
16
17
    /**
18
     * @var Error[]
19
     */
20
    private $errors;
21
22
    /**
23
     * ValidationException constructor.
24
     *
25
     * Exception thrown when the moip API returns a 4xx http code.
26
     * Indicates that an invalid value was passed.
27
     *
28
     * @param int     $statusCode
29
     * @param Error[] $errors
30
     */
31
    public function __construct($statusCode, $errors)
32
    {
33
        $this->errors = $errors;
34
        $this->statusCode = $statusCode;
35
    }
36
37
    /**
38
     * Returns the http status code ie.: 400.
39
     *
40
     * @return int
41
     */
42
    public function getStatusCode()
43
    {
44
        return $this->statusCode;
45
    }
46
47
    /**
48
     * Returns the list of errors returned by the API.
49
     *
50
     * @return Error[]
51
     *
52
     * @see \Moip\Exceptions\Error
53
     */
54
    public function getErrors()
55
    {
56
        return $this->errors;
57
    }
58
59
    /**
60
     * Convert error variables in string.
61
     *
62
     * @return string
63
     */
64
    public function __toString()
65
    {
66
        $template = "[$this->code] The following errors ocurred:\n%s";
67
        $temp_list = '';
68
        foreach ($this->errors as $error) {
69
            $path = $error->getPath();
70
            $desc = $error->getDescription();
71
72
            $temp_list .= "$path: $desc\n";
73
        }
74
75
        return sprintf($template, $temp_list);
76
    }
77
}
78