Dispatcher::processHeadLine()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2015-12-09
5
 */
6
7
namespace Net\Bazzline\Component\Curl\Dispatcher;
8
9
use Net\Bazzline\Component\Curl\Response\Response;
10
11
class Dispatcher implements DispatcherInterface
12
{
13
    /** @var array */
14
    private $headerLines;
15
16
    /**
17
     * @param string $url
18
     * @param array $options
19
     * @return Response
20
     */
21
    public function dispatch($url, array $options = [])
22
    {
23
        $this->reset();
24
        $handler    = $this->getHandler($url);
25
        $handler    = $this->setOptions($handler, $options);
26
        $response   = $this->execute($handler);
27
28
        return $response;
29
    }
30
31
    /**
32
     * @param resource $handler
33
     * @return Response
34
     */
35
    protected function execute($handler)
36
    {
37
        $content        = curl_exec($handler);
38
        $contentType    = curl_getinfo($handler, CURLINFO_CONTENT_TYPE);
39
        //@see http://stackoverflow.com/a/10667879
40
        $error          = curl_error($handler);
41
        $errorCode      = curl_errno($handler);
42
        $statusCode     = curl_getinfo($handler, CURLINFO_HTTP_CODE);
43
        //@todo investigate if needed http://www.ivangabriele.com/php-how-to-use-4-methods-delete-get-post-put-in-a-restful-api-client-using-curl/
44
        //@todo how to handle response code 100 - other header? - http://stackoverflow.com/a/23939785
45
        curl_close($handler);
46
47
        return new Response($content, $contentType, $error, $errorCode, $this->headerLines, $statusCode);
48
    }
49
50
    /**
51
     * @param string $url
52
     * @return resource
53
     */
54
    protected function getHandler($url)
55
    {
56
        $handler = curl_init($url);
57
58
        return $handler;
59
    }
60
61
    /**
62
     * @param resource $handler
63
     * @param array $options
64
     * @return resource
65
     */
66
    protected function setOptions($handler, array $options)
67
    {
68
        $options[CURLINFO_HEADER_OUT]       = 1;
69
        $options[CURLOPT_HEADERFUNCTION]    = [
70
            $this,
71
            'processHeadLine'
72
        ];
73
        $options[CURLOPT_RETURNTRANSFER]    = true;
74
75
        curl_setopt_array($handler, $options);
76
77
        return $handler;
78
    }
79
80
81
82
    /**
83
     * @param resource $handler
84
     * @param string $string
85
     * @return int
86
     */
87
    private function processHeadLine($handler, $string)
88
    {
89
        $delimiter  = ':';
90
        $exploded   = explode($delimiter, trim($string));
91
        $isValid    = (count($exploded) === 2);
92
93
        if ($isValid) {
94
            $prefix                     = array_shift($exploded);
95
            $this->headerLines[$prefix] = implode($delimiter, $exploded); //needed because of lines like "Date: Thu, 17 Dec 2015 16:47:42 GMT"
96
        }
97
98
        return strlen($string);
99
    }
100
101
    private function reset()
102
    {
103
        $this->headerLines = [];
104
    }
105
}
106