Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
90.91% |
10 / 11 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
CriticalSectionScope | |
90.91% |
10 / 11 |
|
75.00% |
3 / 4 |
7.04 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
__destruct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
exit | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace Wikimedia\RequestTimeout; |
4 | |
5 | /** |
6 | * Class for automatically ending a critical section when a variable goes out |
7 | * of scope. |
8 | */ |
9 | class CriticalSectionScope { |
10 | /** @var int */ |
11 | private $id; |
12 | |
13 | /** @var callable */ |
14 | private $exitCallback; |
15 | |
16 | /** @var callable|null */ |
17 | private $implicitExitCallback; |
18 | |
19 | /** @var bool */ |
20 | private $hasExited = false; |
21 | |
22 | /** |
23 | * @internal |
24 | * @param int $id |
25 | * @param callable $exitCallback |
26 | * @param callable|null $implicitExitCallback |
27 | */ |
28 | public function __construct( $id, $exitCallback, $implicitExitCallback ) { |
29 | $this->id = $id; |
30 | $this->exitCallback = $exitCallback; |
31 | $this->implicitExitCallback = $implicitExitCallback; |
32 | } |
33 | |
34 | /** |
35 | * If the section has not already been exited, exit it and call the |
36 | * destruct callback. |
37 | * |
38 | * @throws TimeoutException |
39 | */ |
40 | public function __destruct() { |
41 | if ( !$this->hasExited ) { |
42 | $this->exit(); |
43 | if ( $this->implicitExitCallback ) { |
44 | ( $this->implicitExitCallback )( $this->id ); |
45 | } |
46 | } |
47 | } |
48 | |
49 | /** |
50 | * Exit the critical section |
51 | * |
52 | * @throws TimeoutException |
53 | */ |
54 | public function exit() { |
55 | if ( !$this->hasExited ) { |
56 | $this->hasExited = true; |
57 | ( $this->exitCallback )( $this->id ); |
58 | } |
59 | } |
60 | |
61 | /** |
62 | * Get an integer uniquely identifying the section (within the scope of the |
63 | * parent RequestTimeout object). |
64 | * |
65 | * @since 1.1.0 |
66 | * @return int |
67 | */ |
68 | public function getId() { |
69 | return $this->id; |
70 | } |
71 | } |