Completed
Branch 0.4-dev (999b58)
by Evgenij
18:41
created

RequestDescriptor   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 257
Duplicated Lines 0 %

Coupling/Cohesion

Components 5
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 19
c 2
b 0
f 0
lcom 5
cbo 1
dl 0
loc 257
ccs 46
cts 46
cp 1
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A getOperation() 0 4 1
A setOperation() 0 4 1
A initialize() 0 4 1
A isRunning() 0 4 1
A setRunning() 0 4 1
A getSocket() 0 4 1
A getStreamResource() 0 4 1
A getMetadata() 0 4 1
A setMetadata() 0 11 2
A invokeEvent() 0 6 2
A postpone() 0 8 2
A isPostponed() 0 4 1
A hasState() 0 4 1
A setState() 0 4 1
A clearState() 0 4 1
1
<?php
2
/**
3
 * Async sockets
4
 *
5
 * @copyright Copyright (c) 2015-2016, Efimov Evgenij <[email protected]>
6
 *
7
 * This source file is subject to the MIT license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
namespace AsyncSockets\RequestExecutor\Metadata;
11
12
use AsyncSockets\Event\Event;
13
use AsyncSockets\RequestExecutor\EventHandlerInterface;
14
use AsyncSockets\Operation\OperationInterface;
15
use AsyncSockets\Socket\PersistentClientSocket;
16
use AsyncSockets\Socket\SocketInterface;
17
use AsyncSockets\Socket\StreamResourceInterface;
18
19
/**
20
 * Class RequestDescriptor
21
 */
22
class RequestDescriptor implements StreamResourceInterface, EventHandlerInterface
23
{
24
    /**
25
     * This descriptor is ready for reading
26
     */
27
    const RDS_READ = 0x0001;
28
29
    /**
30
     * This descriptor is ready for writing
31
     */
32
    const RDS_WRITE = 0x0002;
33
34
    /**
35
     * This descriptor is has OOB data
36
     */
37
    const RDS_OOB = 0x0004;
38
39
    /**
40
     * Socket for this operation
41
     *
42
     * @var SocketInterface
43
     */
44
    private $socket;
45
46
    /**
47
     * Key-value pairs with meta information
48
     *
49
     * @var array
50
     */
51
    private $metadata;
52
53
    /**
54
     * Event handler object
55
     *
56
     * @var EventHandlerInterface
57
     */
58
    private $handlers;
59
60
    /**
61
     * Flag whether this socket request is running
62
     *
63
     * @var bool
64
     */
65
    private $isRunning;
66
67
    /**
68
     * Operation to perform on socket
69
     *
70
     * @var OperationInterface
71
     */
72
    private $operation;
73
74 158
    /**
75
     * Flag if this socket is postponed
76
     *
77
     * @var bool
78
     */
79
    private $isPostponed = false;
80 158
81 158
    /**
82 158
     * Set of state flags: RDS_* consts
83 158
     *
84 158
     * @var int
85 158
     */
86
    private $state = 0;
87
88
    /**
89
     * RequestDescriptor constructor.
90
     *
91
     * @param SocketInterface       $socket Socket object
92 99
     * @param OperationInterface    $operation Operation to perform on socket
93
     * @param array                 $metadata Metadata
94 99
     * @param EventHandlerInterface $handlers Handlers for this socket
95
     */
96
    public function __construct(
97
        SocketInterface $socket,
98
        OperationInterface $operation,
99
        array $metadata,
100
        EventHandlerInterface $handlers = null
101
    ) {
102
        $this->socket    = $socket;
103
        $this->operation = $operation;
104 49
        $this->metadata  = $metadata;
105
        $this->handlers  = $handlers;
106 49
        $this->initialize();
107 49
    }
108
109
    /**
110
     * Return Operation
111
     *
112
     * @return OperationInterface
113
     */
114 158
    public function getOperation()
115
    {
116 158
        return $this->operation;
117 158
    }
118
119
    /**
120
     * Sets Operation
121
     *
122
     * @param OperationInterface $operation New operation
123
     *
124 127
     * @return void
125
     */
126 127
    public function setOperation(OperationInterface $operation)
127
    {
128
        $this->operation = $operation;
129
    }
130
131
    /**
132
     * Initialize data before request
133
     *
134
     * @return void
135
     */
136 98
    public function initialize()
137
    {
138 98
        $this->isRunning = false;
139 98
    }
140
141
    /**
142
     * Return flag whether request is running
143
     *
144
     * @return boolean
145
     */
146 126
    public function isRunning()
147
    {
148 126
        return $this->isRunning;
149
    }
150
151
    /**
152 48
     * Sets running flag
153
     *
154 48
     * @param boolean $isRunning New value for IsRunning
155
     *
156
     * @return void
157
     */
158
    public function setRunning($isRunning)
159
    {
160
        $this->isRunning = $isRunning;
161
    }
162 145
163
    /**
164 145
     * Return Socket
165
     *
166
     * @return SocketInterface
167
     */
168
    public function getSocket()
169
    {
170
        return $this->socket;
171
    }
172
173
    /** {@inheritdoc} */
174
    public function getStreamResource()
175
    {
176 140
        return $this->socket->getStreamResource();
177
    }
178 140
179 130
    /**
180 130
     * Return key-value array with metadata
181 20
     *
182 20
     * @return array
183
     */
184 20
    public function getMetadata()
185
    {
186 140
        return $this->metadata;
187
    }
188
189 125
    /**
190
     * Set metadata for given socket
191 125
     *
192 21
     * @param string|array    $key Either string or key-value array of metadata. If string, then value must be
193 17
     *                             passed in third argument, if array, then third argument will be ignored
194 121
     * @param mixed           $value Value for key or null, if $key is array
195
     *
196
     * @return void
197
     */
198
    public function setMetadata($key, $value = null)
199
    {
200
        if (!is_array($key)) {
201
            $this->metadata[$key] = $value;
202 6
        } else {
203
            $this->metadata = array_merge(
204 6
                $this->metadata,
205 5
                $key
206
            );
207
        }
208 1
    }
209 1
210
    /** {@inheritdoc} */
211
    public function invokeEvent(Event $event)
212
    {
213
        if ($this->handlers) {
214
            $this->handlers->invokeEvent($event);
215
        }
216 112
    }
217
218 112
    /**
219
     * Completes processing this socket in event loop, but keep this socket connection opened. Applicable
220
     * only to persistent sockets, all other socket types are ignored by this method.
221
     *
222
     * @return void
223
     */
224
    public function postpone()
225
    {
226
        if (!($this->socket instanceof PersistentClientSocket)) {
227
            return;
228
        }
229
230
        $this->isPostponed = true;
231
    }
232
233
    /**
234
     * Return true, if this socket shouldn't be processed by executor engine
235
     *
236
     * @return bool
237
     */
238
    public function isPostponed()
239
    {
240
        return $this->isPostponed;
241
    }
242
243
    /**
244
     * Return true if descriptor has given state
245
     *
246
     * @param int $state State to check, set of RDS_* consts
247
     *
248
     * @return bool
249
     */
250
    public function hasState($state)
251
    {
252
        return (bool) ($this->state & $state);
253
    }
254
255
    /**
256
     * Sets one state into an object
257
     *
258
     * @param int $state State to set, set of RDS_* consts
259
     *
260
     * @return void
261
     */
262
    public function setState($state)
263
    {
264
        $this->state |= $state;
265
    }
266
267
    /**
268
     * Clears given state in object
269
     *
270
     * @param int $state State to clear, set of RDS_* consts
271
     *
272
     * @return void
273
     */
274
    public function clearState($state)
275
    {
276
        $this->state &= ~$state;
277
    }
278
}
279