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

AbstractFramePicker::isEof()   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 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
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 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 122
    public function __construct()
49
    {
50 122
        $this->isFinished = false;
51 122
        $this->buffer     = '';
52 122
    }
53
54
    /** {@inheritdoc} */
55 75
    public function isEof()
56
    {
57 75
        return $this->isFinished;
58
    }
59
60
    /** {@inheritdoc} */
61 72
    public function pickUpData($chunk, $remoteAddress)
62
    {
63 72
        if ($this->isFinished) {
64 3
            return $chunk;
65
        }
66
67 72
        if (!$this->remoteAddress && $remoteAddress) {
68 67
            $this->remoteAddress = $remoteAddress;
69 67
        }
70
71 72
        $this->frame = null;
72 72
        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 59
    protected function setFinished($isFinished)
104
    {
105 59
        $this->isFinished = $isFinished;
106 59
    }
107
108
    /** {@inheritdoc} */
109 72
    public function createFrame()
110
    {
111 72
        if (!$this->frame) {
112 72
            $this->frame = $this->doCreateFrame($this->buffer, $this->remoteAddress);
113 72
        }
114
115 72
        return $this->frame;
116
    }
117
}
118