Completed
Branch master (1d1574)
by Evgenij
18:38
created

FixedLengthFramePicker   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 4
c 4
b 0
f 2
lcom 1
cbo 2
dl 0
loc 42
ccs 15
cts 15
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A doHandleData() 0 14 2
A doCreateFrame() 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\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