HeaderBag::__construct()   D
last analyzed

Complexity

Conditions 16
Paths 240

Size

Total Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 72
rs 4.2333
c 0
b 0
f 0
cc 16
nc 240
nop 0

How to fix   Long Method    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
 * @license MIT
4
 */
5
namespace Pivasic\Bundle\Common\Bag;
6
7
/**
8
 * Class HeaderBag
9
 * @package Pivasic\Bundle\Common\Bag
10
 */
11
class HeaderBag extends ValueBag
12
{
13
    /**
14
     * Gets the HTTP headers.
15
     */
16
    public function __construct()
17
    {
18
        $headers = [];
19
        $server = filter_input_array(INPUT_SERVER);
20
21
        $contentHeaders = ['CONTENT_LENGTH' => true, 'CONTENT_MD5' => true, 'CONTENT_TYPE' => true];
22
        foreach ($server as $key => $value) {
23
            if (0 === strpos($key, 'HTTP_')) {
24
                $headers[substr($key, 5)] = $value;
25
            }
26
            // CONTENT_* are not prefixed with HTTP_
27
            elseif (isset($contentHeaders[$key])) {
28
                $headers[$key] = $value;
29
            }
30
        }
31
32
        if (isset($server['PHP_AUTH_USER'])) {
33
            $headers['PHP_AUTH_USER'] = $server['PHP_AUTH_USER'];
34
            $headers['PHP_AUTH_PW'] = isset($server['PHP_AUTH_PW']) ? $server['PHP_AUTH_PW'] : '';
35
        } else {
36
            /*
37
             * php-cgi under Apache does not pass HTTP Basic user/pass to PHP by default
38
             * For this workaround to work, add these lines to your .htaccess file:
39
             * RewriteCond %{HTTP:Authorization} ^(.+)$
40
             * RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
41
             *
42
             * A sample .htaccess file:
43
             * RewriteEngine On
44
             * RewriteCond %{HTTP:Authorization} ^(.+)$
45
             * RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
46
             * RewriteCond %{REQUEST_FILENAME} !-f
47
             * RewriteRule ^(.*)$ app.php [QSA,L]
48
             */
49
50
            $authorizationHeader = null;
51
            if (isset($server['HTTP_AUTHORIZATION'])) {
52
                $authorizationHeader = $server['HTTP_AUTHORIZATION'];
53
            } elseif (isset($server['REDIRECT_HTTP_AUTHORIZATION'])) {
54
                $authorizationHeader = $server['REDIRECT_HTTP_AUTHORIZATION'];
55
            }
56
57
            if (null !== $authorizationHeader) {
58
                if (0 === stripos($authorizationHeader, 'basic ')) {
59
                    // Decode AUTHORIZATION header into PHP_AUTH_USER and PHP_AUTH_PW when authorization header is basic
60
                    $exploded = explode(':', base64_decode(substr($authorizationHeader, 6)), 2);
61
                    if (count($exploded) == 2) {
62
                        list($headers['PHP_AUTH_USER'], $headers['PHP_AUTH_PW']) = $exploded;
63
                    }
64
                } elseif (empty($server['PHP_AUTH_DIGEST']) && (0 === stripos($authorizationHeader, 'digest '))) {
65
                    // In some circumstances PHP_AUTH_DIGEST needs to be set
66
                    $headers['PHP_AUTH_DIGEST'] = $authorizationHeader;
67
                    $server['PHP_AUTH_DIGEST'] = $authorizationHeader;
68
                } elseif (0 === stripos($authorizationHeader, 'bearer ')) {
69
                    /*
70
                     * XXX: Since there is no PHP_AUTH_BEARER in PHP predefined variables,
71
                     *      I'll just set $headers['AUTHORIZATION'] here.
72
                     *      http://php.net/manual/en/reserved.variables.server.php
73
                     */
74
                    $headers['AUTHORIZATION'] = $authorizationHeader;
75
                }
76
            }
77
        }
78
79
        // PHP_AUTH_USER/PHP_AUTH_PW
80
        if (isset($headers['PHP_AUTH_USER'])) {
81
            $headers['AUTHORIZATION'] = 'Basic ' . base64_encode($headers['PHP_AUTH_USER'] . ':' . $headers['PHP_AUTH_PW']);
82
        } elseif (isset($headers['PHP_AUTH_DIGEST'])) {
83
            $headers['AUTHORIZATION'] = $headers['PHP_AUTH_DIGEST'];
84
        }
85
86
        parent::__construct($headers);
87
    }
88
}