Completed
Push — master ( 9f8e49...a904dc )
by Joachim
03:03
created

Client::getActor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Loevgaard\Consignor\ShipmentServer\Client;
3
4
use Assert\Assert;
5
use Http\Client\Common\Plugin\ErrorPlugin;
6
use Http\Client\Common\Plugin\HeaderSetPlugin;
7
use Http\Client\Common\PluginClient;
8
use Http\Client\HttpClient;
9
use Http\Discovery\HttpClientDiscovery;
10
use Http\Discovery\MessageFactoryDiscovery;
11
use Http\Message\RequestFactory;
12
use Loevgaard\Consignor\ShipmentServer\Exception\InvalidJsonException;
13
use Loevgaard\Consignor\ShipmentServer\Request\RequestInterface;
14
use Psr\Http\Message\RequestInterface as PsrRequestInterface;
15
use Psr\Http\Message\ResponseInterface;
16
use function GuzzleHttp\Psr7\build_query;
17
use function Loevgaard\Consignor\ShipmentServer\decodeJson;
18
19
class Client
20
{
21
    const ENV_DEV = 'dev';
22
    const ENV_PRODUCTION = 'production';
23
24
    /**
25
     * This URL is used for testing purposes
26
     *
27
     * @var string
28
     */
29
    protected $testServerUrl = 'http://sstest.consignor.com/ship/ShipmentServerModule.dll';
30
31
    /**
32
     * This URL is used for production requests
33
     *
34
     * @var string
35
     */
36
    protected $productionServerUrl = 'https://www.shipmentserver.com/ship/ShipmentServerModule.dll';
37
38
    /**
39
     * @var string
40
     */
41
    protected $actor;
42
43
    /**
44
     * @var string
45
     */
46
    protected $key;
47
48
    /**
49
     * Should be either 'dev' or 'production'
50
     *
51
     * @var string
52
     */
53
    protected $environment;
54
55
    /**
56
     * @var HttpClient
57
     */
58
    protected $httpClient;
59
60
    /**
61
     * @var RequestFactory
62
     */
63
    protected $requestFactory;
64
65
    /**
66
     * This is the last request
67
     *
68
     * @var PsrRequestInterface|null
69
     */
70
    protected $request;
71
72
    /**
73
     * This is the last response
74
     *
75
     * @var ResponseInterface|null
76
     */
77
    protected $response;
78
79 1
    public function __construct(string $actor, string $key, array $plugins = [], HttpClient $httpClient = null, RequestFactory $requestFactory = null, string $environment = 'production')
80
    {
81 1
        $this->actor = $actor;
82 1
        $this->key = $key;
83
84 1
        $plugins[] = new ErrorPlugin();
85 1
        $plugins[] = new HeaderSetPlugin([
86 1
            'Content-Type' => 'application/x-www-form-urlencoded',
87
            'Accept' => 'application/json'
88
        ]);
89 1
        $this->httpClient = new PluginClient($httpClient ?: HttpClientDiscovery::find(), $plugins);
90 1
        $this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find();
91
92 1
        Assert::that($environment)->choice([self::ENV_DEV, self::ENV_PRODUCTION]);
93 1
        $this->environment = $environment;
94 1
    }
95
96
    /**
97
     * @param RequestInterface $request
98
     * @return array
99
     * @throws InvalidJsonException
100
     */
101 1
    public function doRequest(RequestInterface $request) : array
102
    {
103
        // resetting last request and response
104 1
        $this->request = null;
105 1
        $this->response = null;
106
107
        // deduce url
108 1
        $url = $this->environment === self::ENV_DEV ? $this->testServerUrl : $this->productionServerUrl;
109
110
        // convert body to post string and inject auth and command params
111 1
        $body = $request->getBody();
112 1
        $body['actor'] = $this->actor;
113 1
        $body['key'] = $this->key;
114 1
        $body['command'] = $request->getCommand();
115 1
        $body = build_query($body);
116
117
        // create request
118 1
        $this->request = $this->requestFactory->createRequest('POST', $url, [], $body);
119
120
        // send request
121 1
        $this->response = $this->httpClient->sendRequest($this->request);
122
123 1
        return decodeJson((string)$this->response->getBody());
124
    }
125
126
    /**
127
     * @return string
128
     */
129
    public function getActor(): string
130
    {
131
        return $this->actor;
132
    }
133
134
    /**
135
     * @return null|PsrRequestInterface
136
     */
137
    public function getRequest(): ?PsrRequestInterface
138
    {
139
        return $this->request;
140
    }
141
142
    /**
143
     * @return null|ResponseInterface
144
     */
145
    public function getResponse(): ?ResponseInterface
146
    {
147
        return $this->response;
148
    }
149
}
150