AbstractFramePicker   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 0
loc 103
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A isEof() 0 4 1
doHandleData() 0 1 ?
doCreateFrame() 0 1 ?
A setFinished() 0 4 1
A pickUpData() 0 13 4
A createFrame() 0 8 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 AbstractFramePicker
14
 */
15
abstract class AbstractFramePicker implements FramePickerInterface
16
{
17
    /**
18
     * Flag, whether framePicker is finished
19
     *
20
     * @var bool
21
     */
22
    private $isFinished;
23
24
    /**
25
     * Frame with data for this picker
26
     *
27
     * @var FrameInterface
28
     */
29
    private $frame;
30
31
    /**
32
     * Collected data for this frame
33
     *
34
     * @var string
35
     */
36
    private $buffer;
37
38
    /**
39
     * Client's address sent this data
40
     *
41
     * @var string
42
     */
43
    private $remoteAddress;
44
45
    /**
46
     * AbstractFramePicker constructor.
47
     */
48 148
    public function __construct()
49
    {
50 148
        $this->isFinished = false;
51 148
        $this->buffer     = '';
52 148
    }
53
54
    /** {@inheritdoc} */
55 78
    public function isEof()
56
    {
57 78
        return $this->isFinished;
58
    }
59
60
    /** {@inheritdoc} */
61 78
    public function pickUpData($chunk, $remoteAddress)
62
    {
63 78
        if ($this->isFinished) {
64 3
            return $chunk;
65
        }
66
67 78
        if (!$this->remoteAddress && $remoteAddress) {
68 73
            $this->remoteAddress = $remoteAddress;
69 73
        }
70
71 78
        $this->frame = null;
72 78
        return (string) $this->doHandleData($chunk, $remoteAddress, $this->buffer);
73
    }
74
75
    /**
76
     * Process raw network data. Data should be used to determine end of this concrete framePicker
77
     *
78
     * @param string $chunk Chunk read from socket
79
     * @param string $remoteAddress Client's address sent this data
80
     * @param string &$buffer Pointer to internal buffer for collecting data
81
     *
82
     * @return string Unprocessed data after the end of frame
83
     */
84
    abstract protected function doHandleData($chunk, $remoteAddress, &$buffer);
85
86
    /**
87
     * Create frame for picked up data
88
     *
89
     * @param string $buffer Buffer with collected data
90
     * @param string $remoteAddress Remote socket address these data from
91
     *
92
     * @return FrameInterface
93
     */
94
    abstract protected function doCreateFrame($buffer, $remoteAddress);
95
96
    /**
97
     * Sets finished flag
98
     *
99
     * @param boolean $isFinished Flag whether framePicker is finished
100
     *
101
     * @return void
102
     */
103 65
    protected function setFinished($isFinished)
104
    {
105 65
        $this->isFinished = $isFinished;
106 65
    }
107
108
    /** {@inheritdoc} */
109 76
    public function createFrame()
110
    {
111 76
        if (!$this->frame) {
112 76
            $this->frame = $this->doCreateFrame($this->buffer, $this->remoteAddress);
113 76
        }
114
115 76
        return $this->frame;
116
    }
117
}
118