1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivory Http Adapter package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivory\HttpAdapter; |
13
|
|
|
|
14
|
|
|
use Cake\Network\Http\Client; |
15
|
|
|
use Cake\Network\Http\Request; |
16
|
|
|
use Ivory\HttpAdapter\Message\InternalRequestInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author GeLo <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class CakeHttpAdapter extends AbstractHttpAdapter |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var Client |
25
|
|
|
*/ |
26
|
|
|
private $client; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param Client|null $client |
30
|
|
|
* @param ConfigurationInterface|null $configuration |
31
|
|
|
*/ |
32
|
792 |
|
public function __construct(Client $client = null, ConfigurationInterface $configuration = null) |
33
|
|
|
{ |
34
|
792 |
|
parent::__construct($configuration); |
35
|
|
|
|
36
|
792 |
|
$this->client = $client ?: new Client(); |
37
|
792 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
45 |
|
public function getName() |
43
|
|
|
{ |
44
|
45 |
|
return 'cake'; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
756 |
|
protected function sendInternalRequest(InternalRequestInterface $internalRequest) |
51
|
|
|
{ |
52
|
756 |
|
$request = new Request(); |
53
|
|
|
|
54
|
756 |
|
foreach ($this->prepareHeaders($internalRequest) as $name => $value) { |
55
|
756 |
|
$request->header($name, $value); |
56
|
588 |
|
} |
57
|
|
|
|
58
|
756 |
|
$request->method($internalRequest->getMethod()); |
59
|
756 |
|
$request->body($this->prepareBody($internalRequest)); |
60
|
756 |
|
$request->url($uri = (string) $internalRequest->getUri()); |
61
|
756 |
|
$request->version($this->getConfiguration()->getProtocolVersion()); |
62
|
|
|
|
63
|
|
|
try { |
64
|
756 |
|
$response = $this->client->send($request, [ |
65
|
756 |
|
'timeout' => $this->getConfiguration()->getTimeout(), |
66
|
588 |
|
'redirect' => false, |
67
|
588 |
|
]); |
68
|
596 |
|
} catch (\Exception $e) { |
69
|
36 |
|
throw HttpAdapterException::cannotFetchUri($uri, $this->getName(), $e->getMessage()); |
70
|
|
|
} |
71
|
|
|
|
72
|
729 |
|
return $this->getConfiguration()->getMessageFactory()->createResponse( |
73
|
729 |
|
(int) $response->statusCode(), |
74
|
729 |
|
$response->version(), |
75
|
750 |
|
$response->headers(), |
76
|
729 |
|
$response->body() |
77
|
567 |
|
); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|