Rest::request()   A
last analyzed

Complexity

Conditions 2
Paths 4

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 15
c 2
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 Trait
6
 * @category    Ticaje
7
 * @package     Ticaje_Connector
8
 * @author      Hector Luis Barrientos <[email protected]>
9
 */
10
11
namespace Ticaje\Connector\Traits\Gateway\Client;
12
13
use Psr\Http\Message\ResponseInterface;
14
use RuntimeException;
15
16
use Ticaje\Connector\Interfaces\Protocol\RestClientInterface;
17
use Ticaje\Contract\Patterns\Interfaces\Decorator\ResponderInterface;
18
19
/**
20
 * Trait Rest
21
 * @package Ticaje\Connector\Traits\Gateway\Client
22
 * This trait prevents code duplication at the time that fits right with service contracts
23
 * Uses design-by-contract pattern in order to guarantee business rules
24
 */
25
trait Rest
26
{
27
    /**
28
     * @param $verb
29
     * @param $endpoint
30
     * @param array $headers
31
     * @param array $params
32
     * @return array
33
     */
34
    public function requestAsync($verb, $endpoint, array $headers = [], array $params)
35
    {
36
        $result = [];
37
        try {
38
            $promise = $this->client->requestAsync(
39
                $verb,
40
                $endpoint,
41
                [
42
                    'headers' => $this->generateHeaders($headers),
0 ignored issues
show
Bug introduced by
It seems like generateHeaders() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

42
                    'headers' => $this->/** @scrutinizer ignore-call */ generateHeaders($headers),
Loading history...
43
                    $this->getFormRequestKey($verb, $headers) => $params
44
                ]
45
            );
46
            $promise->then(
47
                function (ResponseInterface $response) use (&$result) {
48
                    $result[ResponderInterface::CONTENT_KEY] = $response->getBody()->getContents();
49
                    $result[ResponderInterface::SUCCESS_KEY] = true;
50
                    $result[ResponderInterface::MESSAGE_KEY] = 'Response correctly received'; // Perhaps normalize this message
51
                    return $result;
52
                },
53
                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

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