Settings::define()   C
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 30
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 8

Importance

Changes 0
Metric Value
cc 8
eloc 27
nc 8
nop 2
dl 0
loc 30
ccs 24
cts 24
cp 1
crap 8
rs 5.3846
c 0
b 0
f 0
1
<?php
2
namespace FMUP\Queue\Channel;
3
4
use FMUP\Queue\Exception as QueueException;
5
6
/**
7
 * Class Settings - Global channel settings
8
 * @package FMUP\Queue\Channel
9
 */
10
class Settings
11
{
12
    //(int) in bytes (default system)
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
13
    const PARAM_MAX_MESSAGE_SIZE = 'PARAM_MAX_MESSAGE_SIZE';
14
    //(int) max send retry time, default DEFAULT_RETRY_TIMES
15
    const PARAM_MAX_SEND_RETRY_TIME = 'PARAM_MAX_SEND_RETRY_TIME';
16
    const PARAM_CONSUMER_NAME = 'PARAM_CONSUMER_NAME';//(string) define consumer name - default empty
17
18
    //(bool) if process must wait to be sure the message is sent (default false)
19
    const PARAM_BLOCK_SEND = 'PARAM_BLOCK_SEND';
20
    //(bool) process will be blocked while no message is received (default false)
21
    const PARAM_BLOCK_RECEIVE = 'PARAM_BLOCK_RECEIVE';
22
    //(bool) must serialize a message (default true)
23
    const PARAM_SERIALIZE = 'PARAM_SERIALIZE';
24
    const PARAM_AUTO_ACK = 'PARAM_AUTO_ACK'; //(bool) must ack a message as soon as it is retrieved (default false)
25
26
    const DEFAULT_RETRY_TIMES = 3;
27
28
    const FLAG_BLOCK_SEND = 1;
29
    const FLAG_BLOCK_RECEIVE = 2;
30
    const FLAG_SERIALIZE = 4;
31
    const FLAG_AUTO_ACK = 8;
32
33
    private $settings = array();
34
    private $consumerName = '';
35
    private $flags = self::FLAG_SERIALIZE;
36
37
    /**
38
     * @param array $settings
39
     */
40 14
    public function __construct(array $settings = array())
41
    {
42 14
        $this->defineByArray($settings);
43 12
    }
44
45
    /**
46
     * Define multiple settings by a single array
47
     * @param array $settings
48
     * @return $this
49
     * @throws QueueException
50
     */
51 14
    public function defineByArray(array $settings)
52
    {
53 14
        foreach ($settings as $setting => $value) {
54 4
            $this->define($setting, $value);
55
        }
56 12
        return $this;
57
    }
58
59
    /**
60
     * Define a single setting by its name
61
     * @param string $setting
62
     * @param mixed $value
63
     * @return $this
64
     * @throws QueueException
65
     */
66 3
    public function define($setting, $value)
67
    {
68
        switch ($setting) {
69 3
            case self::PARAM_MAX_MESSAGE_SIZE:
70 1
                $this->setMaxMessageSize($value);
71 1
                break;
72 3
            case self::PARAM_BLOCK_SEND:
73 1
                $this->setBlockSend($value);
74 1
                break;
75 3
            case self::PARAM_SERIALIZE:
76 1
                $this->setSerialize($value);
77 1
                break;
78 3
            case self::PARAM_BLOCK_RECEIVE:
79 1
                $this->setBlockReceive($value);
80 1
                break;
81 3
            case self::PARAM_MAX_SEND_RETRY_TIME:
82 1
                $this->setMaxSendRetryTime($value);
83 1
                break;
84 3
            case self::PARAM_AUTO_ACK:
85 1
                $this->setAutoAck($value);
86 1
                break;
87 3
            case self::PARAM_CONSUMER_NAME:
88 1
                $this->setConsumerName($value);
89 1
                break;
90
            default:
91 2
                throw new QueueException('Setting is not defined');
92
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
93
        }
94 1
        return $this;
95
    }
96
97
    /**
98
     * Define consumer name
99
     * @param string $consumerName
100
     * @return $this
101
     */
102 1
    public function setConsumerName($consumerName = '')
103
    {
104 1
        $this->consumerName = (string)$consumerName;
105 1
        return $this;
106
    }
107
108
    /**
109
     * Retrieve defined consumer name
110
     * @return string
111
     */
112 1
    public function getConsumerName()
113
    {
114 1
        return (string)$this->consumerName;
115
    }
116
117
    /**
118
     * Define max message size (default system)
119
     * @param int $size
120
     * @return $this
121
     */
122 4
    public function setMaxMessageSize($size = 0)
123
    {
124 4
        $this->settings[self::PARAM_MAX_MESSAGE_SIZE] = (int)$size;
125 4
        return $this;
126
    }
127
128
    /**
129
     * Get max message size (default system)
130
     * @return int
131
     */
132 3
    public function getMaxMessageSize()
133
    {
134 3
        return isset($this->settings[self::PARAM_MAX_MESSAGE_SIZE])
135 3
            ? (int)$this->settings[self::PARAM_MAX_MESSAGE_SIZE]
136 3
            : 0;
137
    }
138
139
    /**
140
     * Define if process must wait to be sure the message is sent (default false)
141
     * @param bool|false $blockSend
142
     * @return $this
143
     */
144 3
    public function setBlockSend($blockSend = false)
145
    {
146 3
        $this->flags = ($blockSend) ? $this->flags | self::FLAG_BLOCK_SEND : $this->flags & ~self::FLAG_BLOCK_SEND;
147 3
        return $this;
148
    }
149
150
    /**
151
     * Get if process must wait to be sure the message is sent (default false)
152
     * @return bool
153
     */
154 3
    public function getBlockSend()
155
    {
156 3
        return (bool)($this->flags & self::FLAG_BLOCK_SEND);
157
    }
158
159
    /**
160
     * Define if process must serialize a message (default true)
161
     * @param bool|true $serialize
162
     * @return $this
163
     */
164 3
    public function setSerialize($serialize = true)
165
    {
166 3
        $this->flags = ($serialize) ? $this->flags | self::FLAG_SERIALIZE : $this->flags & ~self::FLAG_SERIALIZE;
167 3
        return $this;
168
    }
169
170
    /**
171
     * Get if process must serialize a message (default true)
172
     * @return bool
173
     */
174 5
    public function getSerialize()
175
    {
176 5
        return (bool)($this->flags & self::FLAG_SERIALIZE);
177
    }
178
179
    /**
180
     * Define if process will be blocked while no message is received (default false)
181
     * @param bool|false $blockReceive
182
     * @return $this
183
     */
184 3
    public function setBlockReceive($blockReceive = false)
185
    {
186 3
        $this->flags = ($blockReceive)
187 2
            ? $this->flags | self::FLAG_BLOCK_RECEIVE
188 2
            : $this->flags & ~self::FLAG_BLOCK_RECEIVE;
189 3
        return $this;
190
    }
191
192
    /**
193
     * Get if process will be blocked while no message is received (default false)
194
     * @return bool
195
     */
196 3
    public function getBlockReceive()
197
    {
198 3
        return (bool)($this->flags & self::FLAG_BLOCK_RECEIVE);
199
    }
200
201
    /**
202
     * Define if message are auto ack-ed (default false)
203
     * @param bool|false $autoAck
204
     * @return $this
205
     */
206 2
    public function setAutoAck($autoAck = false)
207
    {
208 2
        $this->flags = ($autoAck) ? $this->flags | self::FLAG_AUTO_ACK : $this->flags & ~self::FLAG_AUTO_ACK;
209 2
        return $this;
210
    }
211
212
    /**
213
     * Get if message are auto acked on this channel
214
     * @return bool
215
     */
216 1
    public function getAutoAck()
217
    {
218 1
        return (bool)($this->flags & self::FLAG_AUTO_ACK);
219
    }
220
221
    /**
222
     * Define max send retry time, default DEFAULT_RETRY_TIMES
223
     * @param int $maxRetry
224
     * @return $this
225
     */
226 3
    public function setMaxSendRetryTime($maxRetry = 0)
227
    {
228 3
        $this->settings[self::PARAM_MAX_SEND_RETRY_TIME] = (int)$maxRetry;
229 3
        return $this;
230
    }
231
232
    /**
233
     * Get max message size (default system)
234
     * @return int
235
     */
236 3
    public function getMaxSendRetryTime()
237
    {
238 3
        return isset($this->settings[self::PARAM_MAX_SEND_RETRY_TIME])
239 3
            ? (int)$this->settings[self::PARAM_MAX_SEND_RETRY_TIME]
240 3
            : self::DEFAULT_RETRY_TIMES;
241
    }
242
}
243