BufferedPrinter::initializeBuffer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
3
/*
4
 * Copyright (c) 2011-2015, Celestino Diaz <[email protected]>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
25
namespace Brickoo\Component\IO\Printing;
26
27
/**
28
 * Implements an abstract buffered printer.
29
 * The main purpose is to buffer a defined
30
 * length of bytes before calling the print routine.
31
 */
32
abstract class BufferedPrinter implements OutputPrinter {
33
34
    /** @var integer */
35
    protected $bufferLength;
36
37
    /** @var string */
38
    protected $buffer;
39
40
    /** {@inheritdoc} */
41 7
    public function doPrint($output) {
42 7
        if ($this->isBufferTurnedOff()) {
43 2
            $this->output($output);
44 2
            return $this;
45
        }
46
47 5
        if ($this->isBufferLessThan($this->sumBufferWith($output))) {
48 2
            $this->output($this->getBuffer());
49 2
            $this->clearBuffer();
50 2
        }
51
52 5
        $this->addToBuffer($output);
53 5
        return $this;
54
    }
55
56
    /**
57
     * Flush the output buffer and clear the local buffer.
58
     * @return \Brickoo\Component\IO\Printing\OutputBufferedPrinter
59
     */
60 4
    public function flushBuffer() {
61 4
        $this->output($this->getBuffer())->clearBuffer();
62 4
        return $this;
63
    }
64
65
    /**
66
     * Initialize the buffer.
67
     * @param integer $bufferLength
68
     * @param string $bufferContent
69
     * @return \Brickoo\Component\IO\Printing\BufferedPrinter
70
     */
71 3
    protected function initializeBuffer($bufferLength = 255, $bufferContent = "") {
72 3
        $this->bufferLength = $bufferLength;
73 3
        $this->buffer = $bufferContent;
74 3
        return $this;
75
    }
76
77
    /**
78
     * Return the buffered content.
79
     * @return string the buffer content
80
     */
81 1
    protected function getBuffer() {
82 1
        return $this->buffer;
83
    }
84
85
    /**
86
     * Add content to the buffer.
87
     * @param string $content
88
     * @return \Brickoo\Component\IO\Printing\BufferedPrinter
89
     */
90 1
    protected function addToBuffer($content) {
91 1
        $this->buffer .= $content;
92 1
        return $this;
93
    }
94
95
    /**
96
     * Check if the buffer is turned off.
97
     * @return boolean check result
98
     */
99 2
    protected function isBufferTurnedOff() {
100 2
        return $this->bufferLength <= 0;
101
    }
102
103
    /**
104
     * Check if the buffer length
105
     * is less than the argument.
106
     * @param integer $length
107
     * @return boolean check result
108
     */
109 1
    protected function isBufferLessThan($length) {
110 1
        return $this->bufferLength < $length;
111
    }
112
113
    /**
114
     * Return the sum of the current buffer and argument.
115
     * @param string $output
116
     * @return integer the sum of the buffer and argument
117
     */
118 1
    protected function sumBufferWith($output) {
119 1
        return strlen($this->buffer) + strlen($output);
120
    }
121
122
    /**
123
     * Clear the output buffer.
124
     * @return \Brickoo\Component\IO\Printing\BufferedPrinter
125
     */
126 1
    protected function clearBuffer() {
127 1
        $this->buffer = "";
128 1
        return $this;
129
    }
130
131
    /**
132
     * Output the content using the concrete printer implementation.
133
     * @param string $content
134
     * @return \Brickoo\Component\IO\Printing\BufferedPrinter
135
     */
136
    abstract protected function output($content);
137
138
}
139