TimeoutWait::wait()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
rs 9.2
cc 4
eloc 9
nc 4
nop 1
1
<?php
2
3
namespace Dock\IO\Process\WaitStrategy;
4
5
use Symfony\Component\Process\Process;
6
7
class TimeoutWait implements WaitStrategy
8
{
9
    const TICK = 100;
10
11
    /**
12
     * Timeout, in milliseconds.
13
     *
14
     * @var int
15
     */
16
    private $timeout;
17
18
    /**
19
     * @var callable
20
     */
21
    private $callback;
22
23
    public function __construct($timeout, callable $callback)
24
    {
25
        $this->timeout = $timeout;
26
        $this->callback = $callback;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function wait(Process $process)
33
    {
34
        $start = microtime(true);
35
        $end = $start + $this->timeout / 1000;
36
37
        while (!$process->isTerminated() && (microtime(true) < $end)) {
38
            usleep(self::TICK * 1000);
39
        }
40
41
        if ($process->isRunning()) {
42
            $callback = $this->callback;
43
            $callback();
44
        }
45
46
        $process->wait();
47
    }
48
}
49