Completed
Push — master ( e78a07...82539f )
by Jean C.
02:16
created

ValidationException::getStatusCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Moip\Exceptions;
4
5
class ValidationException extends \RuntimeException
6
{
7
    /**
8
     * @var int
9
     */
10
    private $statusCode;
11
12
    /**
13
     * @var string
14
     */
15
    private $statusMessage;
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 string  $statusMessage
30
     * @param Error[] $errors
31
     */
32
    public function __construct($statusCode, $statusMessage, $errors)
33
    {
34
        $this->errors = $errors;
35
        $this->statusCode = $statusCode;
36
        $this->statusMessage = $statusMessage;
37
    }
38
39
    /**
40
     * Returns the http status code ie.: 400.
41
     *
42
     * @return int
43
     */
44
    public function getStatusCode()
45
    {
46
        return $this->statusCode;
47
    }
48
49
    /**
50
     * Returns the http status code description: ie.: 'Bad Request'.
51
     *
52
     * @return string
53
     */
54
    public function getStatusMessage()
55
    {
56
        return $this->statusMessage;
57
    }
58
59
    /**
60
     * Returns the list of errors returned by the API.
61
     *
62
     * @return Error[]
63
     *
64
     * @see \Moip\Exceptions\Error
65
     */
66
    public function getErrors()
67
    {
68
        return $this->errors;
69
    }
70
71
    public function __toString()
72
    {
73
        $template = "[$this->statusMessage] The following errors ocurred:\n%s";
74
        $temp_list = '';
75
        foreach ($this->errors as $error) {
76
            $path = $error->getPath();
77
            $desc = $error->getDescription();
78
79
            $temp_list .= "$path: $desc\n";
80
        }
81
82
        return sprintf($template, $temp_list);
83
    }
84
}
85