TimeoutWait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 42
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A wait() 0 16 4
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