Completed
Push — master ( 15cb6d...86e5be )
by Nikolas
03:31
created

DelayedOutput::write()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php namespace rtens\domin\delivery;
2
3
class DelayedOutput {
4
5
    /** @var callable */
6
    private $runner;
7
8
    /** @var callable */
9
    private $printer;
10
11
    /**
12
     * @param callable $runner Will be called with the object itself, returns void
13
     */
14
    public function __construct(callable $runner) {
15
        $this->runner = $runner;
16
        $this->printer = function ($string) {
17
            echo $string;
18
        };
19
    }
20
21
    public function write($message) {
22
        call_user_func($this->printer, $message);
23
    }
24
25
    public function writeLine($message) {
26
        $this->write($message . "\n");
27
    }
28
29
    function __toString() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
30
        call_user_func($this->runner, $this);
31
        return '';
32
    }
33
34
    public function surroundWith($before, $after) {
35
        $oldRunner = $this->runner;
36
        $this->runner = function (DelayedOutput $output) use ($oldRunner, $before, $after) {
37
            $this->write($before);
38
            call_user_func($oldRunner, $output);
39
            $this->write($after);
40
        };
41
    }
42
43
    /**
44
     * @param callable $printer
45
     */
46
    public function setPrinter(callable $printer) {
47
        $this->printer = $printer;
48
    }
49
}