forked from fritz-payment/jsonrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.php
76 lines (72 loc) · 2.06 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/*
* This file is part of the Fritz Payment JSON RPC package.
*
* (c) Fritz Payment GmbH <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FritzPayment\JsonRpc;
use FritzPayment\JsonRpc\Rpc\Codec;
use FritzPayment\JsonRpc\Client\Transport;
use FritzPayment\JsonRpc\Request;
use FritzPayment\JsonRpc\Response;
use FritzPayment\JsonRpc\Exception\ClientException;
use FritzPayment\JsonRpc\Exception\CodecException;
use FritzPayment\JsonRpc\Exception\TransportException;
class Client
{
/**
* @var string
*/
protected $url;
/**
* @var Rpc\Codec
*/
protected $codec;
/**
* @var Client\Transport
*/
protected $transport;
public function __construct($url, Codec $codec, Transport $transport) {
$this->url = $url;
$this->codec = $codec;
$this->transport = $transport;
if (!$this->transport->setUrl($this->url)) {
throw new ClientException('Error setting URL for transport.');
}
}
/**
* @return Request
*/
public function newRequest() {
return $this->codec->getRequest();
}
/**
* @param Request $request
*
* @return bool|Response
* @throws Exception\CodecException
* @throws Exception\TransportException
*/
public function exec(Request $request) {
if (!$this->codec->isCodecRequest($request)) {
throw new CodecException('Invalid request. Codec cannot handle request object.');
}
try {
$responseBody = $this->transport->send($request);
} catch (\Exception $e) {
$ex = new TransportException('Error sending request.', 0, $e);
throw $ex;
}
if ($responseBody === false) {
return false;
}
$response = $this->codec->getResponse();
$response->setRequest($request)
->setResponseBody($responseBody)
->parseResponse();
return $response;
}
}