Passed
Pull Request — master (#9)
by Shinji
01:29
created

DispatchTable   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 29
dl 0
loc 53
c 1
b 0
f 1
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A release() 0 4 2
A releaseOne() 0 6 1
A updateTargets() 0 14 3
1
<?php
2
3
/**
4
 * This file is part of the sj-i/php-profiler package.
5
 *
6
 * (c) sji <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace PhpProfiler\Inspector\Daemon\Dispatcher;
15
16
use PhpProfiler\Inspector\Daemon\Reader\Context\PhpReaderContextInterface;
17
use PhpProfiler\Inspector\Settings\GetTraceSettings;
18
use PhpProfiler\Inspector\Settings\TargetPhpSettings;
19
use PhpProfiler\Inspector\Settings\TraceLoopSettings;
20
21
final class DispatchTable
22
{
23
    private WorkerPoolInterface $worker_pool;
24
    private TargetProcessListInterface $assigned;
25
    private TargetPhpSettings $target_php_settings;
26
    private TraceLoopSettings $trace_loop_settings;
27
    private GetTraceSettings $get_trace_settings;
28
    /** @var array<int, PhpReaderContextInterface> */
29
    private array $dispatch_table = [];
30
31
    public function __construct(
32
        WorkerPoolInterface $worker_pool,
33
        TargetPhpSettings $target_php_settings,
34
        TraceLoopSettings $trace_loop_settings,
35
        GetTraceSettings $get_trace_settings
36
    ) {
37
        $this->worker_pool = $worker_pool;
38
        $this->target_php_settings = $target_php_settings;
39
        $this->trace_loop_settings = $trace_loop_settings;
40
        $this->get_trace_settings = $get_trace_settings;
41
        $this->assigned = new TargetProcessList();
42
    }
43
44
    public function updateTargets(TargetProcessListInterface $update): void
45
    {
46
        $diff = $this->assigned->getDiff($update);
47
        $this->release($diff);
48
        $unassigned_new = $update->getDiff($this->assigned);
49
        for ($worker = $this->worker_pool->getFreeWorker(); $worker; $worker = $this->worker_pool->getFreeWorker()) {
50
            $picked = $unassigned_new->pickOne();
51
            if (is_null($picked)) {
52
                $this->worker_pool->returnWorkerToPool($worker);
53
                break;
54
            }
55
            $this->assigned->putOne($picked);
56
            $this->dispatch_table[$picked] = $worker;
57
            $worker->sendAttach($picked);
58
        }
59
    }
60
61
    public function release(TargetProcessListInterface $targets): void
62
    {
63
        foreach ($targets->getArray() as $pid) {
64
            $this->releaseOne($pid);
65
        }
66
    }
67
68
    public function releaseOne(int $pid): void
69
    {
70
        $worker = $this->dispatch_table[$pid];
71
        $this->worker_pool->returnWorkerToPool($worker);
72
        unset($this->dispatch_table[$pid]);
73
        $this->assigned = $this->assigned->getDiff(new TargetProcessList($pid));
74
    }
75
}
76