Passed
Push — master ( 7f9295...bd7035 )
by Oss
01:56
created

Response::getSuccess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 11
    public function __construct($rawResponse)
46
    {
47 11
        parent::__construct([
48 11
            'rawResponse' => $rawResponse
49
        ]);
50 11
    }
51
52
    /**
53
     * Parse response contract execution.
54
     *
55
     * @throws ValidateException
56
     */
57 7
    private function parseResponse()
58
    {
59 7
        $jsonResponse = json_decode($this->getRawResponse(), true);
60 7
        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 6
        $this->setJsonResponse($jsonResponse);
67 6
    }
68
69
    /**
70
     * Returns the value of the response contract execution.
71
     *
72
     * @param string    $valueName      Value name.
73
     *
74
     * @return mixed
75
     *
76
     * @throws ValidateException
77
     */
78 7
    protected function getRequestValue($valueName)
79
    {
80 7
        $this->parseResponse();
81 6
        $response = $this->getJsonResponse();
82 6
        if (!isset($response[$valueName])) {
83 1
            throw new ValidateException('Undefined request value name: ' . $valueName);
84
        }
85 5
        return $response[$valueName];
86
    }
87
88
    /**
89
     * Returns the number of records affected.
90
     *
91
     * @return int
92
     *
93
     * @throws ValidateException
94
     */
95 6
    public function getRowsAffected()
96
    {
97 6
        return $this->getRequestValue('rowsAffected');
98
    }
99
100
    /**
101
     * Returns the value of the nextPrcElReady field.
102
     *
103
     * @return bool
104
     *
105
     * @throws ValidateException
106
     */
107 6
    public function getNextPrcElReady()
108
    {
109 6
        return $this->getRequestValue('nextPrcElReady');
110
    }
111
112
    /**
113
     * Returns the value of the success field.
114
     *
115
     * @return bool
116
     *
117
     * @throws ValidateException
118
     */
119 5
    public function getSuccess()
120
    {
121 5
        return $this->getRequestValue('success');
122
    }
123
124
    /**
125
     * Returns error message.
126
     *
127
     * @throws ValidateException
128
     */
129 1
    public function getErrorMessage()
130
    {
131 1
        return $this->getRequestValue('responseStatus')['Message'];
132
    }
133
}
134