Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CriticalSection
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 2
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
 stop
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace Wikimedia\RequestTimeout\Detail;
4
5use ExcimerTimer;
6use Wikimedia\RequestTimeout\EmergencyTimeoutException;
7
8/**
9 * Internal class for tracking critical sections
10 *
11 * @internal
12 */
13class CriticalSection {
14    /** @var string */
15    private $name;
16    /** @var ExcimerTimer|null */
17    private $emergencyTimer;
18
19    /**
20     * @see RequestTimeout::createCriticalSectionProvider
21     *
22     * @param string $name
23     * @param float $emergencyLimit
24     * @param callable|null $emergencyCallback
25     */
26    public function __construct( $name, $emergencyLimit, $emergencyCallback ) {
27        $this->name = $name;
28        if ( !$emergencyCallback ) {
29            /** @return never */
30            $emergencyCallback = static function () use ( $name, $emergencyLimit ) {
31                throw new EmergencyTimeoutException( $name, $emergencyLimit );
32            };
33        }
34
35        if ( $emergencyLimit > 0 && $emergencyLimit !== INF ) {
36            $this->emergencyTimer = new ExcimerTimer;
37            $this->emergencyTimer->setInterval( $emergencyLimit );
38            $this->emergencyTimer->setCallback( $emergencyCallback );
39            $this->emergencyTimer->start();
40        }
41    }
42
43    /**
44     * Stop the emergency timer, if it was started
45     */
46    public function stop() {
47        if ( $this->emergencyTimer ) {
48            $this->emergencyTimer->stop();
49            $this->emergencyTimer = null;
50        }
51    }
52}