Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 30 additions & 22 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,34 +122,42 @@ public function doSendRequest(RequestInterface $request)
*/
public function on(RequestMatcher $requestMatcher, $result)
{
$callable = null;

switch (true) {
case is_callable($result):
$callable = $result;

break;
case $result instanceof ResponseInterface:
$callable = function () use ($result) {
return $result;
};

break;
case $result instanceof \Exception:
$callable = function () use ($result) {
throw $result;
};

break;
default:
throw new \InvalidArgumentException('Result must be either a response, an exception, or a callable');
}
$callable = self::makeCallable($result);

$this->conditionalResults[] = [
'matcher' => $requestMatcher,
'callable' => $callable,
];
}

/**
* @param ResponseInterface|Exception|ClientExceptionInterface|callable $result
*
* @throws \InvalidArgumentException
*
* @return callable
*/
private static function makeCallable($result)
{
if (is_callable($result)) {
return $result;
}

if ($result instanceof ResponseInterface) {
return function () use ($result) {
return $result;
};
}

if ($result instanceof \Exception) {
return function () use ($result) {
throw $result;
};
}

throw new \InvalidArgumentException('Result must be either a response, an exception, or a callable');
}

/**
* Adds an exception that will be thrown.
*/
Expand Down