Request::getVersion()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Cassandra\Request;
3
use Cassandra\Protocol\Frame;
4
use Cassandra\Type;
5
6
class Request implements Frame{
7
8
    const CONSISTENCY_ANY = 0x0000;
9
    const CONSISTENCY_ONE = 0x0001;
10
    const CONSISTENCY_TWO = 0x0002;
11
    const CONSISTENCY_THREE = 0x0003;
12
    const CONSISTENCY_QUORUM = 0x0004;
13
    const CONSISTENCY_ALL = 0x0005;
14
    const CONSISTENCY_LOCAL_QUORUM = 0x0006;
15
    const CONSISTENCY_EACH_QUORUM = 0x0007;
16
    const CONSISTENCY_SERIAL = 0x0008;
17
    const CONSISTENCY_LOCAL_SERIAL = 0x0009;
18
    const CONSISTENCY_LOCAL_ONE = 0x000A;
19
    
20
    /**
21
     * @var int
22
     */
23
    protected $version = 0x03;
24
    
25
    /**
26
     * @var int
27
     */
28
    protected $opcode;
29
    
30
    /**
31
     * @var int
32
     */
33
    protected $stream = 0;
34
    
35
    /**
36
     * @var int
37
     */
38
    protected $flags = 0;
39
    
40
    /**
41
     * @param int $opcode
42
     * @param int $stream
43
     * @param int $flags
44
     */
45
    public function __construct($opcode, $stream = 0, $flags = 0) {
46
        $this->opcode = $opcode;
47
        $this->stream = $stream;
48
        $this->flags = $flags;
49
    }
50
        
51
    public function getVersion(){
52
        return $this->version;
53
    }
54
    
55
    public function getFlags(){
56
        return $this->flags;
57
    }
58
    
59
    public function getStream(){
60
        return $this->stream;
61
    }
62
    
63
    public function getOpcode(){
64
        return $this->opcode;
65
    }
66
    
67
    public function getBody(){
68
        return '';
69
    }
70
    
71
    public function setStream($stream){
72
        $this->stream = $stream;
73
    }
74
    
75
    /**
76
     * @return string
77
     */
78
    public function __toString(){
79
        $body = $this->getBody();
80
        return pack(
81
                'CCnCN',
82
                $this->version,
83
                $this->flags,
84
                $this->stream,
85
                $this->opcode,
86
                strlen($body)
87
        ) . $body;
88
    }
89
    
90
    /**
91
     * 
92
     * @param array $values
93
     * @throws Type\Exception
94
     * @return string
95
     */
96
    public static function valuesBinary(array $values, $namesForValues = false) {
97
        $valuesBinary = pack('n', count($values));
98
        
99
        $index = 0;
100
        foreach($values as $name => $value) {
101
            switch (true) {
102
                case $value instanceof Type\Base:
103
                    $binary = $value->getBinary();
104
                    break;
105
                case $value === null:
106
                    $binary = null;
107
                    break;
108
                case is_int($value):
109
                    $binary = pack('N', $value);
110
                    break;
111
                case is_string($value):
112
                    $binary = $value;
113
                    break;
114
                case is_bool($value):
115
                    $binary = $value ? chr(1) : chr(0);
116
                    break;
117
                default:
118
                    throw new Type\Exception('Unknown type.');
119
            }
120
121
            if ($namesForValues){
122
                $valuesBinary .= pack('n', strlen($name)) . strtolower($name);
123
            }
124
            else{
125
                /**
126
                 * @see https://github.com/duoshuo/php-cassandra/issues/29
127
                 */
128
                if ($index++ !== $name)
129
                    throw new Type\Exception('$values should be an sequential array, associative array given.  Or you can set "names_for_values" option to true.');
130
            }
131
132
            $valuesBinary .= $binary === null
133
                ? "\xff\xff\xff\xff"
134
                : pack('N', strlen($binary)) . $binary;
135
        }
136
        
137
        return $valuesBinary;
138
    }
139
    
140
    /**
141
     * 
142
     * @param array $values
143
     * @param array $columns
144
     * @return array
145
     */
146
    public static function strictTypeValues(array $values, array $columns) {
147
        $strictTypeValues = [];
148
        foreach($columns as $index => $column) {
149
            $key = array_key_exists($column['name'], $values) ? $column['name'] : $index;
150
            
151
            if (!isset($values[$key])){
152
                $strictTypeValues[$key] = null;
153
            }
154
            elseif($values[$key] instanceof Type\Base){
155
                $strictTypeValues[$key] = $values[$key];
156
            }
157
            else{
158
                $strictTypeValues[$key] = Type\Base::getTypeObject($column['type'], $values[$key]);
159
            }
160
        }
161
        
162
        return $strictTypeValues;
163
    }
164
    
165
    /**
166
     * 
167
     * @param int $consistency
168
     * @param array $values
169
     * @param array $options
170
     * @return string
171
     */
172
    public static function queryParameters($consistency, array $values = [], array $options = []){
173
        $flags = 0;
174
        $optional = '';
175
        
176
        if (!empty($values)) {
177
            $flags |= Query::FLAG_VALUES;
178
            $optional .= Request::valuesBinary($values, !empty($options['names_for_values']));
179
        }
180
181
        if (!empty($options['skip_metadata']))
182
            $flags |= Query::FLAG_SKIP_METADATA;
183
184 View Code Duplication
        if (isset($options['page_size'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
185
            $flags |= Query::FLAG_PAGE_SIZE;
186
            $optional .= pack('N', $options['page_size']);
187
        }
188
189
        if (isset($options['paging_state'])) {
190
            $flags |= Query::FLAG_WITH_PAGING_STATE;
191
            $optional .= pack('N', strlen($options['paging_state'])) . $options['paging_state'];
192
        }
193
194 View Code Duplication
        if (isset($options['serial_consistency'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
195
            $flags |= Query::FLAG_WITH_SERIAL_CONSISTENCY;
196
            $optional .= pack('n', $options['serial_consistency']);
197
        }
198
199 View Code Duplication
        if (isset($options['default_timestamp'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
200
            $flags |= Query::FLAG_WITH_DEFAULT_TIMESTAMP;
201
            $optional .= Type\Bigint::binary($options['default_timestamp']);
202
        }
203
204
        if (!empty($options['names_for_values']))
205
            $flags |= Query::FLAG_WITH_NAMES_FOR_VALUES;
206
207
        return pack('n', $consistency) . pack('C', $flags) . $optional;
208
    }
209
}
210