PhpSearcherController::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 1
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
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\Searcher\Controller;
15
16
use Reli\Inspector\Daemon\AutoContextRecoveringInterface;
17
use Reli\Inspector\Daemon\Searcher\Protocol\Message\TargetPhpSettingsMessage;
18
use Reli\Inspector\Daemon\Searcher\Protocol\Message\UpdateTargetProcessMessage;
19
use Reli\Inspector\Daemon\Searcher\Protocol\PhpSearcherControllerProtocolInterface;
20
use Reli\Inspector\Settings\TargetPhpSettings\TargetPhpSettings;
21
22
final class PhpSearcherController implements PhpSearcherControllerInterface
23
{
24
    private ?TargetPhpSettingsMessage $settings_already_sent = null;
25
26
    /** @param AutoContextRecoveringInterface<PhpSearcherControllerProtocol> $auto_context_recovering */
27
    public function __construct(
28
        readonly private AutoContextRecoveringInterface $auto_context_recovering
29
    ) {
30
        $this->auto_context_recovering->onRecover(
31
            function (): void {
32
                if ($this->settings_already_sent !== null) {
33
                    $this->auto_context_recovering
34
                        ->getContext()
35
                        ->getProtocol()
36
                        ->sendTargetRegex($this->settings_already_sent)
37
                    ;
38
                }
39
            }
40
        );
41
    }
42
43
    public function start(): void
44
    {
45
        $this->auto_context_recovering->getContext()->start();
46
    }
47
48
    /** @param non-empty-string $regex */
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
49
    public function sendTarget(
50
        string $regex,
51
        TargetPhpSettings $target_php_settings,
52
        int $pid,
53
    ): void {
54
        $message = new TargetPhpSettingsMessage(
55
            $regex,
56
            $target_php_settings,
57
            $pid
58
        );
59
        $this->auto_context_recovering->withAutoRecover(
60
            function (PhpSearcherControllerProtocolInterface $protocol) use ($message) {
61
                $protocol->sendTargetRegex($message);
62
            },
63
            'failed to send target',
64
        );
65
        $this->settings_already_sent = $message;
66
    }
67
68
    public function receivePidList(): UpdateTargetProcessMessage
69
    {
70
        return $this->auto_context_recovering->withAutoRecover(
71
            function (PhpSearcherControllerProtocolInterface $protocol) {
72
                return $protocol->receiveUpdateTargetProcess();
73
            },
74
            'failed to receive pid list',
75
        );
76
    }
77
}
78