Completed
Push — master ( ad0b2c...f5bffe )
by Joachim
05:31
created

Client   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 110
ccs 0
cts 31
cp 0
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
B doRequest() 0 25 2
A getRequest() 0 4 1
A getResponse() 0 4 1
1
<?php
2
namespace Loevgaard\Consignor\ShipmentServer\Client;
3
4
use Assert\Assert;
5
use Http\Client\HttpClient;
6
use Http\Discovery\HttpClientDiscovery;
7
use Http\Discovery\MessageFactoryDiscovery;
8
use Http\Message\RequestFactory;
9
use Loevgaard\Consignor\ShipmentServer\Request\RequestInterface;
10
use Psr\Http\Message\RequestInterface as PsrRequestInterface;
11
use Psr\Http\Message\ResponseInterface;
12
13
class Client
14
{
15
    /**
16
     * This URL is used for testing purposes
17
     *
18
     * @var string
19
     */
20
    protected $testServerUrl = 'http://sstest.consignor.com/ship/ShipmentServerModule.dll';
21
22
    /**
23
     * This URL is used for production requests
24
     *
25
     * @var string
26
     */
27
    protected $productionServerUrl = 'https://www.shipmentserver.com/ship/ShipmentServerModule.dll';
28
29
    /**
30
     * @var string
31
     */
32
    protected $actor;
33
34
    /**
35
     * @var string
36
     */
37
    protected $key;
38
39
    /**
40
     * Should be either 'dev' or 'production'
41
     *
42
     * @var string
43
     */
44
    protected $environment;
45
46
    /**
47
     * @var HttpClient
48
     */
49
    protected $httpClient;
50
51
    /**
52
     * @var RequestFactory
53
     */
54
    protected $requestFactory;
55
56
    /**
57
     * This is the last request
58
     *
59
     * @var PsrRequestInterface|null
60
     */
61
    protected $request;
62
63
    /**
64
     * This is the last response
65
     *
66
     * @var ResponseInterface|null
67
     */
68
    protected $response;
69
70
    public function __construct(string $actor, string $key, HttpClient $httpClient = null, RequestFactory $requestFactory = null, string $environment = 'production')
71
    {
72
        $this->actor = $actor;
73
        $this->key = $key;
74
        $this->httpClient = $httpClient ?: HttpClientDiscovery::find();
75
        $this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find();
76
77
        Assert::that($environment)->choice(['dev', 'production']);
78
        $this->environment = $environment;
79
    }
80
81
    public function doRequest(RequestInterface $request) : array
82
    {
83
        // resetting last request and response
84
        $this->request = null;
85
        $this->response = null;
86
87
        // deduce url
88
        $url = $this->environment === 'dev' ? $this->testServerUrl : $this->productionServerUrl;
89
90
        // convert body to post string and inject auth and command params
91
        // @todo ask consignor if it's true that ALL requests are POST requests
92
        $body = $request->getBody();
93
        $body['actor'] = $this->actor;
94
        $body['key'] = $this->key;
95
        $body['command'] = $request->getCommand();
96
        $body = http_build_query($body);
97
98
        // create request
99
        $this->request = $this->requestFactory->createRequest($request->getMethod(), $url, $request->getHeaders(), $body);
100
101
        // send request
102
        $this->response = $this->httpClient->sendRequest($this->request);
103
104
        return $this->decodeJson((string)$this->response->getBody());
0 ignored issues
show
Bug introduced by
The method decodeJson() does not seem to exist on object<Loevgaard\Consign...ntServer\Client\Client>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
105
    }
106
107
    /**
108
     * @return null|PsrRequestInterface
109
     */
110
    public function getRequest(): ?PsrRequestInterface
111
    {
112
        return $this->request;
113
    }
114
115
    /**
116
     * @return null|ResponseInterface
117
     */
118
    public function getResponse(): ?ResponseInterface
119
    {
120
        return $this->response;
121
    }
122
}