Lines 0.00% 0 / 8
Methods 0.00% 0 / 5
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 0.00% 0 / 1 0.00% 0 / 1 2
 waitUntilOpen 0.00% 0 / 3 0.00% 0 / 1 2
 open 0.00% 0 / 2 0.00% 0 / 1 6
 isOpen 0.00% 0 / 1 0.00% 0 / 1 2
 countWaiters 0.00% 0 / 1 0.00% 0 / 1 2
10final class Latch
11{
12    /** @var DeferredFuture<null> */
13    private readonly DeferredFuture $opened;
14
15    private int $waiters = 0;
16
17    public function __construct()
18    {
19        $this->opened = new DeferredFuture();
20    }
21
22    public function waitUntilOpen(?Cancellation $cancellation = null): void
23    {
24        ++$this->waiters;
25
26        try {
27            $this->opened->getFuture()->await($cancellation);
28        } finally {
29            --$this->waiters;
30        }
31    }
32
33    public function open(): void
34    {
35        if (!$this->opened->isComplete()) {
36            $this->opened->complete();
37        }
38    }
39
40    public function isOpen(): bool
41    {
42        return $this->opened->isComplete();
43    }
44
45    public function countWaiters(): int
46    {
47        return $this->waiters;
48    }
49}