Passed
Pull Request — master (#96)
by Shinji
01:40
created

RetryingLoopProvider::do()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 18
rs 9.7666
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\Lib\Loop\LoopBuilder;
17
use PhpProfiler\Lib\Loop\LoopMiddleware\CallableMiddleware;
18
use PhpProfiler\Lib\Loop\LoopMiddleware\NanoSleepMiddleware;
19
use PhpProfiler\Lib\Loop\LoopMiddleware\RetryOnExceptionMiddleware;
20
21
class RetryingLoopProvider
22
{
23
    public function __construct(
24
        private LoopBuilder $loop_builder
25
    ) {
26
    }
27
28
    /** @param class-string<\Throwable>[] $retry_on */
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Throwable>[] at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Throwable>[].
Loading history...
29
    public function do(
30
        callable $try,
31
        array $retry_on,
32
        int $max_retry,
33
        int $interval_on_retry_ns,
34
    ): void {
35
        // one successful execution is enough
36
        $loop_canceller = function () use ($try): bool {
37
            $try();
38
            return false;
39
        };
40
41
        $this->loop_builder
42
            ->addProcess(RetryOnExceptionMiddleware::class, [$max_retry, $retry_on])
43
            ->addProcess(NanoSleepMiddleware::class, [$interval_on_retry_ns])
44
            ->addProcess(CallableMiddleware::class, [$loop_canceller])
45
            ->build()
46
            ->invoke();
47
    }
48
}
49