AbstractStreamHttpAdapter::process()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
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 Ivory\HttpAdapter\Extractor\ProtocolVersionExtractor;
15
use Ivory\HttpAdapter\Extractor\StatusCodeExtractor;
16
use Ivory\HttpAdapter\Message\InternalRequestInterface;
17
use Ivory\HttpAdapter\Normalizer\BodyNormalizer;
18
use Ivory\HttpAdapter\Normalizer\HeadersNormalizer;
19
20
/**
21
 * @author GeLo <[email protected]>
22
 */
23
abstract class AbstractStreamHttpAdapter extends AbstractHttpAdapter
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28 1512
    protected function sendInternalRequest(InternalRequestInterface $internalRequest)
29
    {
30 1512
        $context = stream_context_create([
31
            'http' => [
32 1176
                'follow_location'  => false,
33 1512
                'max_redirects'    => 1,
34 1176
                'ignore_errors'    => true,
35 1512
                'timeout'          => $this->getConfiguration()->getTimeout(),
36 1512
                'protocol_version' => $internalRequest->getProtocolVersion(),
37 1512
                'method'           => $internalRequest->getMethod(),
38 1512
                'header'           => $this->prepareHeaders($internalRequest, false),
39 1512
                'content'          => $this->prepareBody($internalRequest),
40 1512
            ],
41 1176
        ]);
42
43 1512
        list($body, $headers) = $this->process($uri = (string) $internalRequest->getUri(), $context);
44
45 1512
        if ($body === false) {
46 72
            $error = error_get_last();
47 72
            throw HttpAdapterException::cannotFetchUri($uri, $this->getName(), $error['message']);
48
        }
49
50 1458
        return $this->getConfiguration()->getMessageFactory()->createResponse(
51 1458
            StatusCodeExtractor::extract($headers),
52 1458
            ProtocolVersionExtractor::extract($headers),
53 1458
            HeadersNormalizer::normalize($headers),
54 1458
            BodyNormalizer::normalize($body, $internalRequest->getMethod())
55 1134
        );
56
    }
57
58
    /**
59
     * @param string   $uri
60
     * @param resource $context
61
     *
62
     * @return array
63
     */
64
    abstract protected function process($uri, $context);
65
}
66