Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 14
CriticalSectionScope
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 4
56
0.00% covered (danger)
0.00%
0 / 14
 __construct
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 4
 __destruct
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 5
 exit
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 4
 getId
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
<?php
namespace Wikimedia\RequestTimeout;
/**
 * Class for automatically ending a critical section when a variable goes out
 * of scope.
 */
class CriticalSectionScope {
    /** @var int */
    private $id;
    /** @var callable */
    private $exitCallback;
    /** @var callable|null */
    private $implicitExitCallback;
    /** @var bool */
    private $hasExited = false;
    /**
     * @internal
     * @param int $id
     * @param callable $exitCallback
     * @param callable|null $implicitExitCallback
     */
    public function __construct( $id, $exitCallback, $implicitExitCallback ) {
        $this->id = $id;
        $this->exitCallback = $exitCallback;
        $this->implicitExitCallback = $implicitExitCallback;
    }
    /**
     * If the section has not already been exited, exit it and call the
     * destruct callback.
     *
     * @throws TimeoutException
     */
    public function __destruct() {
        if ( !$this->hasExited ) {
            $this->exit();
            if ( $this->implicitExitCallback ) {
                ( $this->implicitExitCallback )( $this->id );
            }
        }
    }
    /**
     * Exit the critical section
     *
     * @throws TimeoutException
     */
    public function exit() {
        if ( !$this->hasExited ) {
            $this->hasExited = true;
            ( $this->exitCallback )( $this->id );
        }
    }
    /**
     * Get an integer uniquely identifying the section (within the scope of the
     * parent RequestTimeout object).
     *
     * @since 1.1.0
     * @return int
     */
    public function getId() {
        return $this->id;
    }
}