ConduitResponse::processRawResponse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php namespace Phabricator\Response;
2
3
use BuildR\Foundation\Exception\RuntimeException;
4
5
/**
6
 * Object representation of a conduit API response
7
 *
8
 * Phabricator PHP API
9
 *
10
 * @author Zoltán Borsos <[email protected]>
11
 * @package Phabricator
12
 * @subpackage Response
13
 *
14
 * @copyright    Copyright 2016, Zoltán Borsos.
15
 * @license      https://github.com/Zolli/Phabricator-PHP-API/blob/master/LICENSE.md
16
 * @link         https://github.com/Zolli/Phabricator-PHP-API
17
 */
18
class ConduitResponse {
19
20
    /**
21
     * The raw result data
22
     *
23
     * @type array
24
     */
25
    protected $result;
26
27
    /**
28
     * ConduitResponse constructor.
29
     *
30
     * @param string $result Raw JSON response
31
     */
32 2
    public function __construct($result) {
33 2
        $this->result = $this->processRawResponse($result);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->processRawResponse($result) of type * is incompatible with the declared type array of property $result.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
34 2
    }
35
36
    /**
37
     * Process the raw JSON response, given from conduit.
38
     * Throw an exception when given JSON is malformed
39
     *
40
     * @param $rawResult
41
     *
42
     * @throws \BuildR\Foundation\Exception\RuntimeException
43
     *
44
     * @return mixed
45
     */
46 4
    protected function processRawResponse($rawResult) {
47 4
        $result = json_decode($rawResult, TRUE);
48
49 4
        if(json_last_error() !== JSON_ERROR_NONE) {
50 1
            throw new RuntimeException('Malformed JSON response! Message: ' . json_last_error_msg());
51
        }
52
53 3
        return $result;
54
    }
55
56
    /**
57
     * Determines that the current response is successful or not
58
     *
59
     * @return bool
60
     */
61 2
    public function isSuccessful() {
62 2
        return ($this->result['error_code'] === NULL && $this->result['error_info'] === NULL) ? TRUE : FALSE;
63
    }
64
65
    /**
66
     * Determines that the current response is failed or not
67
     *
68
     * @return bool
69
     */
70 2
    public function isFailed() {
71 2
        return !$this->isSuccessful();
72
    }
73
74
    /**
75
     * Returns the error information from the response. Returns an empty string when
76
     * the API is not provide textual information
77
     *
78
     * @return string
79
     */
80 2
    public function getErrorInfo() {
81 2
        return (!empty($this->result['error_info'])) ? $this->result['error_info'] : '';
82
    }
83
84
    /**
85
     * Returns the error code from the API response, if tha request is not successes
86
     * This is not valid "error code". The conduit API returns short (constant like)
87
     * text, that represents the error, like: ERR_CERT_INVALID
88
     *
89
     * @return string
90
     */
91 2
    public function getErrorCode() {
92 2
        return (!empty($this->result['error_code'])) ? $this->result['error_code'] : '';
93
    }
94
95
    /**
96
     * Returns the API response
97
     *
98
     * @return array
99
     */
100 2
    public function getResult() {
101 2
        return $this->result['result'];
102
    }
103
104
}