(float) self::MAX_USD ) { return false; } return 0.0 === $value || self::toMicros( $value ) > 0; } /** * Whether a limit is set and this month's estimated spend has reached it. * With a limit set, a spend the database could not read counts as * reached: a request held back costs nothing, one sent unchecked may cost * past the limit. * * @return bool */ public static function isReached(): bool { $limit = self::limitMicros(); if ( $limit <= 0 ) { return false; } $spent = UsageTracker::currentMonthSpendMicros(); return null === $spent || $spent >= $limit; } /** * Refuse an AI request once the limit is reached. Callers run it before * anything is sent, outside their provider-failure handling. * * @param string $provider AIPT provider id of the request, when known. * @return void * @throws ProviderCallException With reason spend_limit_reached. */ public static function refuseIfReached( string $provider = '' ): void { if ( self::isReached() ) { // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- A fixed technical text for the log; a merchant sees displayMessage(). throw ProviderCallException::spendLimitReached( $provider ); } } /** * The same refusal as a WP_Error, for a route that answers before any * provider client exists, or null while requests may go. * * @return \WP_Error|null */ public static function refusalError(): ?\WP_Error { if ( ! self::isReached() ) { return null; } return new \WP_Error( 'aipt_spend_limit_reached', ProviderCallException::userMessage( ProviderCallException::SPEND_LIMIT_REACHED, '' ), array( 'status' => ProviderCallException::httpStatusFor( ProviderCallException::SPEND_LIMIT_REACHED ) ) ); } }