|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the bee4/transport package. |
|
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
5
|
|
|
* file that was distributed with this source code. |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright Bee4 2015 |
|
8
|
|
|
* @author Stephane HULARD <[email protected]> |
|
9
|
|
|
* @package Bee4\Transport |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Bee4\Transport; |
|
13
|
|
|
|
|
14
|
|
|
use Bee4\Events\DispatcherAwareTrait; |
|
15
|
|
|
use Bee4\Transport\Events\ErrorEvent; |
|
16
|
|
|
use Bee4\Transport\Events\MessageEvent; |
|
17
|
|
|
use Bee4\Transport\Exception\CurlException; |
|
18
|
|
|
use Bee4\Transport\Message\Request\AbstractRequest; |
|
19
|
|
|
use Bee4\Transport\Message\Request\RequestFactory; |
|
20
|
|
|
use Bee4\Transport\Message\ResponseFactory; |
|
21
|
|
|
use Bee4\Transport\Handle\HandleFactory; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Transport client, generate a request and return the linked response |
|
25
|
|
|
* @package Bee4\Transport |
|
26
|
|
|
*/ |
|
27
|
|
|
class Client implements ClientInterface |
|
28
|
|
|
{ |
|
29
|
|
|
use DispatcherAwareTrait; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Base URL for calls |
|
33
|
|
|
* @var Url |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $baseUrl; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The factory to build request messages |
|
39
|
|
|
* @var RequestFactory |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $requestFactory; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* HTTP Client which use cURL extension |
|
45
|
|
|
* @param string $baseUrl Base URL of the web service |
|
46
|
|
|
*/ |
|
47
|
14 |
|
public function __construct($baseUrl = '') |
|
48
|
|
|
{ |
|
49
|
|
|
// @codeCoverageIgnoreStart |
|
50
|
|
|
if (!extension_loaded('curl')) { |
|
51
|
|
|
throw new \RuntimeException('The PHP cURL extension must be installed!'); |
|
52
|
|
|
} |
|
53
|
|
|
// @codeCoverageIgnoreEnd |
|
54
|
|
|
|
|
55
|
14 |
|
if ($baseUrl != '') { |
|
56
|
13 |
|
$this->baseUrl = new Url($baseUrl); |
|
57
|
13 |
|
} |
|
58
|
|
|
|
|
59
|
14 |
|
$this->requestFactory = new RequestFactory(); |
|
60
|
14 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Create the request object |
|
64
|
|
|
* @param string $method |
|
65
|
|
|
* @param string $url |
|
66
|
|
|
* @param array $headers |
|
67
|
|
|
* @return AbstractRequest |
|
68
|
|
|
*/ |
|
69
|
13 |
|
public function createRequest($method, $url = '', array $headers = []) |
|
70
|
|
|
{ |
|
71
|
13 |
|
if (!is_string($url)) { |
|
72
|
1 |
|
throw new \InvalidArgumentException('Url parameter must be a valid string!!'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
12 |
|
$url = new Url((isset($this->baseUrl)?$this->baseUrl->toString():'').$url); |
|
76
|
|
|
|
|
77
|
11 |
|
$request = $this->requestFactory->build($method, $url, $headers); |
|
78
|
10 |
|
$request->setClient($this); |
|
79
|
|
|
|
|
80
|
10 |
|
return $request; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Send the request |
|
85
|
|
|
* @param AbstractRequest $request The request to be send |
|
86
|
|
|
* @return Message\Response |
|
87
|
|
|
* @throws \Exception |
|
88
|
|
|
*/ |
|
89
|
11 |
|
public function send(AbstractRequest $request) |
|
90
|
|
|
{ |
|
91
|
11 |
|
$handle = HandleFactory::build($request); |
|
92
|
11 |
|
$this->dispatch(MessageEvent::REQUEST, new MessageEvent($request)); |
|
93
|
|
|
|
|
94
|
|
|
try { |
|
95
|
10 |
|
$result = $handle->execute(); |
|
96
|
10 |
|
} catch (\Exception $error) { |
|
97
|
1 |
|
if ($error instanceof CurlException) { |
|
98
|
1 |
|
$response = ResponseFactory::build('', $handle, $request); |
|
99
|
1 |
|
$error->setResponse($response); |
|
100
|
1 |
|
} |
|
101
|
1 |
|
$this->dispatch(ErrorEvent::ERROR, new ErrorEvent($error)); |
|
102
|
1 |
|
throw $error; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
9 |
|
$response = ResponseFactory::build($result, $handle, $request); |
|
106
|
9 |
|
$this->dispatch(MessageEvent::RESPONSE, new MessageEvent($response)); |
|
107
|
|
|
|
|
108
|
9 |
|
return $response; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|