ContentHeaderFrame::unpack()   F
last analyzed

Complexity

Conditions 15
Paths 16384

Size

Total Lines 66
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 24.0716

Importance

Changes 0
Metric Value
cc 15
eloc 34
nc 16384
nop 1
dl 0
loc 66
ccs 23
cts 35
cp 0.6571
crap 24.0716
rs 1.7499
c 0
b 0
f 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
 * This file is part of PHPinnacle/Ridge.
4
 *
5
 * (c) PHPinnacle Team <[email protected]>
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 PHPinnacle\Ridge\Protocol;
12
13
use PHPinnacle\Ridge\Buffer;
14
use PHPinnacle\Ridge\Constants;
15
16
class ContentHeaderFrame extends AbstractFrame
17
{
18
    const
19
        FLAG_CONTENT_TYPE     = 0x8000,
20
        FLAG_CONTENT_ENCODING = 0x4000,
21
        FLAG_HEADERS          = 0x2000,
22
        FLAG_DELIVERY_MODE    = 0x1000,
23
        FLAG_PRIORITY         = 0x0800,
24
        FLAG_CORRELATION_ID   = 0x0400,
25
        FLAG_REPLY_TO         = 0x0200,
26
        FLAG_EXPIRATION       = 0x0100,
27
        FLAG_MESSAGE_ID       = 0x0080,
28
        FLAG_TIMESTAMP        = 0x0040,
29
        FLAG_TYPE             = 0x0020,
30
        FLAG_USER_ID          = 0x0010,
31
        FLAG_APP_ID           = 0x0008,
32
        FLAG_CLUSTER_ID       = 0x0004
33
    ;
34
35
    /**
36
     * @var int
37
     */
38
    public $classId = Constants::CLASS_BASIC;
39
40
    /**
41
     * @var int
42
     */
43
    public $weight = 0;
44
45
    /**
46
     * @var int
47
     */
48
    public $bodySize;
49
50
    /**
51
     * @var int
52
     */
53
    public $flags = 0;
54
55
    /**
56
     * @var string
57
     */
58
    public $contentType;
59
60
    /**
61
     * @var string
62
     */
63
    public $contentEncoding;
64
65
    /**
66
     * @var array
67
     */
68
    public $headers;
69
70
    /**
71
     * @var int
72
     */
73
    public $deliveryMode;
74
75
    /**
76
     * @var int
77
     */
78
    public $priority;
79
80
    /**
81
     * @var string
82
     */
83
    public $correlationId;
84
85
    /**
86
     * @var string
87
     */
88
    public $replyTo;
89
90
    /**
91
     * @var string
92
     */
93
    public $expiration;
94
95
    /**
96
     * @var string
97
     */
98
    public $messageId;
99
100
    /**
101
     * @var \DateTimeInterface
102
     */
103
    public $timestamp;
104
105
    /**
106
     * @var string
107
     */
108
    public $typeHeader;
109
110
    /**
111
     * @var string
112
     */
113
    public $userId;
114
115
    /**
116
     * @var string
117
     */
118
    public $appId;
119
120
    /**
121
     * @var string
122
     */
123
    public $clusterId;
124
125 13
    public function __construct()
126
    {
127 13
        parent::__construct(Constants::FRAME_HEADER);
128 13
    }
129
130
    /**
131
     * @param Buffer $buffer
132
     *
133
     * @return ContentHeaderFrame
134
     */
135 13
    public static function unpack(Buffer $buffer): self
136
    {
137 13
        $self = new self;
138
139 13
        $self->classId  = $buffer->consumeUint16();
140 13
        $self->weight   = $buffer->consumeUint16();
141 13
        $self->bodySize = $buffer->consumeUint64();
142 13
        $self->flags    = $flags = $buffer->consumeUint16();
143
    
144 13
        if ($flags & self::FLAG_CONTENT_TYPE) {
145 1
            $self->contentType = $buffer->consumeString();
146
        }
147
    
148 13
        if ($flags & self::FLAG_CONTENT_ENCODING) {
149
            $self->contentEncoding = $buffer->consumeString();
150
        }
151
    
152 13
        if ($flags & self::FLAG_HEADERS) {
153 1
            $self->headers = $buffer->consumeTable();
154
        }
155
    
156 13
        if ($flags & self::FLAG_DELIVERY_MODE) {
157
            $self->deliveryMode = $buffer->consumeUint8();
158
        }
159
    
160 13
        if ($flags & self::FLAG_PRIORITY) {
161
            $self->priority = $buffer->consumeUint8();
162
        }
163
    
164 13
        if ($flags & self::FLAG_CORRELATION_ID) {
165
            $self->correlationId = $buffer->consumeString();
166
        }
167
    
168 13
        if ($flags & self::FLAG_REPLY_TO) {
169
            $self->replyTo = $buffer->consumeString();
170
        }
171
    
172 13
        if ($flags & self::FLAG_EXPIRATION) {
173
            $self->expiration = $buffer->consumeString();
174
        }
175
    
176 13
        if ($flags & self::FLAG_MESSAGE_ID) {
177
            $self->messageId = $buffer->consumeString();
178
        }
179
    
180 13
        if ($flags & self::FLAG_TIMESTAMP) {
181
            $self->timestamp = $buffer->consumeTimestamp();
182
        }
183
    
184 13
        if ($flags & self::FLAG_TYPE) {
185
            $self->typeHeader = $buffer->consumeString();
186
        }
187
    
188 13
        if ($flags & self::FLAG_USER_ID) {
189
            $self->userId = $buffer->consumeString();
190
        }
191
    
192 13
        if ($flags & self::FLAG_APP_ID) {
193
            $self->appId = $buffer->consumeString();
194
        }
195
    
196 13
        if ($flags & self::FLAG_CLUSTER_ID) {
197
            $self->clusterId = $buffer->consumeString();
198
        }
199
        
200 13
        return $self;
201
    }
202
    
203
    /**
204
     * @return Buffer
205
     */
206
    public function pack(): Buffer
207
    {
208
        $buffer = new Buffer;
209
        $buffer
210
            ->appendUint16($this->classId)
211
            ->appendUint16($this->weight)
212
            ->appendUint64($this->bodySize)
213
        ;
214
    
215
        $flags = $this->flags;
216
    
217
        $buffer->appendUint16($flags);
218
    
219
        if ($flags & ContentHeaderFrame::FLAG_CONTENT_TYPE) {
220
            $buffer->appendString($this->contentType);
221
        }
222
    
223
        if ($flags & ContentHeaderFrame::FLAG_CONTENT_ENCODING) {
224
            $buffer->appendString($this->contentEncoding);
225
        }
226
    
227
        if ($flags & ContentHeaderFrame::FLAG_HEADERS) {
228
            $buffer->appendTable($this->headers);
229
        }
230
    
231
        if ($flags & ContentHeaderFrame::FLAG_DELIVERY_MODE) {
232
            $buffer->appendUint8($this->deliveryMode);
233
        }
234
    
235
        if ($flags & ContentHeaderFrame::FLAG_PRIORITY) {
236
            $buffer->appendUint8($this->priority);
237
        }
238
    
239
        if ($flags & ContentHeaderFrame::FLAG_CORRELATION_ID) {
240
            $buffer->appendString($this->correlationId);
241
        }
242
    
243
        if ($flags & ContentHeaderFrame::FLAG_REPLY_TO) {
244
            $buffer->appendString($this->replyTo);
245
        }
246
    
247
        if ($flags & ContentHeaderFrame::FLAG_EXPIRATION) {
248
            $buffer->appendString($this->expiration);
249
        }
250
    
251
        if ($flags & ContentHeaderFrame::FLAG_MESSAGE_ID) {
252
            $buffer->appendString($this->messageId);
253
        }
254
    
255
        if ($flags & ContentHeaderFrame::FLAG_TIMESTAMP) {
256
            $buffer->appendTimestamp($this->timestamp);
257
        }
258
    
259
        if ($flags & ContentHeaderFrame::FLAG_TYPE) {
260
            $buffer->appendString($this->typeHeader);
261
        }
262
    
263
        if ($flags & ContentHeaderFrame::FLAG_USER_ID) {
264
            $buffer->appendString($this->userId);
265
        }
266
    
267
        if ($flags & ContentHeaderFrame::FLAG_APP_ID) {
268
            $buffer->appendString($this->appId);
269
        }
270
    
271
        if ($flags & ContentHeaderFrame::FLAG_CLUSTER_ID) {
272
            $buffer->appendString($this->clusterId);
273
        }
274
    
275
        return $buffer;
276
    }
277
278
    /**
279
     * @return array
280
     */
281 13
    public function toArray(): array
282
    {
283 13
        $headers = $this->headers ?: [];
284
285 13
        if ($this->contentType !== null) {
286 1
            $headers['content-type'] = $this->contentType;
287
        }
288
289 13
        if ($this->contentEncoding !== null) {
290
            $headers['content-encoding'] = $this->contentEncoding;
291
        }
292
293 13
        if ($this->deliveryMode !== null) {
294
            $headers['delivery-mode'] = $this->deliveryMode;
295
        }
296
297 13
        if ($this->priority !== null) {
298
            $headers['priority'] = $this->priority;
299
        }
300
301 13
        if ($this->correlationId !== null) {
302
            $headers['correlation-id'] = $this->correlationId;
303
        }
304
305 13
        if ($this->replyTo !== null) {
306
            $headers['reply-to'] = $this->replyTo;
307
        }
308
309 13
        if ($this->expiration !== null) {
310
            $headers['expiration'] = $this->expiration;
311
        }
312
313 13
        if ($this->messageId !== null) {
314
            $headers['message-id'] = $this->messageId;
315
        }
316
317 13
        if ($this->timestamp !== null) {
318
            $headers['timestamp'] = $this->timestamp;
319
        }
320
321 13
        if ($this->typeHeader !== null) {
322
            $headers['type'] = $this->typeHeader;
323
        }
324
325 13
        if ($this->userId !== null) {
326
            $headers['user-id'] = $this->userId;
327
        }
328
329 13
        if ($this->appId !== null) {
330
            $headers['app-id'] = $this->appId;
331
        }
332
333 13
        if ($this->clusterId !== null) {
334
            $headers['cluster-id'] = $this->clusterId;
335
        }
336
337 13
        return $headers;
338
    }
339
}
340