1 <?php
2 /**
3 * Copyright 2014 Klarna AB
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * File containing the ResponseValidator class.
18 */
19
20 namespace Klarna\Rest\Transport;
21
22 use Psr\Http\Message\ResponseInterface;
23
24 /**
25 * HTTP response validator helper class.
26 */
27 class ResponseValidator
28 {
29 /**
30 * HTTP response to validate against.
31 *
32 * @var ResponseInterface
33 */
34 protected $response;
35
36 /**
37 * Constructs a response validator instance.
38 *
39 * @param ResponseInterface $response Response to validate
40 */
41 public function __construct(ResponseInterface $response)
42 {
43 $this->response = $response;
44 }
45
46 /**
47 * Gets the response object.
48 *
49 * @return ResponseInterface
50 */
51 public function getResponse()
52 {
53 return $this->response;
54 }
55
56 /**
57 * Asserts the HTTP response status code.
58 *
59 * @param string|string[] $status Expected status code(s)
60 *
61 * @throws \RuntimeException If status code does not match
62 *
63 * @return self
64 */
65 public function status($status)
66 {
67 $httpStatus = (string) $this->response->getStatusCode();
68 if (is_array($status) && !in_array($httpStatus, $status)) {
69 throw new \RuntimeException(
70 "Unexpected response status code: {$httpStatus}"
71 );
72 }
73
74 if (is_string($status) && $httpStatus !== $status) {
75 throw new \RuntimeException(
76 "Unexpected response status code: {$httpStatus}"
77 );
78 }
79
80 return $this;
81 }
82
83 /**
84 * Asserts the Content-Type header.
85 *
86 * @param string $mediaType Expected media type
87 *
88 * @throws \RuntimeException If Content-Type header is missing
89 * @throws \RuntimeException If Content-Type header does not match
90 *
91 * @return self
92 */
93 public function contentType($mediaType)
94 {
95 if (!$this->response->hasHeader('Content-Type')) {
96 throw new \RuntimeException('Response is missing a Content-Type header');
97 }
98
99 $contentType = $this->response->getHeader('Content-Type');
100
101 if (!in_array($mediaType, $contentType)) {
102 throw new \RuntimeException(
103 'Unexpected Content-Type header received: ' . implode(',', $contentType)
104 );
105 }
106
107 return $this;
108 }
109
110 /**
111 * Get the decoded JSON response.
112 *
113 * @throws \RuntimeException If the response body is not in JSON format
114 * @throws \InvalidArgumentException If the JSON cannot be parsed
115 *
116 * @return array
117 */
118 public function getJson()
119 {
120 return \json_decode($this->response->getBody(), true);
121 }
122
123 /**
124 * Gets the Location header.
125 *
126 * @throws \RuntimeException If the Location header is missing
127 *
128 * @return string
129 */
130 public function getLocation()
131 {
132 if (!$this->response->hasHeader('Location')) {
133 throw new \RuntimeException('Response is missing a Location header');
134 }
135
136 return $this->response->getHeader('Location')[0];
137 }
138 }
139