Completed
Push — master ( 95a54c...7178fa )
by Samuel
10:58
created

UserInteractionPipe::error()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Dock\IO\Process\Pipe;
4
5
use Dock\IO\Process\InteractiveProcess;
6
use Dock\IO\UserInteraction;
7
8
class UserInteractionPipe implements Pipe
9
{
10
    /**
11
     * @var UserInteraction
12
     */
13
    private $userInteraction;
14
15
    /**
16
     * @param UserInteraction $userInteraction
17
     */
18
    public function __construct(UserInteraction $userInteraction)
19
    {
20
        $this->userInteraction = $userInteraction;
21
    }
22
23
    /**
24
     * Rewind pipe from the beginning of the process.
25
     *
26
     * @param InteractiveProcess $process
27
     */
28
    public function rewind(InteractiveProcess $process)
29
    {
30
        $process = $process->getProcess();
31
32
        $this->error($process->getErrorOutput());
33
        $this->output($process->getOutput());
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function error($buffer)
40
    {
41
        $this->pipeLinesWithPrefix('<error>ERR</error>', explode("\n", $buffer));
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function output($buffer)
48
    {
49
        $this->pipeLinesWithPrefix('<question>OUT</question>', explode("\n", $buffer));
50
    }
51
52
    /**
53
     * @param string $prefix
54
     * @param array  $lines
55
     */
56
    private function pipeLinesWithPrefix($prefix, array $lines)
57
    {
58 View Code Duplication
        foreach ($lines as $line) {
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...
59
            $line = trim($line);
60
            if (!empty($line)) {
61
                $this->userInteraction->write($prefix.' '.$line);
62
            }
63
        }
64
    }
65
}
66