diff --git a/src/Client.php b/src/Client.php index e3a5b31..a9ae12f 100644 --- a/src/Client.php +++ b/src/Client.php @@ -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. */