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

FixedLengthFramePicker::doCreateFrame()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
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\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 27
    public function __construct($length)
30
    {
31 27
        parent::__construct();
32 27
        $this->length = (int) $length;
33 27
    }
34
35
    /** {@inheritdoc} */
36 26
    protected function doHandleData($chunk, $remoteAddress, &$buffer)
37
    {
38 26
        $chunkLength   = strlen($chunk);
39 26
        $dataLength    = min($this->length - strlen($buffer), $chunkLength);
40 26
        $buffer       .= substr($chunk, 0, $dataLength);
41 26
        $isEndReached  = strlen($buffer) === $this->length;
42
43 26
        if ($isEndReached) {
44 25
            $this->setFinished(true);
45 25
            return substr($chunk, $dataLength);
46
        }
47
48 15
        return '';
49
    }
50
51
    /** {@inheritdoc} */
52 26
    protected function doCreateFrame($buffer, $remoteAddress)
53
    {
54 26
        return new Frame($buffer, $remoteAddress);
55
    }
56
}
57