|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
* |
|
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
16
|
|
|
* and is licensed under the MIT license. |
|
17
|
|
|
*/ |
|
18
|
|
|
namespace Elastification\Client\Transport\HttpGuzzle; |
|
19
|
|
|
|
|
20
|
|
|
use Elastification\Client\Transport\Exception\TransportLayerException; |
|
21
|
|
|
use Elastification\Client\Transport\TransportInterface; |
|
22
|
|
|
use Elastification\Client\Transport\TransportRequestInterface; |
|
23
|
|
|
use GuzzleHttp\ClientInterface; |
|
24
|
|
|
use GuzzleHttp\Psr7\Request; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @package Elastification\Client\Transport\HttpGuzzle |
|
28
|
|
|
* @author Mario Mueller |
|
29
|
|
|
*/ |
|
30
|
|
View Code Duplication |
class GuzzleTransport implements TransportInterface |
|
|
|
|
|
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var ClientInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
private $guzzleClient; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param ClientInterface $guzzleClient |
|
39
|
|
|
*/ |
|
40
|
3 |
|
public function __construct(ClientInterface $guzzleClient) |
|
41
|
|
|
{ |
|
42
|
3 |
|
$this->guzzleClient = $guzzleClient; |
|
43
|
3 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $httpMethod The http method to use. |
|
47
|
|
|
* |
|
48
|
|
|
* @return TransportRequestInterface |
|
49
|
|
|
* @author Mario Mueller |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public function createRequest($httpMethod) |
|
52
|
|
|
{ |
|
53
|
1 |
|
return new GuzzleTransportRequest(new Request($httpMethod, '')); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param TransportRequestInterface $request The configured request to send. |
|
58
|
|
|
* |
|
59
|
|
|
* @throws \Elastification\Client\Exception\ClientException |
|
60
|
|
|
* @return \Elastification\Client\Transport\TransportResponseInterface |
|
61
|
|
|
* @author Mario Mueller |
|
62
|
|
|
*/ |
|
63
|
2 |
|
public function send(TransportRequestInterface $request) |
|
64
|
|
|
{ |
|
65
|
|
|
try { |
|
66
|
2 |
|
return new GuzzleTransportResponse($this->guzzleClient->send($request->getWrappedRequest())); |
|
67
|
1 |
|
} catch (\Exception $exception) { |
|
68
|
1 |
|
throw new TransportLayerException($exception->getMessage(), $exception->getCode(), $exception); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.