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

ReaderLoopProvider   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 22
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getMainLoop() 0 13 1
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;
15
16
use PhpProfiler\Inspector\Settings\TraceLoopSettings;
17
use PhpProfiler\Lib\Loop\AsyncLoop;
18
use PhpProfiler\Lib\Loop\AsyncLoopBuilder;
19
use PhpProfiler\Lib\Loop\AsyncLoopMiddleware\CallableMiddlewareAsync;
20
use PhpProfiler\Lib\Loop\AsyncLoopMiddleware\NanoSleepMiddlewareAsync;
21
use PhpProfiler\Lib\Loop\AsyncLoopMiddleware\RetryOnExceptionMiddlewareAsync;
22
use PhpProfiler\Lib\Process\MemoryReader\MemoryReaderException;
23
24
final class ReaderLoopProvider
25
{
26
    private AsyncLoopBuilder $loop_builder;
27
28
    public function __construct(AsyncLoopBuilder $loop_builder)
29
    {
30
        $this->loop_builder = $loop_builder;
31
    }
32
33
    public function getMainLoop(callable $main, TraceLoopSettings $settings): AsyncLoop
34
    {
35
        return $this->loop_builder
36
            ->addProcess(
37
                RetryOnExceptionMiddlewareAsync::class,
38
                [
39
                    $settings->max_retries,
40
                    [MemoryReaderException::class]
41
                ]
42
            )
43
            ->addProcess(NanoSleepMiddlewareAsync::class, [$settings->sleep_nano_seconds])
44
            ->addProcess(CallableMiddlewareAsync::class, [$main])
45
            ->build();
46
    }
47
}
48