Passed
Push — master ( 8aab84...b5d78e )
by Alexandr
01:40
created

ApiClient::preprocessResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace TheHorhe\ApiClient;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Psr7\Request;
7
use function GuzzleHttp\Psr7\try_fopen;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
11
/**
12
 * TODO: ApiClientInterface
13
 *
14
 * Class ApiClient
15
 * @package TheHorhe\ApiClient
16
 */
17
class ApiClient
18
{
19
    /**
20
     * @param MethodInterface $method
21
     * @return mixed
22
     */
23
    public function executeMethod(MethodInterface $method)
24
    {
25
        $this->preprocessMethod($method);
26
27
        $client = $this->createClient();
28
        $request = $this->buildRequest($method);
29
30
        try {
31
            $response = $client->send($request, [
32
                'timeout' => $method->getTimeout()
33
            ]);
34
35
            $this->preprocessResponse($response);
36
37
            $result = $method->processResponse($response);
38
            return $result;
39
        } catch (\Throwable $exception) {
40
            $method->handleException($exception);
41
        }
42
    }
43
44
    /**
45
     * TODO: Method body
46
     * Parameters: Get -> query / Post -> body
47
     *
48
     * @param MethodInterface $method
49
     * @return RequestInterface
50
     */
51
    protected function buildRequest(MethodInterface $method)
52
    {
53
        $parameters = http_build_query($method->getQueryParameters());
54
55
        $request = new Request(
56
            $method->getHttpMethod(),
57
            sprintf('%s://%s%s?%s', $method->getScheme(), $method->getHost(), $method->getMethodUrl(), $parameters),
58
            $method->getHeaders(),
59
            $method->getRawBody()
60
        );
61
62
        return $request;
63
    }
64
65
    /**
66
     * @return Client
67
     */
68
    protected function createClient()
69
    {
70
        return new Client();
71
    }
72
73
    /**
74
     * Methods allows add some common operations, e.g. add body parameter to the body of all methods executed by this client.
75
     *
76
     * @param MethodInterface $method
77
     */
78
    protected function preprocessMethod(MethodInterface $method)
0 ignored issues
show
Unused Code introduced by
The parameter $method is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

78
    protected function preprocessMethod(/** @scrutinizer ignore-unused */ MethodInterface $method)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
    {
80
        return;
81
    }
82
83
    /**
84
     * Use this to modify response before processing
85
     *
86
     * @param ResponseInterface $response
87
     */
88
    protected function preprocessResponse(ResponseInterface $response)
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

88
    protected function preprocessResponse(/** @scrutinizer ignore-unused */ ResponseInterface $response)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
89
    {
90
        return;
91
    }
92
}