Rest::request()   A
last analyzed

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 Trait
6
 * @category    Ticaje
7
 * @author      Max Demian <[email protected]>
8
 */
9
10
namespace Ticaje\AConnector\Traits\Gateway\Client;
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
15
use Ticaje\AConnector\Interfaces\Protocol\RestClientInterface;
16
use Ticaje\Contract\Patterns\Interfaces\Decorator\ResponderInterface;
17
18
/**
19
 * Trait Rest
20
 * @package Ticaje\AConnector\Traits\Gateway\Client
21
 * This trait prevents code duplication at the time that fits right with service contracts
22
 * Uses design-by-contract pattern in order to guarantee business rules
23
 */
24
trait Rest
25
{
26
    /**
27
     * @param $verb
28
     * @param $endpoint
29
     * @param array $headers
30
     * @param array $params
31
     * @return array
32
     */
33
    public function requestAsync($verb, $endpoint, array $headers = [], array $params)
34
    {
35
        $result = [];
36
        try {
37
            $promise = $this->client->requestAsync(
38
                $verb,
39
                $endpoint,
40
                [
41
                    '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

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

52
                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...
53
                    // Missing implementation
54
                }
55
            );
56
            $promise->wait();
57
        } catch (\Exception $exception) {
58
            $result[ResponderInterface::CONTENT_KEY] = null;
59
            $result[ResponderInterface::SUCCESS_KEY] = false;
60
            $result[ResponderInterface::MESSAGE_KEY] = $exception->getMessage();
61
        }
62
        return $this->responder->process($result);
63
    }
64
65
    /**
66
     * @param $verb
67
     * @param $endpoint
68
     * @param array $headers
69
     * @param array $params
70
     * @return array
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' => $this->generateHeaders($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 $this->responder->process($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