Passed
Push — master ( 5319ee...e6b641 )
by Dani
02:13
created

RESTfulException   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 51
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A get() 0 7 2
A getErrorCode() 0 4 1
1
<?php
2
3
namespace Postpay\Exceptions;
4
5
use Postpay\Http\Response;
6
7
class RESTfulException extends ApiException
8
{
9
    /**
10
     * @const string Error key returned.
11
     */
12
    const ERROR_KEY = 'error';
13
14
    /**
15
     * @var string API error code.
16
     */
17
    protected $errorCode;
18
19
    /**
20
     * Creates a RESTfulException.
21
     *
22
     * @param Response $response The response that threw the exception.
23
     */
24 4
    public function __construct(Response $response)
25
    {
26 4
        $this->response = $response;
27 4
        $this->decodedBody = $response->json();
28 4
        $this->errorCode = $this->get('code', 'unknown');
29 4
        parent::__construct($this->get('message', 'Unknown error.'));
30 4
    }
31
32
    /**
33
     * Checks isset and returns that or a default value.
34
     *
35
     * @param string $key
36
     * @param mixed  $default
37
     *
38
     * @return mixed
39
     */
40 4
    private function get($key, $default = null)
41
    {
42 4
        if (isset($this->decodedBody[self::ERROR_KEY][$key])) {
43 1
            return $this->decodedBody[self::ERROR_KEY][$key];
44
        }
45 4
        return $default;
46
    }
47
48
    /**
49
     * Returns API error code.
50
     *
51
     * @return string
52
     */
53 1
    public function getErrorCode()
54
    {
55 1
        return $this->errorCode;
56
    }
57
}
58