Response::parseResponse()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 0
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @category    Brownie/BpmOnline
4
 * @author      Brownie <[email protected]>
5
 * @license     https://opensource.org/licenses/MIT
6
 */
7
8
namespace Brownie\BpmOnline\DataService;
9
10
use Brownie\BpmOnline\Exception\ValidateException;
11
use Brownie\Util\StorageArray;
12
13
/**
14
 * Common response class when requesting contract execution.
15
 *
16
 * @method string   getRawResponse()                    Returns raw response body.
17
 * @method Response setJsonResponse($jsonResponse)      Sets json response.
18
 * @method array    getJsonResponse()                   Returns json response.
19
 */
20
class Response extends StorageArray
21
{
22
23
    /**
24
     * List of supported fields.
25
     *
26
     * @var array
27
     */
28
    protected $fields = [
29
        /**
30
         * Raw response.
31
         */
32
        'rawResponse' => '',
33
34
        /**
35
         * Json response.
36
         */
37
        'jsonResponse' => null,
38
    ];
39
40
    /**
41
     * Sets the input values.
42
     *
43
     * @param string    $rawResponse    Raw response.
44
     */
45 14
    public function __construct($rawResponse)
46
    {
47 14
        parent::__construct([
48 14
            'rawResponse' => $rawResponse
49
        ]);
50 14
    }
51
52
    /**
53
     * Parse response contract execution.
54
     *
55
     * @throws ValidateException
56
     */
57 9
    private function parseResponse()
58
    {
59 9
        $jsonResponse = json_decode($this->getRawResponse(), true);
60 9
        if (JSON_ERROR_NONE != json_last_error())
61
        {
62 1
            throw new ValidateException(
63 1
                'Invalid response: ' . substr($this->getRawResponse(), 0, 30) . '...'
64
            );
65
        }
66 8
        if (isset($jsonResponse['ResponseStatus'])) {
67 1
            $jsonResponse['responseStatus'] = $jsonResponse['ResponseStatus'];
68
        }
69 8
        $this->setJsonResponse($jsonResponse);
70 8
    }
71
72
    /**
73
     * Returns the value of the response contract execution.
74
     *
75
     * @param string    $valueName      Value name.
76
     *
77
     * @return mixed
78
     *
79
     * @throws ValidateException
80
     */
81 9
    protected function getRequestValue($valueName)
82
    {
83 9
        $this->parseResponse();
84 8
        $response = $this->getJsonResponse();
85 8
        if (!isset($response[$valueName])) {
86 1
            throw new ValidateException('Undefined request value name: ' . $valueName);
87
        }
88 7
        return $response[$valueName];
89
    }
90
91
    /**
92
     * Returns the number of records affected.
93
     *
94
     * @return int
95
     *
96
     * @throws ValidateException
97
     */
98 7
    public function getRowsAffected()
99
    {
100 7
        return $this->getRequestValue('rowsAffected');
101
    }
102
103
    /**
104
     * Returns the value of the nextPrcElReady field.
105
     *
106
     * @return bool
107
     *
108
     * @throws ValidateException
109
     */
110 7
    public function getNextPrcElReady()
111
    {
112 7
        return $this->getRequestValue('nextPrcElReady');
113
    }
114
115
    /**
116
     * Returns the value of the success field.
117
     *
118
     * @return bool
119
     *
120
     * @throws ValidateException
121
     */
122 6
    public function getSuccess()
123
    {
124 6
        return $this->getRequestValue('success');
125
    }
126
127
    /**
128
     * Returns error message.
129
     *
130
     * @throws ValidateException
131
     */
132 2
    public function getErrorMessage()
133
    {
134 2
        return $this->getRequestValue('responseStatus')['Message'];
135
    }
136
}
137