FixedPool   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 3
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 51
ccs 24
cts 24
cp 1
rs 10
c 4
b 1
f 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A execute() 0 9 3
B wait() 0 17 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;
10
11
12
/**
13
 * fixed pool
14
 *
15
 * @package Jenner\SimpleFork
16
 */
17
class FixedPool extends AbstractPool
18
{
19
    /**
20
     * @var int max process count
21
     */
22
    protected $max;
23
24
    /**
25
     * @param int $max
26
     */
27 3
    public function __construct($max = 4)
28
    {
29 3
        $this->max = $max;
30 3
    }
31
32 3
    public function execute(Process $process)
33
    {
34 3
        Utils::checkOverwriteRunMethod(get_class($process));
35
36 3
        if ($this->aliveCount() < $this->max && !$process->isStarted()) {
37 3
            $process->start();
38 3
        }
39 3
        array_push($this->processes, $process);
40 3
    }
41
42
    /**
43
     * wait for all process done
44
     *
45
     * @param bool $block block the master process
46
     * to keep the sub process count all the time
47
     * @param int $interval check time interval
48
     */
49 3
    public function wait($block = false, $interval = 100)
50
    {
51
        do {
52 3
            if ($this->isFinished()) {
53 3
                return;
54
            }
55 3
            parent::wait(false);
56 3
            if ($this->aliveCount() < $this->max) {
57 3
                foreach ($this->processes as $process) {
58 3
                    if ($process->isStarted()) continue;
59 3
                    $process->start();
60 3
                    if ($this->aliveCount() >= $this->max) break;
61 3
                }
62 3
            }
63 3
            $block ? usleep($interval) : null;
64 3
        } while ($block);
65 3
    }
66
67
}