HttpEncoding   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 215
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 1
dl 0
loc 215
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A encodeContent() 0 4 1
A decodeContent() 0 4 1
A getLevel() 0 4 1
A setLevel() 0 10 3
A getEncoding() 0 4 1
A setEncoding() 0 6 1
A getContent() 0 4 1
A getLength() 0 4 1
A encode() 0 19 4
A computeContentLength() 0 11 4
A decode() 0 17 4
A __toString() 0 4 1
1
<?php
2
/*
3
 * This file is part of the Borobudur-Http package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\Http;
12
13
use Borobudur\Http\Exception\InvalidArgumentException;
14
15
/**
16
 * @author      Iqbal Maulana <[email protected]>
17
 * @created     7/31/15
18
 */
19
class HttpEncoding
20
{
21
    const GZIP = 'gzip';
22
    const DEFLATE = 'deflate';
23
    const NONE = 'none';
24
25
    /**
26
     * @var string
27
     */
28
    private $content;
29
30
    /**
31
     * @var int
32
     */
33
    private $length;
34
35
    /**
36
     * @var string
37
     */
38
    private $encoding;
39
40
    /**
41
     * @var int
42
     */
43
    private $level;
44
45
    /**
46
     * Constructor.
47
     *
48
     * @param string $content
49
     * @param string $encoding
50
     * @param int    $level
51
     */
52
    public function __construct($content, $encoding, $level = -1)
53
    {
54
        $this->content = $content;
55
        $this->setEncoding($encoding);
56
        $this->setLevel($level);
57
    }
58
59
    /**
60
     * Factory encode content.
61
     *
62
     * @param string $content
63
     * @param string $encoding
64
     * @param int    $level
65
     *
66
     * @return mixed
67
     */
68
    public static function encodeContent($content, $encoding, $level = -1)
69
    {
70
        return (new static($content, $encoding, $level))->encode();
71
    }
72
73
    /**
74
     * Factory decode content.
75
     *
76
     * @param string $content
77
     * @param string $encoding
78
     * @param int    $level
79
     *
80
     * @return mixed
81
     */
82
    public static function decodeContent($content, $encoding, $level = -1)
83
    {
84
        return (new static($content, $encoding, $level))->decode();
85
    }
86
87
    /**
88
     * Get encoding level.
89
     *
90
     * @return int
91
     */
92
    public function getLevel()
93
    {
94
        return $this->level;
95
    }
96
97
    /**
98
     * Set encoding level.
99
     *
100
     * @param int $level
101
     *
102
     * @return $this
103
     */
104
    public function setLevel($level)
105
    {
106
        if ($level < -1 && $level > 9) {
107
            throw new InvalidArgumentException(sprintf('Level should be -1 or between 0 and 9, "%s" given.', $level));
108
        }
109
110
        $this->level = $level;
111
112
        return $this;
113
    }
114
115
    /**
116
     * Get encoding.
117
     *
118
     * @return string
119
     */
120
    public function getEncoding()
121
    {
122
        return $this->encoding;
123
    }
124
125
    /**
126
     * Set encoding.
127
     *
128
     * @param string $encoding
129
     *
130
     * @return $this
131
     */
132
    public function setEncoding($encoding)
133
    {
134
        $this->encoding = $encoding;
135
136
        return $this;
137
    }
138
139
    /**
140
     * Get content.
141
     *
142
     * @return string
143
     */
144
    public function getContent()
145
    {
146
        return $this->content;
147
    }
148
149
    /**
150
     * Get content length.
151
     *
152
     * @return int
153
     */
154
    public function getLength()
155
    {
156
        return $this->length;
157
    }
158
159
    /**
160
     * Encode content.
161
     *
162
     * @return $this
163
     */
164
    public function encode()
165
    {
166
        switch ($this->encoding) {
167
            case HttpEncoding::GZIP:
168
                $this->content = gzcompress($this->content, $this->level, ZLIB_ENCODING_GZIP);
169
                break;
170
            case HttpEncoding::DEFLATE:
171
                $this->content = gzcompress($this->content, $this->level, ZLIB_ENCODING_DEFLATE);
172
                break;
173
            case HttpEncoding::NONE:
174
                break;
175
            default:
176
                throw new InvalidArgumentException(sprintf('Unsupported "%s" encoding.', $this->encoding));
177
        }
178
179
        $this->length = $this->computeContentLength();
180
181
        return $this;
182
    }
183
184
    /**
185
     * Get computed content length.
186
     *
187
     * @return int
188
     */
189
    private function computeContentLength()
190
    {
191
        if ((function_exists('mb_strlen')
192
            && (ini_get('mbstring.func_overload') !== '')
193
            && ((int) ini_get('mbstring.func_overload') & 2))
194
        ) {
195
            return mb_strlen($this->content, '8bit');
196
        }
197
198
        return strlen($this->content);
199
    }
200
201
    /**
202
     * Decode content.
203
     *
204
     * @return $this
205
     */
206
    public function decode()
207
    {
208
        switch ($this->encoding) {
209
            case HttpEncoding::GZIP:
210
            case HttpEncoding::DEFLATE:
211
                $this->content = gzuncompress($this->content);
212
                break;
213
            case HttpEncoding::NONE:
214
                break;
215
            default:
216
                throw new InvalidArgumentException(sprintf('Unsupported "%s" encoding.', $this->encoding));
217
        }
218
219
        $this->length = $this->computeContentLength();
220
221
        return $this;
222
    }
223
224
    /**
225
     * Cast HttpEncoding to encoded or decoded content.
226
     *
227
     * @return string
228
     */
229
    public function __toString()
230
    {
231
        return $this->getContent();
232
    }
233
}
234