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

InteractiveProcessBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A forCommand() 0 8 1
A disableOutput() 0 6 1
A withoutTimeout() 0 6 1
A ifTakesMoreThan() 0 9 1
A getManager() 0 15 1
1
<?php
2
3
namespace Dock\IO\Process;
4
5
use Dock\IO\Process\Pipe\NullPipe;
6
use Dock\IO\Process\Pipe\Pipe;
7
use Dock\IO\Process\WaitStrategy\TimeoutWait;
8
use Dock\IO\Process\WaitStrategy\WaitStrategy;
9
use Dock\IO\UserInteraction;
10
use Symfony\Component\Process\Process;
11
12
class InteractiveProcessBuilder
13
{
14
    /**
15
     * Default process timeout, in seconds.
16
     */
17
    const DEFAULT_TIMEOUT = 60;
18
19
    /**
20
     * @var InteractiveProcessManager
21
     */
22
    private $manager;
23
24
    /**
25
     * @var UserInteraction
26
     */
27
    private $userInteraction;
28
29
    /**
30
     * @var string
31
     */
32
    private $command;
33
34
    /**
35
     * @var Pipe
36
     */
37
    private $pipe;
38
39
    /**
40
     * @var WaitStrategy
41
     */
42
    private $waitStrategy;
43
44
    /**
45
     * Timeout of the built process, in seconds.
46
     *
47
     * @var int
48
     */
49
    private $timeout;
50
51
    /**
52
     * @param UserInteraction $userInteraction
53
     */
54
    public function __construct(UserInteraction $userInteraction)
55
    {
56
        $this->userInteraction = $userInteraction;
57
        $this->timeout = self::DEFAULT_TIMEOUT;
58
    }
59
60
    /**
61
     * Set the process command.
62
     *
63
     * @param string $command
64
     *
65
     * @return InteractiveProcessBuilder
66
     */
67
    public function forCommand($command)
68
    {
69
        $this->command = $command;
70
        $this->pipe = new NullPipe();
71
        $this->manager = new InteractiveProcessManager($this->userInteraction);
72
73
        return $this;
74
    }
75
76
    /**
77
     * @return InteractiveProcessBuilder
78
     */
79
    public function disableOutput()
80
    {
81
        $this->pipe = new NullPipe();
82
83
        return $this;
84
    }
85
86
    /**
87
     * @return InteractiveProcessBuilder
88
     */
89
    public function withoutTimeout()
90
    {
91
        $this->timeout = null;
92
93
        return $this;
94
    }
95
96
    /**
97
     * Makes the process to call the given callback after the given time.
98
     *
99
     * @param int      $milliSeconds
100
     * @param callable $callback
101
     *
102
     * @return InteractiveProcessBuilder
103
     */
104
    public function ifTakesMoreThan($milliSeconds, callable $callback)
105
    {
106
        $manager = $this->manager;
107
        $this->waitStrategy = new TimeoutWait($milliSeconds, function () use ($callback, $manager) {
108
            $callback($manager);
109
        });
110
111
        return $this;
112
    }
113
114
    /**
115
     * Get generated process manager.
116
     *
117
     * @return InteractiveProcessManager
118
     */
119
    public function getManager()
120
    {
121
        $symfonyProcess = new Process($this->command);
122
        $symfonyProcess->setTimeout($this->timeout);
123
124
        $process = new InteractiveProcess(
125
            $symfonyProcess,
126
            $this->pipe,
127
            $this->waitStrategy
128
        );
129
130
        $this->manager->setProcess($process);
131
132
        return $this->manager;
133
    }
134
}
135