FixedLengthFramePicker::doHandleData()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 3
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\Frame;
11
12
/**
13
 * Class FixedLengthFramePicker
14
 */
15
class FixedLengthFramePicker extends AbstractFramePicker
16
{
17
    /**
18
     * Desired length of framePicker
19
     *
20
     * @var int
21
     */
22
    private $length;
23
24
    /**
25
     * FixedLengthFramePicker constructor.
26
     *
27
     * @param int $length Length of data for this framePicker
28
     */
29 29
    public function __construct($length)
30
    {
31 29
        parent::__construct();
32 29
        $this->length = (int) $length;
33 29
    }
34
35
    /** {@inheritdoc} */
36 28
    protected function doHandleData($chunk, $remoteAddress, &$buffer)
37
    {
38 28
        $chunkLength   = strlen($chunk);
39 28
        $dataLength    = min($this->length - strlen($buffer), $chunkLength);
40 28
        $buffer       .= substr($chunk, 0, $dataLength);
41 28
        $isEndReached  = strlen($buffer) === $this->length;
42
43 28
        if ($isEndReached) {
44 27
            $this->setFinished(true);
45 27
            return substr($chunk, $dataLength);
46
        }
47
48 15
        return '';
49
    }
50
51
    /** {@inheritdoc} */
52 28
    protected function doCreateFrame($buffer, $remoteAddress)
53
    {
54 28
        return new Frame($buffer, $remoteAddress);
55
    }
56
}
57