Completed
Push — master ( 42bf17...fdb7de )
by Sergey
04:25
created

Properties::encode()   F

Complexity

Conditions 15
Paths 16384

Size

Total Lines 77
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 77
rs 2.1927
cc 15
eloc 46
nc 16384
nop 1

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 automatically generated.
4
 */
5
6
namespace ButterAMQP\Framing;
7
8
use ButterAMQP\Buffer;
9
use ButterAMQP\Value;
10
11
/**
12
 * Helper to encode and decode basic properties.
13
 *
14
 * @codeCoverageIgnore
15
 */
16
class Properties
17
{
18
    /**
19
     * This class can not be instantiated.
20
     */
21
    private function __construct()
22
    {
23
    }
24
25
    /**
26
     * @param array $properties
27
     *
28
     * @return string
29
     */
30
    public static function encode(array $properties)
31
    {
32
        $flags = 0;
33
        $payload = '';
34
35
        if (array_key_exists('content-type', $properties)) {
36
            $flags |= 32768;
37
            $payload .= Value\ShortStringValue::encode($properties['content-type']);
38
        }
39
40
        if (array_key_exists('content-encoding', $properties)) {
41
            $flags |= 16384;
42
            $payload .= Value\ShortStringValue::encode($properties['content-encoding']);
43
        }
44
45
        if (array_key_exists('headers', $properties)) {
46
            $flags |= 8192;
47
            $payload .= Value\TableValue::encode($properties['headers']);
48
        }
49
50
        if (array_key_exists('delivery-mode', $properties)) {
51
            $flags |= 4096;
52
            $payload .= Value\OctetValue::encode($properties['delivery-mode']);
53
        }
54
55
        if (array_key_exists('priority', $properties)) {
56
            $flags |= 2048;
57
            $payload .= Value\OctetValue::encode($properties['priority']);
58
        }
59
60
        if (array_key_exists('correlation-id', $properties)) {
61
            $flags |= 1024;
62
            $payload .= Value\ShortStringValue::encode($properties['correlation-id']);
63
        }
64
65
        if (array_key_exists('reply-to', $properties)) {
66
            $flags |= 512;
67
            $payload .= Value\ShortStringValue::encode($properties['reply-to']);
68
        }
69
70
        if (array_key_exists('expiration', $properties)) {
71
            $flags |= 256;
72
            $payload .= Value\ShortStringValue::encode($properties['expiration']);
73
        }
74
75
        if (array_key_exists('message-id', $properties)) {
76
            $flags |= 128;
77
            $payload .= Value\ShortStringValue::encode($properties['message-id']);
78
        }
79
80
        if (array_key_exists('timestamp', $properties)) {
81
            $flags |= 64;
82
            $payload .= Value\LongLongValue::encode($properties['timestamp']);
83
        }
84
85
        if (array_key_exists('type', $properties)) {
86
            $flags |= 32;
87
            $payload .= Value\ShortStringValue::encode($properties['type']);
88
        }
89
90
        if (array_key_exists('user-id', $properties)) {
91
            $flags |= 16;
92
            $payload .= Value\ShortStringValue::encode($properties['user-id']);
93
        }
94
95
        if (array_key_exists('app-id', $properties)) {
96
            $flags |= 8;
97
            $payload .= Value\ShortStringValue::encode($properties['app-id']);
98
        }
99
100
        if (array_key_exists('reserved', $properties)) {
101
            $flags |= 4;
102
            $payload .= Value\ShortStringValue::encode($properties['reserved']);
103
        }
104
105
        return Value\ShortValue::encode($flags).$payload;
106
    }
107
108
    /**
109
     * @param Buffer $data
110
     *
111
     * @return array
112
     */
113
    public static function decode(Buffer $data)
114
    {
115
        $flags = Value\ShortValue::decode($data);
116
        $properties = [];
117
118
        if ($flags & 32768) {
119
            $properties['content-type'] = Value\ShortStringValue::decode($data);
120
        }
121
122
        if ($flags & 16384) {
123
            $properties['content-encoding'] = Value\ShortStringValue::decode($data);
124
        }
125
126
        if ($flags & 8192) {
127
            $properties['headers'] = Value\TableValue::decode($data);
128
        }
129
130
        if ($flags & 4096) {
131
            $properties['delivery-mode'] = Value\OctetValue::decode($data);
132
        }
133
134
        if ($flags & 2048) {
135
            $properties['priority'] = Value\OctetValue::decode($data);
136
        }
137
138
        if ($flags & 1024) {
139
            $properties['correlation-id'] = Value\ShortStringValue::decode($data);
140
        }
141
142
        if ($flags & 512) {
143
            $properties['reply-to'] = Value\ShortStringValue::decode($data);
144
        }
145
146
        if ($flags & 256) {
147
            $properties['expiration'] = Value\ShortStringValue::decode($data);
148
        }
149
150
        if ($flags & 128) {
151
            $properties['message-id'] = Value\ShortStringValue::decode($data);
152
        }
153
154
        if ($flags & 64) {
155
            $properties['timestamp'] = Value\LongLongValue::decode($data);
156
        }
157
158
        if ($flags & 32) {
159
            $properties['type'] = Value\ShortStringValue::decode($data);
160
        }
161
162
        if ($flags & 16) {
163
            $properties['user-id'] = Value\ShortStringValue::decode($data);
164
        }
165
166
        if ($flags & 8) {
167
            $properties['app-id'] = Value\ShortStringValue::decode($data);
168
        }
169
170
        if ($flags & 4) {
171
            $properties['reserved'] = Value\ShortStringValue::decode($data);
172
        }
173
174
        return $properties;
175
    }
176
}
177