Completed
Push — master ( 96ecdf...475a08 )
by Andreas
02:03
created

XmlClient   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 62
ccs 0
cts 30
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A post() 0 7 1
A getMessageFactory() 0 4 1
A resolveResponse() 0 12 1
A authenticate() 0 4 1
1
<?php
2
3
namespace Larium\Pay\Client;
4
5
use Psr\Http\Message\RequestInterface;
6
use Psr\Http\Message\ResponseInterface;
7
use Http\Discovery\HttpClientDiscovery;
8
use Http\Discovery\MessageFactoryDiscovery;
9
10
class XmlClient extends AbstractClient
11
{
12
    private $uri;
13
14
    private $rawRequest;
15
16
    private $options;
17
18
    public function __construct($uri, array $options = [])
19
    {
20
        $this->uri = $uri;
21
        $this->options = $options;
22
    }
23
24
    /**
25
     * Post given xml string to remote gateway.
26
     *
27
     * @param string $xml
28
     * @return array @see self::resolveResponse
29
     */
30
    public function post($xml)
31
    {
32
        $factory = $this->getMessageFactory();
33
        $request = $factory->createRequest('POST', $this->uri, [], $xml);
34
35
        return $this->resolveResponse($this->sendRequest($request));
36
    }
37
38
    private function getMessageFactory()
39
    {
40
        return MessageFactoryDiscovery::find();
41
    }
42
43
    /**
44
     * Resolve the response from client.
45
     *
46
     * @param ResponseInterface $response
47
     * @return array An array with following values:
48
     *              'status'      : The Http status of response
49
     *              'headers'     : An array of response headers
50
     *              'body'        : The response string.
51
     *              'raw_response': The raw body response for logging purposes.
52
     *              'raw_request' : The raw body request for logging purposes.
53
     */
54
    protected function resolveResponse(ResponseInterface $response)
55
    {
56
        $body = $response->getBody()->__toString();
57
58
        return array(
59
            'status' => $response->getStatusCode(),
60
            'headers' => $response->getHeaders(),
61
            'body' => $body,
62
            'raw_response' => $body,
63
            'raw_request' => $this->rawRequest,
64
        );
65
    }
66
67
    protected function authenticate(RequestInterface $request)
68
    {
69
        return $request;
70
    }
71
}
72