1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JiraRestApi; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
6
|
|
|
use Psr\Http\Message\StreamInterface; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
|
9
|
|
|
class JiraClientResponse |
10
|
|
|
{ |
11
|
|
|
protected $originalResponse; |
12
|
|
|
protected $response; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* raw response |
16
|
|
|
*/ |
17
|
|
|
protected $raw; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* interpreted response array |
21
|
|
|
*/ |
22
|
|
|
protected $rawData = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* error code if available |
26
|
|
|
*/ |
27
|
|
|
protected $code; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* error message if present |
31
|
|
|
*/ |
32
|
|
|
protected $error; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var LoggerInterface |
36
|
|
|
*/ |
37
|
|
|
protected $log; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* AbstractResponse constructor. |
41
|
|
|
* @param ResponseInterface $raw |
42
|
|
|
*/ |
43
|
|
|
public function __construct(ResponseInterface $raw, LoggerInterface $logger) |
44
|
|
|
{ |
45
|
|
|
$this->raw = $raw; |
46
|
|
|
$this->log = $logger; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
|
|
public function parse() |
53
|
|
|
{ |
54
|
|
|
/** @var StreamInterface $body */ |
55
|
|
|
$body = $this->raw->getBody(); |
56
|
|
|
$content = (string) $body; |
57
|
|
|
$content = json_decode($content, true); |
58
|
|
|
|
59
|
|
|
$this->code = $this->raw->getStatusCode(); |
60
|
|
|
|
61
|
|
|
if (in_array($this->code, [200, 201, 204])) { |
62
|
|
|
$this->rawData = $content; |
|
|
|
|
63
|
|
|
} else { |
64
|
|
|
$this->error = $content; |
65
|
|
|
$this->log->error('JiraRestApi parse response error: ', [$this->error]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
public function getRawData() |
75
|
|
|
{ |
76
|
|
|
return $this->rawData; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param $rawData |
81
|
|
|
*/ |
82
|
|
|
public function setRawData(array $rawData) |
83
|
|
|
{ |
84
|
|
|
$this->rawData = array_merge_recursive($this->rawData, $rawData); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return ResponseInterface |
89
|
|
|
*/ |
90
|
|
|
public function getRaw() |
91
|
|
|
{ |
92
|
|
|
return $this->raw; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return ResponseInterface |
97
|
|
|
*/ |
98
|
|
|
public function getCode() |
99
|
|
|
{ |
100
|
|
|
return $this->code; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return ResponseInterface |
105
|
|
|
*/ |
106
|
|
|
public function getError() |
107
|
|
|
{ |
108
|
|
|
return $this->error; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return ResponseInterface |
113
|
|
|
*/ |
114
|
|
|
public function hasErrors() |
115
|
|
|
{ |
116
|
|
|
return !empty($this->error); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param $message |
121
|
|
|
* |
122
|
|
|
* @return $this |
123
|
|
|
*/ |
124
|
|
|
public function setError($message) |
125
|
|
|
{ |
126
|
|
|
if(is_array($this->error) && isset($this->error['errorMessages'])) { |
127
|
|
|
$this->error['errorMessages'][] = $message; |
128
|
|
|
} else { |
129
|
|
|
$this->error = ['errorMessages' => [$message]]; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param $name |
137
|
|
|
* @param $args |
138
|
|
|
* @return mixed |
139
|
|
|
* @throws \Exception |
140
|
|
|
*/ |
141
|
|
|
public function __call($name, $args) |
142
|
|
|
{ |
143
|
|
|
$key = strtolower(str_replace('get', '', $name)); |
144
|
|
|
|
145
|
|
|
if (isset($this->rawData[$key])) { |
146
|
|
|
return $this->rawData[$key]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
throw new \Exception('JiraApi: unknown field ' . $key); |
150
|
|
|
} |
151
|
|
|
} |
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..