Completed
Branch 0.4-dev (f2f961)
by Evgenij
02:48
created

RequestDescriptor::setState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 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
    /**
75
     * Flag if this socket is postponed
76
     *
77
     * @var bool
78
     */
79
    private $isPostponed = false;
80
81
    /**
82
     * Set of state flags: RDS_* consts
83
     *
84
     * @var int
85
     */
86
    private $state = 0;
87
88
    /**
89
     * RequestDescriptor constructor.
90
     *
91
     * @param SocketInterface       $socket Socket object
92
     * @param OperationInterface    $operation Operation to perform on socket
93
     * @param array                 $metadata Metadata
94
     * @param EventHandlerInterface $handlers Handlers for this socket
95
     */
96 158
    public function __construct(
97
        SocketInterface $socket,
98
        OperationInterface $operation,
99
        array $metadata,
100
        EventHandlerInterface $handlers = null
101
    ) {
102 158
        $this->socket    = $socket;
103 158
        $this->operation = $operation;
104 158
        $this->metadata  = $metadata;
105 158
        $this->handlers  = $handlers;
106 158
        $this->initialize();
107 158
    }
108
109
    /**
110
     * Return Operation
111
     *
112
     * @return OperationInterface
113
     */
114 99
    public function getOperation()
115
    {
116 99
        return $this->operation;
117
    }
118
119
    /**
120
     * Sets Operation
121
     *
122
     * @param OperationInterface $operation New operation
123
     *
124
     * @return void
125
     */
126 49
    public function setOperation(OperationInterface $operation)
127
    {
128 49
        $this->operation = $operation;
129 49
    }
130
131
    /**
132
     * Initialize data before request
133
     *
134
     * @return void
135
     */
136 158
    public function initialize()
137
    {
138 158
        $this->isRunning = false;
139 158
    }
140
141
    /**
142
     * Return flag whether request is running
143
     *
144
     * @return boolean
145
     */
146 127
    public function isRunning()
147
    {
148 127
        return $this->isRunning;
149
    }
150
151
    /**
152
     * Sets running flag
153
     *
154
     * @param boolean $isRunning New value for IsRunning
155
     *
156
     * @return void
157
     */
158 98
    public function setRunning($isRunning)
159
    {
160 98
        $this->isRunning = $isRunning;
161 98
    }
162
163
    /**
164
     * Return Socket
165
     *
166
     * @return SocketInterface
167
     */
168 126
    public function getSocket()
169
    {
170 126
        return $this->socket;
171
    }
172
173
    /** {@inheritdoc} */
174 48
    public function getStreamResource()
175
    {
176 48
        return $this->socket->getStreamResource();
177
    }
178
179
    /**
180
     * Return key-value array with metadata
181
     *
182
     * @return array
183
     */
184 145
    public function getMetadata()
185
    {
186 145
        return $this->metadata;
187
    }
188
189
    /**
190
     * Set metadata for given socket
191
     *
192
     * @param string|array    $key Either string or key-value array of metadata. If string, then value must be
193
     *                             passed in third argument, if array, then third argument will be ignored
194
     * @param mixed           $value Value for key or null, if $key is array
195
     *
196
     * @return void
197
     */
198 140
    public function setMetadata($key, $value = null)
199
    {
200 140
        if (!is_array($key)) {
201 130
            $this->metadata[$key] = $value;
202 130
        } else {
203 20
            $this->metadata = array_merge(
204 20
                $this->metadata,
205
                $key
206 20
            );
207
        }
208 140
    }
209
210
    /** {@inheritdoc} */
211 125
    public function invokeEvent(Event $event)
212
    {
213 125
        if ($this->handlers) {
214 21
            $this->handlers->invokeEvent($event);
215 17
        }
216 121
    }
217
218
    /**
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 6
    public function postpone()
225
    {
226 6
        if (!($this->socket instanceof PersistentClientSocket)) {
227 5
            return;
228
        }
229
230 1
        $this->isPostponed = true;
231 1
    }
232
233
    /**
234
     * Return true, if this socket shouldn't be processed by executor engine
235
     *
236
     * @return bool
237
     */
238 112
    public function isPostponed()
239
    {
240 112
        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 100
    public function hasState($state)
251
    {
252 100
        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 118
    public function setState($state)
263
    {
264 118
        $this->state |= $state;
265 118
    }
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 100
    public function clearState($state)
275
    {
276 100
        $this->state &= ~$state;
277 100
    }
278
}
279