Completed
Pull Request — master (#131)
by Eric
63:39 queued 61:20
created

CakeHttpAdapter::sendInternalRequest()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 0
cts 21
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 19
nc 4
nop 1
crap 12
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 18
    public function __construct(Client $client = null, ConfigurationInterface $configuration = null)
33
    {
34 18
        parent::__construct($configuration);
35
36 18
        $this->client = $client ?: new Client();
37 18
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getName()
43
    {
44
        return 'cake';
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    protected function sendInternalRequest(InternalRequestInterface $internalRequest)
51
    {
52
        $request = new Request();
53
54
        foreach ($this->prepareHeaders($internalRequest) as $name => $value) {
55
            $request->header($name, $value);
56
        }
57
58
        $request->method($internalRequest->getMethod());
59
        $request->body($this->prepareBody($internalRequest));
60
        $request->url($uri = (string) $internalRequest->getUri());
61
        $request->version($this->getConfiguration()->getProtocolVersion());
62
63
        try {
64
            $response = $this->client->send($request, [
65
                'timeout'  => $this->getConfiguration()->getTimeout(),
66
                'redirect' => false,
67
            ]);
68
        } catch (\Exception $e) {
69
            throw HttpAdapterException::cannotFetchUri($uri, $this->getName(), $e->getMessage());
70
        }
71
72
        return $this->getConfiguration()->getMessageFactory()->createResponse(
73
            (int) $response->statusCode(),
74
            $response->version(),
75
            $response->headers(),
76
            $response->body()
77
        );
78
    }
79
}
80