BufferIO   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 0
dl 0
loc 91
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A open() 0 4 1
A close() 0 4 1
A read() 0 11 2
A write() 0 6 1
A push() 0 6 1
A pop() 0 12 2
1
<?php
2
3
namespace ButterAMQP\IO;
4
5
use ButterAMQP\IOInterface;
6
7
class BufferIO implements IOInterface
8
{
9
    /**
10
     * @var string
11
     */
12
    private $writeBuffer;
13
14
    /**
15
     * @var string
16
     */
17
    private $readBuffer;
18
19
    /**
20
     * @param string $readBuffer
21
     */
22 18
    public function __construct($readBuffer = '')
23
    {
24 18
        $this->readBuffer = $readBuffer;
25 18
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 1
    public function open($protocol, $host, $port, array $parameters = [])
31
    {
32 1
        return $this;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 1
    public function close()
39
    {
40 1
        return $this;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 11
    public function read($length, $blocking = true)
47
    {
48 11
        if (strlen($this->readBuffer) < $length) {
49 11
            $length = strlen($this->readBuffer);
50 11
        }
51
52 11
        $data = substr($this->readBuffer, 0, $length);
53 11
        $this->readBuffer = substr($this->readBuffer, $length, strlen($this->readBuffer) - $length);
54
55 11
        return $data;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 5
    public function write($data, $length = null)
62
    {
63 5
        $this->writeBuffer .= $data;
64
65 5
        return $this;
66
    }
67
68
    /**
69
     * @param string $data
70
     *
71
     * @return $this
72
     */
73 9
    public function push($data)
74
    {
75 9
        $this->readBuffer .= $data;
76
77 9
        return $this;
78
    }
79
80
    /**
81
     * @param null $length
82
     *
83
     * @return string
84
     */
85 4
    public function pop($length = null)
86
    {
87 4
        if ($length === null) {
88 1
            $data = $this->writeBuffer;
89 1
            $this->writeBuffer = '';
90 1
        } else {
91 3
            $data = substr($this->writeBuffer, 0, $length);
92 3
            $this->writeBuffer = substr($this->writeBuffer, $length, strlen($this->writeBuffer) - $length);
93
        }
94
95 4
        return $data;
96
    }
97
}
98