Passed
Pull Request — master (#31)
by Shinji
04:07 queued 02:36
created

PhpSearcherEntryPoint   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 27
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A run() 0 14 2
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\Searcher\Worker;
15
16
use PhpProfiler\Inspector\Daemon\Searcher\Protocol\Message\UpdateTargetProcessMessage;
17
use PhpProfiler\Inspector\Daemon\Dispatcher\TargetProcessList;
18
use PhpProfiler\Inspector\Daemon\Searcher\Protocol\Message\TargetRegexMessage;
19
use PhpProfiler\Inspector\Daemon\Searcher\Protocol\PhpSearcherWorkerProtocolInterface;
20
use PhpProfiler\Lib\Amphp\WorkerEntryPointInterface;
21
use PhpProfiler\Lib\Process\Search\ProcessSearcherInterface;
22
23
final class PhpSearcherEntryPoint implements WorkerEntryPointInterface
24
{
25
    private PhpSearcherWorkerProtocolInterface $protocol;
26
    private ProcessSearcherInterface $process_searcher;
27
28
    public function __construct(
29
        PhpSearcherWorkerProtocolInterface $protocol,
30
        ProcessSearcherInterface $process_searcher
31
    ) {
32
        $this->protocol = $protocol;
33
        $this->process_searcher = $process_searcher;
34
    }
35
36
    public function run(): \Generator
37
    {
38
        /** @var TargetRegexMessage $target_regex */
39
        $target_regex = yield $this->protocol->receiveTargetRegex();
40
41
        while (1) {
42
            yield $this->protocol->sendUpdateTargetProcess(
43
                new UpdateTargetProcessMessage(
44
                    new TargetProcessList(
45
                        ...$this->process_searcher->searchByRegex($target_regex->regex)
46
                    )
47
                )
48
            );
49
            sleep(1);
50
        }
51
    }
52
}
53