Lines 90.90% 10 / 11
Functions and Methods 80.00% 4 / 5
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ManualTimer 90.90% 10 / 11 80.00% 4 / 5 7.04 0.00% 0 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 cancel 100.00% 1 / 1 100.00% 1 / 1 1
 isPending 100.00% 1 / 1 100.00% 1 / 1 1
 fire 75.00% 3 / 4 0.00% 0 / 1 2.06
 release 100.00% 4 / 4 100.00% 1 / 1 2
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Testing\Time;
6
7use Closure;
8use Stewart\Contracts\Time\MonotonicTime;
9use Stewart\Contracts\Time\TimerHandle;
10
11final class ManualTimer implements TimerHandle
12{
13    private bool $pending = true;
14
15    /**
16     * @param Closure(): void $callback
17     * @param Closure(self): void $onRelease
18     */
19    public function __construct(
20        public readonly MonotonicTime $dueAt,
21        private readonly Closure $callback,
22        private readonly Closure $onRelease,
23    ) {}
24
25    public function cancel(): void
26    {
27        $this->release();
28    }
29
30    public function isPending(): bool
31    {
32        return $this->pending;
33    }
34
35    public function fire(): void
36    {
37        if (!$this->pending) {
38            return;
39        }
40
41        $this->release();
42        ($this->callback)();
43    }
44
45    private function release(): void
46    {
47        if (!$this->pending) {
48            return;
49        }
50
51        $this->pending = false;
52        ($this->onRelease)($this);
53    }
54}