Response::send()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 27
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 4
nop 1
dl 0
loc 27
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Stores response data and headers
5
 *
6
 * PHP Version 5
7
 *
8
 * @category  Core
9
 * @package   HTTP
10
 * @author    Hans-Joachim Piepereit <[email protected]>
11
 * @copyright 2013 cSphere Team
12
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
13
 * @link      http://www.csphere.eu
14
 **/
15
16
namespace csphere\core\http;
17
18
/**
19
 * Stores response data and headers
20
 *
21
 * @category  Core
22
 * @package   HTTP
23
 * @author    Hans-Joachim Piepereit <[email protected]>
24
 * @copyright 2013 cSphere Team
25
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
26
 * @link      http://www.csphere.eu
27
 **/
28
29
abstract class Response
30
{
31
    /**
32
     * Array with headers to be send
33
     **/
34
    private static $_headers = [];
35
36
    /**
37
     * Store if send was already called
38
     **/
39
    private static $_status = false;
40
41
    /**
42
     * Whether to manually compress the output
43
     **/
44
    private static $_zlib = false;
45
46
    /**
47
     * Send headers and echo content
48
     *
49
     * @param string $string Content as string
50
     *
51
     * @return string
52
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
53
54
    public static function send($string)
55
    {
56
        // Update status
57
        self::$_status = true;
58
59
        // Send headers first as they are most important
60
        if (is_array(self::$_headers)) {
61
62
            foreach (self::$_headers AS $name => $content) {
63
64
                header($name . ': ' . $content);
65
            }
66
        }
67
68
        // Response is always ok
69
        header(':', true, 200);
70
71
        // Enable zlib output compression if enabled
72
        if (!empty(self::$_zlib) && extension_loaded('zlib')) {
73
74
            // This is preferred over using ob_gzhandler
75
            ini_set('zlib.output_compression', 1);
76
        }
77
78
        // Content should follow for completion
79
        echo $string;
80
    }
81
82
    /**
83
     * Allows for a new header to be stored
84
     *
85
     * @param string  $name    Name of header, e.g. Content-Type
86
     * @param string  $content Content of header, e.g. text/html
87
     * @param boolean $replace Replace earlier entries with same name
88
     *
89
     * @throws \Exception
90
     *
91
     * @return boolean
92
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
93
94
    public static function header($name, $content, $replace = false)
95
    {
96
        // Check if the header is already stored
97
        if (!isset(self::$_headers[$name]) || $replace == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
98
99
            self::$_headers[$name] = $content;
100
        } else {
101
102
            throw new \Exception('This header was already set');
103
        }
104
105
        return true;
106
    }
107
108
    /**
109
     * Allows for compression changes
110
     *
111
     * @param boolean $zlib Defines if output gets manually compressed
112
     *
113
     * @return boolean
114
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
115
116
    public static function compression($zlib)
117
    {
118
        self::$_zlib = (boolean)$zlib;
119
120
        return true;
121
    }
122
123
    /**
124
     * Checks if content was already send
125
     *
126
     * @return boolean
127
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
128
129
    public static function status()
130
    {
131
        return self::$_status;
132
    }
133
}
134