Passed
Push — master ( 06a092...1f9a54 )
by y
02:00
created

AsanaError::isCurl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php
2
3
namespace Helix\Asana\Api;
4
5
use Helix\Asana\Api;
6
use RuntimeException;
7
8
/**
9
 * An {@link Api} error.
10
 *
11
 * Codes less than `400` are cURL errors.
12
 *
13
 * The {@link Api} returns `null` on `404`, it doesn't throw.
14
 *
15
 * @see https://developers.asana.com/docs/errors
16
 */
17
class AsanaError extends RuntimeException {
18
19
    /**
20
     * @var array
21
     */
22
    protected $curlInfo;
23
24
    /**
25
     * @param int $code
26
     * @param string $message
27
     * @param array $curlInfo
28
     */
29
    public function __construct (int $code, string $message, array $curlInfo) {
30
        parent::__construct($message, $code);
31
        $this->curlInfo = $curlInfo;
32
    }
33
34
    /**
35
     * Asana context.
36
     *
37
     * @return array
38
     */
39
    public function asResponse () {
40
        return json_decode($this->getMessage(), true, JSON_BIGINT_AS_STRING | JSON_THROW_ON_ERROR);
41
    }
42
43
    /**
44
     * cURL context.
45
     *
46
     * @return array
47
     */
48
    public function getCurlInfo (): array {
49
        return $this->curlInfo;
50
    }
51
52
    /**
53
     * @param int $code
54
     * @return bool
55
     */
56
    final public function is (int $code): bool {
57
        return $this->code === $code;
58
    }
59
60
    /**
61
     * @return bool
62
     */
63
    final public function isCurl (): bool {
64
        return $this->code < 400;
65
    }
66
}