clock = is_callable( $clock ) ? $clock : static function () { return microtime( true ); }; $this->expires = $this->now() + (float) $seconds; } /** * Seconds left, never negative. * * @return float */ public function remaining() { return max( 0.0, $this->expires - $this->now() ); } /** * Timeout for a call: the smaller of `$max` and the time left. * * @param float $max Call timeout in seconds. * @return float */ public function timeout( $max ) { return min( (float) $max, $this->remaining() ); } /** * Whether at least `$seconds` are left. * * @param float $seconds Seconds. * @return bool */ public function has( $seconds ) { return $this->remaining() >= (float) $seconds; } /** * Error returned instead of a request when too little time is left. * * @return \WP_Error */ public static function error() { return new \WP_Error( 'atproto_comments_timeout', __( 'There was not enough time left to contact the AT Protocol server. Try again.', 'parolas' ), array( 'status' => 502 ) ); } /** * Current time from the clock. * * @return float */ private function now() { return (float) call_user_func( $this->clock ); } }