Completed
Push — master ( bd10e4...c7dba8 )
by Hu
14:29 queued 12:10
created

FixedPool::wait()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 8

Importance

Changes 3
Bugs 1 Features 2
Metric Value
c 3
b 1
f 2
dl 0
loc 17
ccs 14
cts 14
cp 1
rs 7.7778
cc 8
eloc 12
nc 9
nop 2
crap 8
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Jenner
5
 * Date: 2015/11/2
6
 * Time: 17:45
7
 */
8
9
namespace Jenner\SimpleFork;
1 ignored issue
show
Coding Style introduced by
There must be one blank line after the namespace declaration
Loading history...
10
11
12
class FixedPool extends AbstractPool
13
{
14
    /**
15
     * @var int max process count
16
     */
17
    protected $max;
18
19
    /**
20
     * @param int $max
21
     */
22 3
    public function __construct($max = 4)
23
    {
24 3
        $this->max = $max;
25 3
    }
26
27 3
    public function execute(Process $process)
28
    {
29 3
        Utils::checkOverwriteRunMethod(get_class($process));
30
31 3
        if ($this->aliveCount() < $this->max) {
32 3
            $process->start();
33 3
        }
34 3
        array_push($this->processes, $process);
35 3
    }
36
37
    /**
38
     * wait for all process done
39
     *
40
     * @param bool $block block the master process
41
     * to keep the sub process count all the time
42
     * @param int $interval check time interval
43
     */
44 3
    public function wait($block = false, $interval = 100)
45
    {
46
        do {
47 3
            if ($this->isFinished()) {
48 3
                return;
49
            }
50 3
            parent::wait(false);
51 3
            if ($this->aliveCount() < $this->max) {
52 3
                foreach ($this->processes as $process) {
53 3
                    if ($process->hasStarted()) continue;
54 3
                    $process->start();
55 3
                    if ($this->aliveCount() >= $this->max) break;
56 3
                }
57 3
            }
58 3
            $block ? usleep($interval) : null;
59 3
        } while ($block);
60 3
    }
61
62
}
1 ignored issue
show
Coding Style introduced by
According to PSR2, the closing brace of classes should be placed on the next line directly after the body.

Below you find some examples:

// Incorrect placement according to PSR2
class MyClass
{
    public function foo()
    {

    }
    // This blank line is not allowed.

}

// Correct
class MyClass
{
    public function foo()
    {

    } // No blank lines after this line.
}
Loading history...