Skip to content

Commit 79f11ad

Browse files
authored
Merge pull request #43 from localheinz/feature/extract
Enhancement: Extract method which ensures result is or converts it to callable
2 parents 6dcd595 + 1ad4c09 commit 79f11ad

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

src/Client.php

+30-22
Original file line numberDiff line numberDiff line change
@@ -122,34 +122,42 @@ public function doSendRequest(RequestInterface $request)
122122
*/
123123
public function on(RequestMatcher $requestMatcher, $result)
124124
{
125-
$callable = null;
126-
127-
switch (true) {
128-
case is_callable($result):
129-
$callable = $result;
130-
131-
break;
132-
case $result instanceof ResponseInterface:
133-
$callable = function () use ($result) {
134-
return $result;
135-
};
136-
137-
break;
138-
case $result instanceof \Exception:
139-
$callable = function () use ($result) {
140-
throw $result;
141-
};
142-
143-
break;
144-
default:
145-
throw new \InvalidArgumentException('Result must be either a response, an exception, or a callable');
146-
}
125+
$callable = self::makeCallable($result);
126+
147127
$this->conditionalResults[] = [
148128
'matcher' => $requestMatcher,
149129
'callable' => $callable,
150130
];
151131
}
152132

133+
/**
134+
* @param ResponseInterface|Exception|ClientExceptionInterface|callable $result
135+
*
136+
* @throws \InvalidArgumentException
137+
*
138+
* @return callable
139+
*/
140+
private static function makeCallable($result)
141+
{
142+
if (is_callable($result)) {
143+
return $result;
144+
}
145+
146+
if ($result instanceof ResponseInterface) {
147+
return function () use ($result) {
148+
return $result;
149+
};
150+
}
151+
152+
if ($result instanceof \Exception) {
153+
return function () use ($result) {
154+
throw $result;
155+
};
156+
}
157+
158+
throw new \InvalidArgumentException('Result must be either a response, an exception, or a callable');
159+
}
160+
153161
/**
154162
* Adds an exception that will be thrown.
155163
*/

0 commit comments

Comments
 (0)