Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.15% |
75 / 78 |
|
91.67% |
11 / 12 |
CRAP | |
0.00% |
0 / 1 |
| Result | |
96.15% |
75 / 78 |
|
91.67% |
11 / 12 |
33 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setResponse | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| testMeta | |
100.00% |
32 / 32 |
|
100.00% |
1 / 1 |
9 | |||
| getRequest | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getResponse | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isStatus | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getEntity | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| getCollection | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| getData | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| getMeta | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| getIds | |
66.67% |
6 / 9 |
|
0.00% |
0 / 1 |
7.33 | |||
| setData | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BO\Zmsclient; |
| 4 | |
| 5 | use BO\Mellon\Valid; |
| 6 | use BO\Mellon\Validator; |
| 7 | use BO\Zmsentities\Metaresult; |
| 8 | use BO\Zmsentities\Collection\Base as BaseCollection; |
| 9 | use BO\Zmsentities\Schema\Entity; |
| 10 | use BO\Zmsentities\Schema\Factory; |
| 11 | use Psr\Http\Message\RequestInterface; |
| 12 | use Psr\Http\Message\ResponseInterface; |
| 13 | |
| 14 | /** |
| 15 | * Handle default response |
| 16 | */ |
| 17 | class Result |
| 18 | { |
| 19 | /** |
| 20 | * @var ResponseInterface |
| 21 | */ |
| 22 | protected $response; |
| 23 | |
| 24 | /** |
| 25 | * @var RequestInterface|null |
| 26 | */ |
| 27 | protected $request; |
| 28 | |
| 29 | /** |
| 30 | * @var Entity[]|null |
| 31 | */ |
| 32 | protected $data = null; |
| 33 | |
| 34 | /** |
| 35 | * @var Metaresult|null |
| 36 | */ |
| 37 | protected $meta = null; |
| 38 | |
| 39 | /** |
| 40 | * @param ResponseInterface $response |
| 41 | * @param RequestInterface|null $request (optional) reference for better error messages |
| 42 | */ |
| 43 | public function __construct( |
| 44 | ResponseInterface $response, |
| 45 | RequestInterface $request = null |
| 46 | ) { |
| 47 | $this->request = $request; |
| 48 | $this->response = $response; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Parse response and the object values |
| 53 | * @param ResponseInterface $response |
| 54 | * @return self |
| 55 | */ |
| 56 | public function setResponse(ResponseInterface $response) |
| 57 | { |
| 58 | $body = Validator::value((string) $response->getBody())->isJson(); |
| 59 | $this->testMeta($body, $response); |
| 60 | $result = $body->getValue(); |
| 61 | if (array_key_exists("data", $result)) { |
| 62 | $this->setData($result['data']); |
| 63 | } |
| 64 | return $this; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Test meta data on errors |
| 69 | * |
| 70 | * @param Valid $body |
| 71 | * @param ResponseInterface $response |
| 72 | * @throws Exception |
| 73 | */ |
| 74 | protected function testMeta($body, ResponseInterface $response) |
| 75 | { |
| 76 | if ($body->hasFailed()) { |
| 77 | $content = (string) $response->getBody(); |
| 78 | throw new Exception\ApiFailed( |
| 79 | 'API-Call failed, JSON parsing with error: ' . $body->getMessages() |
| 80 | . ' - Snippet: ' . substr(\strip_tags($content), 0, 2000) . '[...]', |
| 81 | $response, |
| 82 | $this->request |
| 83 | ); |
| 84 | } |
| 85 | $result = $body->getValue(); |
| 86 | if (!$result || !array_key_exists("meta", $result)) { |
| 87 | throw new Exception( |
| 88 | 'Missing "meta" value on result, API-Call failed.', |
| 89 | $response, |
| 90 | $this->request |
| 91 | ); |
| 92 | } |
| 93 | $entity = Factory::create($result['meta'])->getEntity(); |
| 94 | $this->meta = $entity; |
| 95 | if ($entity->error == true) { |
| 96 | $message = $entity->message ? $entity->message : $entity->exception; |
| 97 | $exception = new Exception( |
| 98 | 'API-Error: ' . $message, |
| 99 | $response, |
| 100 | $this->request |
| 101 | ); |
| 102 | if (isset($entity->trace)) { |
| 103 | $exception->trace = $entity['trace']; |
| 104 | } |
| 105 | $exception->originalMessage = $entity->message; |
| 106 | if (array_key_exists('data', $result)) { |
| 107 | $exception->data = $result['data']; |
| 108 | } |
| 109 | if (isset($entity->exception)) { |
| 110 | $exception->template = $entity->exception; |
| 111 | } |
| 112 | throw $exception; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Get the origin request |
| 118 | * |
| 119 | * @return RequestInterface|null |
| 120 | */ |
| 121 | public function getRequest() |
| 122 | { |
| 123 | return $this->request; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Get the origin response |
| 128 | * |
| 129 | * @return ResponseInterface |
| 130 | */ |
| 131 | public function getResponse() |
| 132 | { |
| 133 | return $this->response; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Get the origin response |
| 138 | * |
| 139 | * @return bool |
| 140 | */ |
| 141 | public function isStatus($statuscode) |
| 142 | { |
| 143 | return $this->getResponse()->getStatusCode() == $statuscode; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Description |
| 148 | * |
| 149 | * @return Entity|null|false |
| 150 | */ |
| 151 | public function getEntity() |
| 152 | { |
| 153 | $entity = null; |
| 154 | if (null !== $this->getData()) { |
| 155 | $data = $this->getData(); |
| 156 | $entity = reset($data); |
| 157 | } |
| 158 | return $entity; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Description |
| 163 | * |
| 164 | * @return BaseCollection|null |
| 165 | */ |
| 166 | public function getCollection() |
| 167 | { |
| 168 | $collection = null; |
| 169 | $entity = $this->getEntity(); |
| 170 | if (null !== $entity) { |
| 171 | $class = get_class($entity); |
| 172 | $alias = ucfirst(preg_replace('#^.*\\\#', '', $class) ?? ''); |
| 173 | $className = "\\BO\\Zmsentities\\Collection\\" . $alias . "List"; |
| 174 | $collection = new $className($this->getData()); |
| 175 | } |
| 176 | return $collection; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Description |
| 181 | * |
| 182 | * @return Entity[] |
| 183 | */ |
| 184 | public function getData() |
| 185 | { |
| 186 | if (null === $this->data) { |
| 187 | $this->setResponse($this->response); |
| 188 | } |
| 189 | return $this->data; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Description |
| 194 | * |
| 195 | * @return Metaresult|null |
| 196 | */ |
| 197 | public function getMeta() |
| 198 | { |
| 199 | if (null === $this->meta) { |
| 200 | $this->setResponse($this->response); |
| 201 | } |
| 202 | return $this->meta; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Get the list of IDs from the data |
| 207 | * |
| 208 | * @return string |
| 209 | */ |
| 210 | public function getIds() |
| 211 | { |
| 212 | $data = $this->getData(); |
| 213 | $idList = []; |
| 214 | |
| 215 | foreach ($data as $item) { |
| 216 | if (is_object($item) && method_exists($item, 'getId')) { |
| 217 | $idList[] = $item->getId(); |
| 218 | } elseif (is_array($item) && array_key_exists('id', $item)) { |
| 219 | $idList[] = $item['id']; |
| 220 | } else { |
| 221 | throw new \UnexpectedValueException('Item is neither array nor object with getId() method'); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | return implode(',', array_unique($idList)); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Set entity from response |
| 230 | * |
| 231 | * @param array $data |
| 232 | * |
| 233 | * @return self |
| 234 | */ |
| 235 | public function setData(array $data) |
| 236 | { |
| 237 | if (array_key_exists('$schema', $data)) { |
| 238 | $data = [$data]; |
| 239 | } |
| 240 | foreach ($data as $entityData) { |
| 241 | if (!array_key_exists('$schema', $entityData)) { |
| 242 | $entityData['$schema'] = $data[0]['$schema']; |
| 243 | } |
| 244 | $this->data[] = Factory::create($entityData)->getEntity(); |
| 245 | } |
| 246 | return $this; |
| 247 | } |
| 248 | } |