LazyWrite::write()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 8
Ratio 72.73 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 8
loc 11
ccs 9
cts 9
cp 1
rs 9.2
cc 4
eloc 7
nc 4
nop 3
crap 4
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
8
 */
9
10
namespace AnimeDb\Bundle\CatalogBundle\Console\Output;
11
12
/**
13
 * Lazy write output.
14
 *
15
 * @author  Peter Gribanov <[email protected]>
16
 */
17
class LazyWrite extends Decorator
18
{
19
    /**
20
     * Write stack.
21
     *
22
     * @var array
23
     */
24
    protected $stack = [];
25
26
    /**
27
     * Save messages in stack.
28
     *
29
     * @var bool
30
     */
31
    protected $lazy_write = true;
32
33
    /**
34
     * @param array|string $messages
35
     * @param bool|false $newline
36
     * @param int $type
37
     */
38 30
    public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)
39
    {
40 30 View Code Duplication
        if ($this->lazy_write) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41 24
            $messages = (array) $messages;
42 24
            foreach ($messages as $message) {
43 24
                $this->stack[] = [$message.($newline ? PHP_EOL : ''), $type];
44 24
            }
45 24
        } else {
46 6
            parent::write($messages, $newline, $type);
47
        }
48 30
    }
49
50
    /**
51
     * @param array|string $messages
52
     * @param int $type
53
     */
54 15
    public function writeln($messages, $type = self::OUTPUT_NORMAL)
55
    {
56 15 View Code Duplication
        if ($this->lazy_write) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57 12
            $messages = (array) $messages;
58 12
            foreach ($messages as $message) {
59 12
                $this->stack[] = [$message, $type];
60 12
            }
61 12
        } else {
62 3
            parent::writeln($messages, $type);
63
        }
64 15
    }
65
66
    /**
67
     * Write all messages from stack.
68
     */
69 18
    public function writeAll()
70
    {
71 18
        while ($message = array_shift($this->stack)) {
72 18
            parent::writeln($message[0], $message[1]);
73 18
        }
74 18
    }
75
76
    /**
77
     * @return bool
78
     */
79 1
    public function isLazyWrite()
80
    {
81 1
        return $this->lazy_write;
82
    }
83
84
    /**
85
     * @param bool $lazy_write
86
     */
87 10
    public function setLazyWrite($lazy_write)
88
    {
89 10
        $this->lazy_write = $lazy_write;
90 10
    }
91
}
92