Completed
Branch 0.4-dev (79cc15)
by Evgenij
03:32
created

SocketBag::resetSpeedRateCounters()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * Async sockets
4
 *
5
 * @copyright Copyright (c) 2015-2017, 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\Operation\OperationInterface;
13
use AsyncSockets\RequestExecutor\EventHandlerInterface;
14
use AsyncSockets\RequestExecutor\RequestExecutorInterface;
15
use AsyncSockets\RequestExecutor\SocketBagInterface;
16
use AsyncSockets\Socket\SocketInterface;
17
18
/**
19
 * Class SocketBag
20
 */
21
class SocketBag implements SocketBagInterface
22
{
23
    /**
24
     * RequestExecutorInterface
25
     *
26
     * @var RequestExecutorInterface
27
     */
28
    private $executor;
29
30
    /**
31
     * Target metadata items
32
     *
33
     * @var RequestDescriptor[]
34
     */
35
    private $items;
36
37
    /**
38
     * Default connection timeout
39
     *
40
     * @var double
41
     */
42
    private $connectTimeout;
43
44
    /**
45
     * Default I/O timeout
46
     *
47
     * @var double
48
     */
49
    private $ioTimeout;
50
51
    /**
52
     * SocketBag constructor.
53
     *
54
     * @param RequestExecutorInterface $executor Owner RequestExecutor
55
     * @param double                   $connectTimeout Default connection timeout
56
     * @param double                   $ioTimeout Default I/O timeout
57
     */
58 99
    public function __construct(RequestExecutorInterface $executor, $connectTimeout, $ioTimeout)
59
    {
60 99
        $this->executor       = $executor;
61 99
        $this->items          = [ ];
62 99
        $this->connectTimeout = $connectTimeout;
63 99
        $this->ioTimeout      = $ioTimeout;
64 99
    }
65
66
    /** {@inheritdoc} */
67 1
    public function count()
68
    {
69 1
        return count($this->items);
70
    }
71
72
73
    /** {@inheritdoc} */
74 89
    public function addSocket(
75
        SocketInterface $socket,
76
        OperationInterface $operation,
77
        array $metadata = null,
78
        EventHandlerInterface $eventHandlers = null
79
    ) {
80 89
        $hash = $this->getOperationStorageKey($socket);
81 89
        if (isset($this->items[$hash])) {
82 1
            throw new \LogicException('Can not add socket twice.');
83
        }
84
85 89
        $meta = array_merge(
86
            [
87 89
                RequestExecutorInterface::META_ADDRESS                    => null,
88
                RequestExecutorInterface::META_USER_CONTEXT               => null,
89
                RequestExecutorInterface::META_SOCKET_STREAM_CONTEXT      => null,
90
                RequestExecutorInterface::META_MIN_RECEIVE_SPEED          => null,
91
                RequestExecutorInterface::META_MIN_RECEIVE_SPEED_DURATION => null,
92 89
                RequestExecutorInterface::META_CONNECTION_TIMEOUT         => $this->connectTimeout,
93 89
                RequestExecutorInterface::META_IO_TIMEOUT                 => $this->ioTimeout,
94
            ],
95 89
            $metadata ?: [],
96
            [
97 89
                RequestExecutorInterface::META_CONNECTION_START_TIME  => null,
98
                RequestExecutorInterface::META_CONNECTION_FINISH_TIME => null,
99
                RequestExecutorInterface::META_LAST_IO_START_TIME     => null,
100
                RequestExecutorInterface::META_BYTES_SENT             => 0,
101
                RequestExecutorInterface::META_BYTES_RECEIVED         => 0,
102
                RequestExecutorInterface::META_REQUEST_COMPLETE       => false,
103
                RequestExecutorInterface::META_RECEIVE_SPEED          => 0,
104
            ]
105
        );
106
107 89
        $this->items[$hash] = new RequestDescriptor($socket, $operation, $meta, $eventHandlers);
108 89
    }
109
110
    /** {@inheritdoc} */
111 1
    public function getSocketOperation(SocketInterface $socket)
112
    {
113 1
        return $this->requireDescriptor($socket)->getOperation();
114
    }
115
116
    /** {@inheritdoc} */
117 1
    public function setSocketOperation(SocketInterface $socket, OperationInterface $operation)
118
    {
119 1
        $this->requireDescriptor($socket)->setOperation($operation);
120 1
    }
121
122
    /** {@inheritdoc} */
123 3
    public function hasSocket(SocketInterface $socket)
124
    {
125 3
        $hash = $this->getOperationStorageKey($socket);
126 3
        return isset($this->items[$hash]);
127
    }
128
129
    /** {@inheritdoc} */
130 3
    public function removeSocket(SocketInterface $socket)
131
    {
132 3
        $key = $this->getOperationStorageKey($socket);
133 3
        if (!isset($this->items[$key])) {
134 1
            return;
135
        }
136
137 2
        $meta = $this->items[$key]->getMetadata();
138 2
        if (!$meta[RequestExecutorInterface::META_REQUEST_COMPLETE] && $this->executor->isExecuting()) {
139 1
            throw new \LogicException('Can not remove unprocessed socket during request processing.');
140
        }
141
142 1
        unset($this->items[$key]);
143 1
    }
144
145
    /** {@inheritdoc} */
146 1
    public function postponeSocket(SocketInterface $socket)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
147
    {
148 1
        $key = $this->getOperationStorageKey($socket);
149 1
        if (!isset($this->items[$key])) {
150 1
            return;
151
        }
152
153
        $this->items[$key]->postpone();
154
    }
155
156
    /** {@inheritdoc} */
157 2
    public function resetSpeedRateCounters(SocketInterface $socket)
158
    {
159 2
        $descriptor = $this->requireDescriptor($socket);
160 1
        $counter    = $descriptor->getCounter(RequestDescriptor::COUNTER_TRANSFER_MIN_RATE);
161 1
        if ($counter) {
162 1
            $counter->reset();
163 1
            $descriptor->setMetadata(RequestExecutorInterface::META_RECEIVE_SPEED, null);
164
        }
165 1
    }
166
167
    /** {@inheritdoc} */
168 58
    public function getSocketMetaData(SocketInterface $socket)
169
    {
170 58
        return $this->requireDescriptor($socket)->getMetadata();
171
    }
172
173
    /** {@inheritdoc} */
174 14
    public function setSocketMetaData(SocketInterface $socket, $key, $value = null)
175
    {
176
        $writableKeys = [
177 14
            RequestExecutorInterface::META_ADDRESS                    => 1,
178
            RequestExecutorInterface::META_USER_CONTEXT               => 1,
179
            RequestExecutorInterface::META_CONNECTION_TIMEOUT         => 1,
180
            RequestExecutorInterface::META_IO_TIMEOUT                 => 1,
181
            RequestExecutorInterface::META_SOCKET_STREAM_CONTEXT      => 1,
182
            RequestExecutorInterface::META_MIN_RECEIVE_SPEED          => 1,
183
            RequestExecutorInterface::META_MIN_RECEIVE_SPEED_DURATION => 1,
184
        ];
185
186 14
        if (!is_array($key)) {
187 14
            $key = [ $key => $value ];
188
        }
189
190 14
        $key = array_intersect_key($key, $writableKeys);
191 14
        $this->requireDescriptor($socket)->setMetadata($key);
192 14
    }
193
194
    /**
195
     * Return socket key in internal storage
196
     *
197
     * @param SocketInterface $socket Socket object
198
     *
199
     * @return string
200
     */
201 92
    private function getOperationStorageKey(SocketInterface $socket)
202
    {
203 92
        return spl_object_hash($socket);
204
    }
205
206
    /**
207
     * Require operation descriptor for given socket
208
     *
209
     * @param SocketInterface $socket Socket object
210
     *
211
     * @return RequestDescriptor
212
     * @throws \OutOfBoundsException
213
     */
214 61
    private function requireDescriptor(SocketInterface $socket)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
215
    {
216 61
        $hash = $this->getOperationStorageKey($socket);
217 61
        if (!isset($this->items[$hash])) {
218 2
            throw new \OutOfBoundsException('Trying to perform operation on not added socket.');
219
        }
220
221 59
        return $this->items[$hash];
222
    }
223
224
    /**
225
     * Return metadata items
226
     *
227
     * @return RequestDescriptor[]
228
     */
229 68
    public function getItems()
230
    {
231 68
        return $this->items;
232
    }
233
}
234