Passed
Push — master ( 944055...238e3d )
by Joao
02:01
created

ParallelHandler::waitFinish()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 2
nc 2
nop 0
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 12
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace ByJG\PHPThread\Handler;
4
5
class ParallelHandler implements ThreadInterface
6
{
7
    protected $closure;
8
    protected $runtime;
9
    protected $future;
10
11
    public function execute()
12
    {
13
        $fnArgs = func_get_args();
14
15
        $this->runtime = new \parallel\Runtime();
16
        $this->future = $this->runtime->run($this->closure, $fnArgs);
17
    }
18
19
    public function getResult()
20
    {
21
        return $this->future->value();
22
    }
23
24
    public function stop($signal = SIGKILL, $wait = false)
25
    {
26
        $this->runtime->kill();
27
    }
28
29
    public function isAlive()
30
    {
31
        return !$this->future->cancelled() && !$this->future->done();
32
    }
33
34
    public function setClosure(\Closure $closure)
35
    {
36
        $this->closure = $closure;
37
    }
38
39
    public function waitFinish()
40
    {
41
        while (!$this->future->cancelled() && !$this->future->done()) {
42
            sleep(1);
43
        }
44
    }
45
46
    public function getClassName()
47
    {
48
        return ParallelHandler::class;
49
    }
50
}