ValidationException::getStatusCode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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