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

PhpReaderEntryPoint   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 42
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A run() 0 29 4
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\Reader\Worker;
15
16
use PhpProfiler\Inspector\Daemon\Reader\Protocol\Message\DetachWorkerMessage;
17
use PhpProfiler\Inspector\Daemon\Reader\Protocol\Message\AttachMessage;
18
use PhpProfiler\Inspector\Daemon\Reader\Protocol\Message\SetSettingsMessage;
19
use PhpProfiler\Inspector\Daemon\Reader\Protocol\PhpReaderWorkerProtocolInterface;
20
use PhpProfiler\Inspector\Settings\TargetProcessSettings\TargetProcessSettings;
21
use PhpProfiler\Lib\Amphp\WorkerEntryPointInterface;
22
use PhpProfiler\Lib\Process\MemoryReader\MemoryReaderException;
23
24
final class PhpReaderEntryPoint implements WorkerEntryPointInterface
25
{
26
    private PhpReaderTraceLoopInterface $trace_loop;
27
    private PhpReaderWorkerProtocolInterface $protocol;
28
29
    public function __construct(
30
        PhpReaderTraceLoopInterface $trace_loop,
31
        PhpReaderWorkerProtocolInterface $protocol
32
    ) {
33
        $this->trace_loop = $trace_loop;
34
        $this->protocol = $protocol;
35
    }
36
37
    public function run(): \Generator
38
    {
39
        /** @var SetSettingsMessage $set_settings_message */
40
        $set_settings_message = yield $this->protocol->receiveSettings();
41
42
        while (1) {
43
            /** @var AttachMessage $attach_message */
44
            $attach_message = yield $this->protocol->receiveAttach();
45
46
            $target_process_settings = new TargetProcessSettings(
47
                $attach_message->pid
48
            );
49
50
            try {
51
                $loop_runner = $this->trace_loop->run(
52
                    $target_process_settings,
53
                    $set_settings_message->trace_loop_settings,
54
                    $set_settings_message->target_php_settings,
55
                    $set_settings_message->get_trace_settings
56
                );
57
                foreach ($loop_runner as $message) {
58
                    yield $this->protocol->sendTrace($message);
59
                }
60
            } catch (MemoryReaderException $e) {
61
                // TODO: log errors
62
            }
63
64
            yield $this->protocol->sendDetachWorker(
65
                new DetachWorkerMessage($attach_message->pid)
66
            );
67
        }
68
    }
69
}
70