Passed
Push — master ( 59a2ef...64aade )
by Hector Luis
02:16
created

Guzzle::request()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 20
rs 9.7666
cc 2
nc 4
nop 4
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * Gateway Client Class
6
 * @category    Ticaje
7
 * @author      Max Demian <[email protected]>
8
 */
9
10
namespace Ticaje\AConnector\Gateway\Implementations\Client\Rest;
11
12
use Psr\Http\Message\ResponseInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ResponseInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use RuntimeException;
14
use Ticaje\AConnector\Interfaces\Implementors\Client\Rest\RestClientImplementorInterface;
15
use Ticaje\AConnector\Interfaces\Protocol\RestClientInterface;
16
use Ticaje\Contract\Patterns\Interfaces\Decorator\ResponderInterface;
17
18
/**
19
 * Class Guzzle
20
 * @package Ticaje\AConnector\Gateway\Implementations\Client\Rest
21
 */
22
class Guzzle implements RestClientImplementorInterface, RestClientInterface
23
{
24
    private $client;
25
26
    /**
27
     * @inheritDoc
28
     */
29
    public function setClient($client)
30
    {
31
        $this->client = $client;
32
    }
33
34
    /**
35
     * @inheritDoc
36
     */
37
    public function requestAsync($verb, $endpoint, array $headers = [], array $params)
38
    {
39
        $result = [];
40
        try {
41
            $promise = $this->client->requestAsync(
42
                $verb,
43
                $endpoint,
44
                [
45
                    'headers' => $this->generateHeaders($headers),
0 ignored issues
show
Bug introduced by
The method generateHeaders() does not exist on Ticaje\AConnector\Gatewa...ions\Client\Rest\Guzzle. ( Ignorable by Annotation )

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

45
                    'headers' => $this->/** @scrutinizer ignore-call */ generateHeaders($headers),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
46
                    $this->getFormRequestKey($verb, $headers) => $params
47
                ]
48
            );
49
            $promise->then(
50
                function (ResponseInterface $response) use (&$result) {
51
                    $result[ResponderInterface::CONTENT_KEY] = $response->getBody()->getContents();
52
                    $result[ResponderInterface::SUCCESS_KEY] = true;
53
                    $result[ResponderInterface::MESSAGE_KEY] = 'Response correctly received'; // Perhaps normalize this message
54
                    return $result;
55
                },
56
                function (RuntimeException $exception) {
0 ignored issues
show
Unused Code introduced by
The parameter $exception 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

56
                function (/** @scrutinizer ignore-unused */ RuntimeException $exception) {

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...
57
                    // Missing implementation
58
                }
59
            );
60
            $promise->wait();
61
        } catch (\Exception $exception) {
62
            $result[ResponderInterface::CONTENT_KEY] = null;
63
            $result[ResponderInterface::SUCCESS_KEY] = false;
64
            $result[ResponderInterface::MESSAGE_KEY] = $exception->getMessage();
65
        }
66
        return $result;
67
    }
68
69
    /**
70
     * @inheritDoc
71
     */
72
    public function request($verb, $endpoint, array $headers = [], array $params = [])
73
    {
74
        $result = [];
75
        try {
76
            $result[ResponderInterface::CONTENT_KEY] = $this->client->request(
77
                $verb,
78
                $endpoint,
79
                [
80
                    'headers' => $headers,
81
                    $this->getFormRequestKey($verb, $headers) => $params
82
                ]
83
            )->getBody()->getContents();
84
            $result[ResponderInterface::SUCCESS_KEY] = true;
85
            $result[ResponderInterface::MESSAGE_KEY] = 'Response correctly received'; // Perhaps normalize this message
86
        } catch (\Exception $exception) {
87
            $result[ResponderInterface::CONTENT_KEY] = null;
88
            $result[ResponderInterface::SUCCESS_KEY] = false;
89
            $result[ResponderInterface::MESSAGE_KEY] = $exception->getMessage();
90
        }
91
        return $result;
92
    }
93
94
    /**
95
     * @param $verb
96
     * @param $headers
97
     * @return string
98
     */
99
    private function getFormRequestKey($verb, $headers)
100
    {
101
        // The values should also be constantized
102
        $key = $verb ? 'query' : 'form_params';
103
        $key = $headers[RestClientInterface::CONTENT_TYPE_KEY] == RestClientInterface::CONTENT_TYPE_JSON ? 'json' : $key;
104
        return $key;
105
    }
106
}
107