Completed
Push — guzzle-5 ( 0ecdde...1d908f )
by Harry
04:32
created

RequestException::create()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4.0039

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 27
ccs 15
cts 16
cp 0.9375
rs 8.5806
c 2
b 0
f 0
cc 4
eloc 19
nc 3
nop 3
crap 4.0039
1
<?php
2
/*
3
 * This file is part of Guzzle HTTP JSON-RPC
4
 *
5
 * Copyright (c) 2014 Nature Delivered Ltd. <http://graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @see  http://github.com/graze/guzzle-jsonrpc/blob/master/LICENSE
11
 * @link http://github.com/graze/guzzle-jsonrpc
12
 */
13
namespace Graze\GuzzleHttp\JsonRpc\Exception;
14
15
use Exception;
16
use Graze\GuzzleHttp\JsonRpc\Message\RequestInterface;
17
use Graze\GuzzleHttp\JsonRpc\Message\ResponseInterface;
18
use GuzzleHttp\Message\RequestInterface as HttpRequestInterface;
19
use GuzzleHttp\Message\ResponseInterface as HttpResponseInterface;
20
use GuzzleHttp\Exception\RequestException as HttpRequestException;
21
22
class RequestException extends HttpRequestException
23
{
24
    /**
25
     * {@inheritdoc}
26
     *
27
     * @param HttpRequestInterface  $request  Request
28
     * @param HttpResponseInterface $response Response received
0 ignored issues
show
Documentation introduced by
Should the type for parameter $response not be null|HttpResponseInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
29
     * @param \Exception        $previous Previous exception
0 ignored issues
show
Documentation introduced by
Should the type for parameter $previous not be null|Exception?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
30
     *
31
     * @return HttpRequestException
32
     */
33 9
    public static function create(
34
        HttpRequestInterface $request,
35
        HttpResponseInterface $response = null,
36
        Exception $previous = null
37
    ) {
38 9
        if ($request instanceof RequestInterface && $response instanceof ResponseInterface) {
39 9
            static $clientErrorCodes = [-32600, -32601, -32602, -32700];
40
41 9
            $errorCode = $response->getRpcErrorCode();
42 9
            if (in_array($errorCode, $clientErrorCodes)) {
43 4
                $label = 'Client RPC error response';
44 4
                $className = __NAMESPACE__ . '\\ClientException';
45 4
            } else {
46 5
                $label = 'Server RPC error response';
47 5
                $className = __NAMESPACE__ . '\\ServerException';
48
            }
49
50 9
            $message = $label . ' [url] ' . $request->getUrl()
51 9
                . ' [method] ' . $request->getRpcMethod()
52 9
                . ' [error code] ' . $errorCode
53 9
                . ' [error message] ' . $response->getRpcErrorMessage();
54
55 9
            return new $className($message, $request, $response, $previous);
56
        }
57
58
        return parent::create($request, $response, $previous);
59
    }
60
}
61