Completed
Pull Request — master (#14)
by Márk
05:00
created

ErrorPlugin::transformResponseToException()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 6
Ratio 50 %

Code Coverage

Tests 6
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 6
loc 12
ccs 6
cts 6
cp 1
rs 8.8571
cc 5
eloc 6
nc 3
nop 2
crap 5
1
<?php
2
3
namespace Http\Client\Common\Plugin;
4
5
use Http\Client\Common\Exception\ClientErrorException;
6
use Http\Client\Common\Exception\ServerErrorException;
7
use Http\Client\Common\Plugin;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
11
/**
12
 * Throw exception when the response of a request is not acceptable.
13
 *
14
 * By default an exception will be thrown for all status codes from 400 to 599.
15
 *
16
 * @author Joel Wurtz <[email protected]>
17
 *
18
 * TODO: make class final in version 2.0, once plugins does not extend it anymore.
19
 */
20
/*final*/ class ErrorPlugin implements Plugin
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25 3
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
26
    {
27 3
        $promise = $next($request);
28
29 3
        return $promise->then(function (ResponseInterface $response) use ($request) {
30 3
            return $this->transformResponseToException($request, $response);
31 3
        });
32
    }
33
34
    /**
35
     * Transform response to an error if possible.
36
     *
37
     * @param RequestInterface  $request  Request of the call
38
     * @param ResponseInterface $response Response of the call
39
     *
40
     * @throws ClientErrorException If response status code is a 4xx
41
     * @throws ServerErrorException If response status code is a 5xx
42
     *
43
     * @return ResponseInterface If status code is not in 4xx or 5xx return response
44
     */
45 3
    protected function transformResponseToException(RequestInterface $request, ResponseInterface $response)
46
    {
47 3 View Code Duplication
        if ($response->getStatusCode() >= 400 && $response->getStatusCode() < 500) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48 1
            throw new ClientErrorException($response->getReasonPhrase(), $request, $response);
49
        }
50
51 2 View Code Duplication
        if ($response->getStatusCode() >= 500 && $response->getStatusCode() < 600) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52 1
            throw new ServerErrorException($response->getReasonPhrase(), $request, $response);
53
        }
54
55 1
        return $response;
56
    }
57
}
58