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

DelayedOutput   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 1
cbo 0
dl 0
loc 47
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A write() 0 3 1
A writeLine() 0 3 1
A __toString() 0 4 1
A surroundWith() 0 8 1
A setPrinter() 0 3 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
}