Completed
Push — master ( ad55a4...c98fe8 )
by Joachim
01:57
created

Client::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
crap 1
1
<?php
2
namespace Loevgaard\AltaPay;
3
4
use GuzzleHttp\Client as GuzzleClient;
5
use GuzzleHttp\ClientInterface as GuzzleClientInterface;
6
use Loevgaard\AltaPay\Payload\CaptureReservationInterface;
7
use Loevgaard\AltaPay\Payload\PaymentRequestInterface;
8
use Loevgaard\AltaPay\Payload\RefundCapturedReservationInterface;
9
use Loevgaard\AltaPay\Response\CaptureReservation as CaptureReservationResponse;
10
use Loevgaard\AltaPay\Response\GetTerminals as GetTerminalsResponse;
11
use Loevgaard\AltaPay\Response\PaymentRequest as PaymentRequestResponse;
12
use Loevgaard\AltaPay\Response\RefundCapturedReservation as RefundCapturedReservationResponse;
13
use Psr\Http\Message\ResponseInterface;
14
15
class Client
16
{
17
    /**
18
     * @var GuzzleClientInterface
19
     */
20
    protected $client;
21
22
    /**
23
     * Your username for the AltaPay gateway
24
     *
25
     * @var string
26
     */
27
    private $username;
28
29
    /**
30
     * Your password for the AltaPay gateway
31
     *
32
     * @var string
33
     */
34
    private $password;
35
36
    /**
37
     * The URL for the gateway
38
     * The default value for this is the test gateway URL
39
     *
40
     * @var string
41
     */
42
    private $baseUrl;
43
44 15
    public function __construct($username, $password, $baseUrl = 'https://testgateway.altapaysecure.com')
45
    {
46 15
        $this->username = $username;
47 15
        $this->password = $password;
48
49 15
        $parsedBaseUrl = parse_url($baseUrl);
50 15
        $baseUrl = $parsedBaseUrl['scheme'].
51 15
            '://'.rawurlencode($this->username).':'.rawurlencode($this->password).'@'.$parsedBaseUrl['host'];
52
53 15
        $this->baseUrl  = $baseUrl;
54 15
    }
55
56
    /**
57
     * @param PaymentRequestInterface $paymentRequest
58
     * @return PaymentRequestResponse
59
     */
60 3
    public function createPaymentRequest(PaymentRequestInterface $paymentRequest) : PaymentRequestResponse
61
    {
62 3
        $response = new PaymentRequestResponse($this->doRequest('post', '/merchant/API/createPaymentRequest', [
63 3
            'form_params' => $paymentRequest->getPayload()
64
        ]));
65 3
        return $response;
66
    }
67
68
    /**
69
     * @codeCoverageIgnore
70
     */
71
    public function testConnection()
72
    {
73
        // @todo Implement method
74
        throw new \RuntimeException('Method is not implemented');
75
    }
76
77
    /**
78
     * @codeCoverageIgnore
79
     */
80
    public function login()
81
    {
82
        // @todo Implement method
83
        throw new \RuntimeException('Method is not implemented');
84
    }
85
86
    /**
87
     * @codeCoverageIgnore
88
     */
89
    public function payments()
90
    {
91
        // @todo Implement method
92
        throw new \RuntimeException('Method is not implemented');
93
    }
94
95
    /**
96
     * @param CaptureReservationInterface $captureReservation
97
     * @return CaptureReservationResponse
98
     */
99 3
    public function captureReservation(CaptureReservationInterface $captureReservation) : CaptureReservationResponse
100
    {
101 3
        return new CaptureReservationResponse($this->doRequest('get', '/merchant/API/captureReservation', [
102 3
            'query' => $captureReservation->getPayload()
103
        ]));
104
    }
105
106
    /**
107
     * @codeCoverageIgnore
108
     */
109
    public function releaseReservation()
110
    {
111
        // @todo Implement method
112
        throw new \RuntimeException('Method is not implemented');
113
    }
114
115
116
    /**
117
     * @param RefundCapturedReservationInterface $refundCapturedReservation
118
     * @return RefundCapturedReservationResponse
119
     */
120 3
    public function refundCapturedReservation(RefundCapturedReservationInterface $refundCapturedReservation) : RefundCapturedReservationResponse
121
    {
122 3
        return new RefundCapturedReservationResponse($this->doRequest('get', '/merchant/API/refundCapturedReservation', [
123 3
            'query' => $refundCapturedReservation->getPayload()
124
        ]));
125
    }
126
127
    /**
128
     * @codeCoverageIgnore
129
     */
130
    public function setupSubscription()
131
    {
132
        // @todo Implement method
133
        throw new \RuntimeException('Method is not implemented');
134
    }
135
136
    /**
137
     * @codeCoverageIgnore
138
     */
139
    public function chargeSubscription()
140
    {
141
        // @todo Implement method
142
        throw new \RuntimeException('Method is not implemented');
143
    }
144
145
    /**
146
     * @codeCoverageIgnore
147
     */
148
    public function reserveSubscriptionCharge()
149
    {
150
        // @todo Implement method
151
        throw new \RuntimeException('Method is not implemented');
152
    }
153
154
    /**
155
     * @codeCoverageIgnore
156
     */
157
    public function updateOrder()
158
    {
159
        // @todo Implement method
160
        throw new \RuntimeException('Method is not implemented');
161
    }
162
163
    /**
164
     * @codeCoverageIgnore
165
     */
166
    public function fundingList()
167
    {
168
        // @todo Implement method
169
        throw new \RuntimeException('Method is not implemented');
170
    }
171
172
    /**
173
     * @codeCoverageIgnore
174
     */
175
    public function fundingDownload()
176
    {
177
        // @todo Implement method
178
        throw new \RuntimeException('Method is not implemented');
179
    }
180
181
    /**
182
     * @codeCoverageIgnore
183
     */
184
    public function getCustomReport()
185
    {
186
        // @todo Implement method
187
        throw new \RuntimeException('Method is not implemented');
188
    }
189
190
    /**
191
     * @codeCoverageIgnore
192
     */
193
    public function reservationOfFixedAmount()
194
    {
195
        // @todo Implement method
196
        throw new \RuntimeException('Method is not implemented');
197
    }
198
199
    /**
200
     * @codeCoverageIgnore
201
     */
202
    public function credit()
203
    {
204
        // @todo Implement method
205
        throw new \RuntimeException('Method is not implemented');
206
    }
207
208
    /**
209
     * @return GetTerminalsResponse
210
     */
211 3
    public function getTerminals() : GetTerminalsResponse
212
    {
213 3
        return new GetTerminalsResponse($this->doRequest('get', '/merchant/API/getTerminals'));
214
    }
215
216
    /**
217
     * @codeCoverageIgnore
218
     */
219
    public function getInvoiceText()
220
    {
221
        // @todo Implement method
222
        throw new \RuntimeException('Method is not implemented');
223
    }
224
225
    /**
226
     * @codeCoverageIgnore
227
     */
228
    public function createInvoiceReservation()
229
    {
230
        // @todo Implement method
231
        throw new \RuntimeException('Method is not implemented');
232
    }
233
234
    /**
235
     * @codeCoverageIgnore
236
     */
237
    public function calculateSurcharge()
238
    {
239
        // @todo Implement method
240
        throw new \RuntimeException('Method is not implemented');
241
    }
242
243
    /**
244
     * @codeCoverageIgnore
245
     */
246
    public function queryGiftCard()
247
    {
248
        // @todo Implement method
249
        throw new \RuntimeException('Method is not implemented');
250
    }
251
252
    /**
253
     * @param string $method
254
     * @param string $uri
255
     * @param array|null $options
256
     * @return ResponseInterface
257
     */
258 12
    public function doRequest($method, $uri, array $options = null)
259
    {
260 12
        $url = $this->baseUrl.$uri;
261 12
        $options = $options ? : [];
262 12
        $defaultOptions = [];
263 12
        $options = array_merge($defaultOptions, $options);
264 12
        $client = $this->getClient();
265 12
        return $client->request($method, $url, $options);
266
    }
267
268
    /**
269
     * @return GuzzleClientInterface
270
     */
271 15
    public function getClient()
272
    {
273 15
        if (!$this->client) {
274 3
            $this->client = new GuzzleClient([
275 3
                'allow_redirects' => false,
276
                'cookies' => false,
277
            ]);
278
        }
279 15
        return $this->client;
280
    }
281
282
    /**
283
     * @param GuzzleClientInterface $client
284
     * @return Client
285
     */
286 12
    public function setClient(GuzzleClientInterface $client)
287
    {
288 12
        $this->client = $client;
289 12
        return $this;
290
    }
291
}
292