HttpHeadersTrait::adjustHeaders()   B
last analyzed

Complexity

Conditions 11
Paths 48

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 11.0359

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 14
cts 15
cp 0.9333
rs 7.3166
c 0
b 0
f 0
cc 11
nc 48
nop 1
crap 11.0359

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of the PHP Generics package.
4
 *
5
 * @package Generics
6
 */
7
namespace Generics\Client;
8
9
use Generics\Util\Arrays;
10
11
/**
12
 * This trait provides common http(s) header functionality
13
 *
14
 * @author Maik Greubel <[email protected]>
15
 */
16
trait HttpHeadersTrait
17
{
18
19
    /**
20
     * Headers
21
     *
22
     * @var array
23
     */
24
    private $headers;
25
26
    /**
27
     * The response status code
28
     *
29
     * @var int
30
     */
31
    private $responseCode;
32
    
33
    /**
34
     *
35
     * {@inheritdoc}
36
     * @see \Generics\Streams\HttpStream::setHeader()
37
     * @return HttpClient
38
     */
39 22
    public function setHeader($headerName, $headerValue)
40
    {
41 22
        $this->headers[$headerName] = $headerValue;
42 22
    }
43
44
    /**
45
     * Reset the headers
46
     */
47 25
    public function resetHeaders()
48
    {
49 25
        $this->headers = array();
50 25
    }
51
52
    /**
53
     *
54
     * {@inheritdoc}
55
     * @see \Generics\Streams\HttpStream::getHeaders()
56
     */
57 22
    public function getHeaders(): array
58
    {
59 22
        return $this->headers;
60
    }
61
    
62
    /**
63
     * Retrieve the response status code
64
     *
65
     * @return int
66
     */
67 16
    public function getResponseCode(): int
68
    {
69 16
        return $this->responseCode;
70
    }
71
72
    /**
73
     * Adjust the headers by injecting default values for missing keys.
74
     */
75 22
    private function adjustHeaders($requestType)
76
    {
77 22
        if (! array_key_exists('Accept', $this->headers) && $requestType != 'HEAD') {
78 18
            $this->setHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');
79
        }
80
        
81 22
        if (! array_key_exists('Accept-Language', $this->headers) && $requestType != 'HEAD') {
82 18
            $this->setHeader('Accept-Language', 'en-US;q=0.7,en;q=0.3');
83
        }
84
        
85 22
        if (! array_key_exists('User-Agent', $this->headers) && $requestType != 'HEAD') {
86 18
            $this->setHeader('User-Agent', 'phpGenerics 1.0');
87
        }
88
        
89 22
        if (! array_key_exists('Connection', $this->headers) || strlen($this->headers['Connection']) == 0) {
90 18
            $this->adjustConnectionHeader($requestType);
91
        }
92
        
93 22
        if (! array_key_exists('Accept-Encoding', $this->headers)) {
94 22
            if (function_exists('gzinflate')) {
95 22
                $encoding = 'gzip, deflate';
96
            } else {
97
                $encoding = 'identity';
98
            }
99 22
            $this->setHeader('Accept-Encoding', $encoding);
100
        }
101 22
    }
102
103
    /**
104
     * Depending on request type the connection header is either
105
     * set to keep-alive or close
106
     *
107
     * @param string $requestType
108
     */
109 18
    private function adjustConnectionHeader($requestType)
110
    {
111 18
        if ($requestType == 'HEAD') {
112 2
            $this->setHeader('Connection', 'close');
113
        } else {
114 16
            $this->setHeader('Connection', 'keep-alive');
115
        }
116 18
    }
117
118
    /**
119
     * Try to parse line as header and add the results to local header list
120
     *
121
     * @param string $line
122
     */
123 18
    private function addParsedHeader($line)
124
    {
125 18
        if (strpos($line, ':') === false) {
126 18
            $this->responseCode = HttpStatus::parseStatus($line)->getCode();
127
        } else {
128 18
            $line = trim($line);
129 18
            list ($headerName, $headerValue) = explode(':', $line, 2);
130 18
            $this->headers[$headerName] = trim($headerValue);
131
        }
132 18
    }
133
134
    /**
135
     * Adjust number of bytes to read according content length header
136
     *
137
     * @param int $numBytes
138
     * @return int
139
     */
140 18
    private function adjustNumbytes($numBytes): int
141
    {
142 18
        if (isset($this->headers['Content-Length'])) {
143
            // Try to read the whole payload at once
144 18
            $numBytes = intval($this->headers['Content-Length']);
145
        }
146
        
147 18
        return $numBytes;
148
    }
149
150
    /**
151
     * Retrieve content type from headers
152
     *
153
     * @return string
154
     */
155 14
    private function getContentEncoding(): string
156
    {
157 14
        return $this->getHeader('Content-Encoding');
158
    }
159
160
    /**
161
     * Retrieve an given header
162
     *
163
     * @param string $name
164
     * @return string
165
     */
166 18
    private function getHeader(string $name): string
167
    {
168 18
        $result = "";
169
        
170 18
        if (Arrays::hasElement($this->headers, $name)) {
171 18
            $result = $this->headers[$name];
172
        }
173
        
174 18
        return $result;
175
    }
176
}
177