Passed
Pull Request — master (#9)
by Shinji
02:03
created

TraceLoopProvider::getMainLoop()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
nc 1
nop 2
dl 0
loc 8
c 0
b 0
f 0
cc 1
rs 10
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;
15
16
use PhpProfiler\Inspector\Settings\TraceLoopSettings;
17
use PhpProfiler\Lib\Loop\Loop;
18
use PhpProfiler\Lib\Loop\LoopBuilder;
19
use PhpProfiler\Lib\Loop\LoopMiddleware\CallableMiddleware;
20
use PhpProfiler\Lib\Loop\LoopMiddleware\KeyboardCancelMiddleware;
21
use PhpProfiler\Lib\Loop\LoopMiddleware\NanoSleepMiddleware;
22
use PhpProfiler\Lib\Loop\LoopMiddleware\RetryOnExceptionMiddleware;
23
use PhpProfiler\Lib\Process\MemoryReader\MemoryReaderException;
24
25
final class TraceLoopProvider
26
{
27
    private LoopBuilder $loop_builder;
28
29
    public function __construct(LoopBuilder $loop_builder)
30
    {
31
        $this->loop_builder = $loop_builder;
32
    }
33
34
    public function getMainLoop(callable $main, TraceLoopSettings $settings): Loop
35
    {
36
        return $this->loop_builder
37
            ->addProcess(RetryOnExceptionMiddleware::class, [$settings->max_retries, [MemoryReaderException::class]])
38
            ->addProcess(KeyboardCancelMiddleware::class, [$settings->cancel_key])
39
            ->addProcess(NanoSleepMiddleware::class, [$settings->sleep_nano_seconds])
40
            ->addProcess(CallableMiddleware::class, [$main])
41
            ->build();
42
    }
43
}
44