Passed
Push — 0.6.x ( 9a1948...a98caa )
by Shinji
03:23 queued 01:32
created

PhpReaderEntryPoint   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 48
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A run() 0 40 4
1
<?php
2
3
/**
4
 * This file is part of the reliforp/reli-prof 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 Reli\Inspector\Daemon\Reader\Worker;
15
16
use Reli\Inspector\Daemon\Reader\Protocol\Message\DetachWorkerMessage;
17
use Reli\Inspector\Daemon\Reader\Protocol\Message\AttachMessage;
18
use Reli\Inspector\Daemon\Reader\Protocol\Message\SetSettingsMessage;
19
use Reli\Inspector\Daemon\Reader\Protocol\PhpReaderWorkerProtocolInterface;
20
use Reli\Lib\Amphp\WorkerEntryPointInterface;
21
use Reli\Lib\Log\Log;
22
23
final class PhpReaderEntryPoint implements WorkerEntryPointInterface
24
{
25
    public function __construct(
26
        private PhpReaderTraceLoopInterface $trace_loop,
27
        private PhpReaderWorkerProtocolInterface $protocol,
28
    ) {
29
    }
30
31
    public function run(): \Generator
32
    {
33
        /**
34
         * @psalm-ignore-var
35
         * @var SetSettingsMessage $set_settings_message
36
         */
37
        $set_settings_message = yield $this->protocol->receiveSettings();
38
        Log::debug('settings_message', [$set_settings_message]);
39
40
        while (1) {
41
            /**
42
             * @psalm-ignore-var
43
             * @var AttachMessage $attach_message
44
             */
45
            $attach_message = yield $this->protocol->receiveAttach();
46
            Log::debug('attach_message', [$attach_message]);
47
48
            try {
49
                $loop_runner = $this->trace_loop->run(
50
                    $set_settings_message->trace_loop_settings,
51
                    $attach_message->process_descriptor,
52
                    $set_settings_message->get_trace_settings
53
                );
54
                Log::debug('start trace');
55
                foreach ($loop_runner as $message) {
56
                    yield $this->protocol->sendTrace($message);
57
                }
58
                Log::debug('end trace');
59
            } catch (\Throwable $e) {
60
                Log::debug('exception thrown at reading traces', [
61
                    'exception' => $e,
62
                    'trace' => $e->getTrace(),
63
                ]);
64
            }
65
66
            Log::debug('detaching worker');
67
            yield $this->protocol->sendDetachWorker(
68
                new DetachWorkerMessage($attach_message->process_descriptor->pid)
69
            );
70
            Log::debug('detached worker');
71
        }
72
    }
73
}
74