Passed
Pull Request — master (#2)
by Andreas
11:41
created

XmlClient::getMessageFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Larium\Pay\Client;
6
7
use Http\Discovery\Psr17FactoryDiscovery;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
11
class XmlClient extends AbstractClient
12
{
13
    private array $headers = [];
14
15
    public function __construct(
16
        private readonly string $uri,
17
        array $options = []
18
    ) {
19
        $this->options = $options;
20
    }
21
22
    public function addHeader(string $name, string $value): void
23
    {
24
        $this->headers[$name] = $value;
25
    }
26
27
    /**
28
     * Post given xml string to remote gateway.
29
     *
30
     * @param string $xml
31
     * @return array @see self::resolveResponse
32
     */
33
    public function post(string $xml): array
34
    {
35
        $factory = Psr17FactoryDiscovery::findRequestFactory();
36
        $request = $factory->createRequest('POST', $this->uri, $this->headers, $xml);
0 ignored issues
show
Unused Code introduced by
The call to Psr\Http\Message\Request...erface::createRequest() has too many arguments starting with $this->headers. ( Ignorable by Annotation )

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

36
        /** @scrutinizer ignore-call */ 
37
        $request = $factory->createRequest('POST', $this->uri, $this->headers, $xml);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
37
38
        return $this->resolveResponse($this->sendRequest($request));
39
    }
40
41
    /**
42
     * Resolve the response from client.
43
     *
44
     * @param ResponseInterface $response
45
     * @return array An array with following values:
46
     *              'status'      : The Http status of response
47
     *              'headers'     : An array of response headers
48
     *              'body'        : The response string.
49
     *              'raw_response': The raw body response for logging purposes.
50
     *              'raw_request' : The raw body request for logging purposes.
51
     */
52
    protected function resolveResponse(ResponseInterface $response): array
53
    {
54
        $body = $response->getBody()->__toString();
55
56
        return [
57
            'status' => $response->getStatusCode(),
58
            'headers' => $response->getHeaders(),
59
            'body' => $body,
60
            'raw_response' => $body,
61
            'raw_request' => $this->rawRequest,
62
        ];
63
    }
64
65
    protected function authenticate(RequestInterface $request): RequestInterface
66
    {
67
        return $request;
68
    }
69
}
70