Completed
Push — master ( f8145c...2f8080 )
by Andreas
04:16 queued 02:58
created

XmlClient   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 65
ccs 0
cts 34
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addHeader() 0 4 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 $headers = [];
15
16
    public function __construct($uri, array $options = [])
17
    {
18
        $this->uri = $uri;
19
        $this->options = $options;
20
    }
21
22
    public function addHeader($name, $value)
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($xml)
34
    {
35
        $factory = $this->getMessageFactory();
36
        $request = $factory->createRequest('POST', $this->uri, $this->headers, $xml);
37
38
        return $this->resolveResponse($this->sendRequest($request));
39
    }
40
41
    private function getMessageFactory()
42
    {
43
        return MessageFactoryDiscovery::find();
44
    }
45
46
    /**
47
     * Resolve the response from client.
48
     *
49
     * @param ResponseInterface $response
50
     * @return array An array with following values:
51
     *              'status'      : The Http status of response
52
     *              'headers'     : An array of response headers
53
     *              'body'        : The response string.
54
     *              'raw_response': The raw body response for logging purposes.
55
     *              'raw_request' : The raw body request for logging purposes.
56
     */
57
    protected function resolveResponse(ResponseInterface $response)
58
    {
59
        $body = $response->getBody()->__toString();
60
61
        return array(
62
            'status' => $response->getStatusCode(),
63
            'headers' => $response->getHeaders(),
64
            'body' => $body,
65
            'raw_response' => $body,
66
            'raw_request' => $this->rawRequest,
67
        );
68
    }
69
70
    protected function authenticate(RequestInterface $request)
71
    {
72
        return $request;
73
    }
74
}
75