Completed
Push — master ( 70406b...d8bd85 )
by Jairo
02:44
created

HttpParser   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 147
ccs 58
cts 58
cp 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A parseHttpRequestHeader() 0 6 1
A parseHttpResponseHeader() 0 6 1
A get() 0 15 3
A getHeader() 0 4 1
A processHeader() 0 5 1
A splitRawLine() 0 11 3
A processFields() 0 6 2
A setHttpRaw() 0 5 1
B extract() 0 21 5
1
<?php
2
3
/**
4
 * User: jairo.rodriguez <[email protected]>
5
 * Date: 16/04/2016
6
 * Time: 11:00
7
 */
8
9
namespace BFunky\HttpParser;
10
11
use \BFunky\HttpParser\Entity\HttpField;
12
use \BFunky\HttpParser\Entity\HttpRequestHeader;
13
use \BFunky\HttpParser\Entity\HttpResponseHeader;
14
use \BFunky\HttpParser\Entity\HttpHeaderInterface;
15
use \BFunky\HttpParser\Exception\HttpParserBadFormatException;
16
17
class HttpParser
18
{
19
    /**
20
     * @var string
21
     */
22
    protected $httpRaw;
23
24
    /**
25
     * @var HttpHeaderInterface
26
     */
27
    protected $httpHeader;
28
29
    /**
30
     * @var array
31
     */
32
    protected $httpFields;
33
34
    /**
35
     * HttpParser constructor.
36
     */
37 3
    public function __construct()
38
    {
39 3
    }
40
41
    /**
42
     * @param string $rawHttpHeader
43
     */
44 2
    public function parseHttpRequestHeader($rawHttpHeader)
45
    {
46 2
        list($header, $fieldList) = $this->processHeader($rawHttpHeader);
47 1
        $this->httpHeader = new HttpRequestHeader($header[0], $header[1], $header[2]);
48 1
        $this->processFields($fieldList);
49 1
    }
50
51
    /**
52
     * @param string $rawHttpHeader
53
     */
54 1
    public function parseHttpResponseHeader($rawHttpHeader)
55
    {
56 1
        list($header, $fieldList) = $this->processHeader($rawHttpHeader);
57 1
        $this->httpHeader = new HttpResponseHeader($header[0], $header[1], $header[2]);
58 1
        $this->processFields($fieldList);
59 1
    }
60
61
    /**
62
     * @param string $headerFieldName
63
     * @return string
64
     */
65 2
    public function get($headerFieldName)
66
    {
67 2
        $value = '';
68 2
        foreach ($this->httpFields as $httpField) {
69
            /**
70
             * @var HttpField $httpField
71
             */
72 2
            if ($httpField->getName() === $headerFieldName) {
73 2
                $value = $httpField->getValue();
74 2
                break;
75
            }
76
77 2
        }
78 2
        return $value;
79
    }
80
81
    /**
82
     * @return HttpHeaderInterface
83
     */
84 2
    public function getHeader()
85
    {
86 2
        return $this->httpHeader;
87
    }
88
89
    /**
90
     * @param string $rawHttpHeader
91
     * @return mixed
92
     */
93 3
    protected function processHeader($rawHttpHeader)
94
    {
95 3
        $this->setHttpRaw($rawHttpHeader);
96 3
        return $this->extract();
97
    }
98
99
    /**
100
     * Split the http string
101
     * @return mixed
102
     * @throws HttpParserBadFormatException
103
     */
104 3
    protected function extract()
105
    {
106 3
        $header = array();
107 3
        $fieldList = array();
108 3
        $headers = explode("\n", $this->httpRaw);
109 3
        foreach ($headers as $i => $headerLine) {
110 3
            if ($headerLine === '') {
111 2
                continue;
112
            }
113 3
            $parts = $this->splitRawLine($headerLine);
114 3
            if (isset($parts[1])) {
115 2
                $fieldList[$parts[0]] = $parts[1];
116 2
            } else {
117 3
                $header = preg_split('/ /', $headerLine);
118 3
                if (count($header) != 3) {
119 1
                    throw new HttpParserBadFormatException();
120
                }
121
            }
122 2
        }
123 2
        return array($header, $fieldList);
124
    }
125
126
    /**
127
     * @param string $line
128
     * @return array
129
     */
130 3
    protected function splitRawLine($line)
131
    {
132 3
        $parts = array();
133 3
        if (strpos($line, ': ') !== false) {
134 2
            $parts = explode(': ', $line);
135 2
        }
136 3
        else if (strpos($line, ':') !== false) {
137 1
            $parts = explode(':', $line);
138 1
        }
139 3
        return $parts;
140
    }
141
142
    /**
143
     * @param array $fieldList
144
     */
145 2
    protected function processFields($fieldList)
146
    {
147 2
        foreach ($fieldList as $fieldName => $fieldValue) {
148 2
            $this->httpFields [] = new HttpField($fieldName, $fieldValue);
149 2
        }
150 2
    }
151
152
    /**
153
     * @param string $httpRaw
154
     * @return HttpParser
155
     */
156 3
    protected function setHttpRaw($httpRaw)
157
    {
158 3
        $this->httpRaw = $httpRaw;
159 3
        return $this;
160
    }
161
162
163
}