Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
RemoteInstanceApiException
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 forHttpError
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 forConnectionFailure
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5/** @license For full copyright and license information, please see the LICENSE.md file. */
6
7namespace App\Core\Instance\Domain\Exception;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11use RuntimeException;
12
13/**
14 * Remote Instance API Exception.
15 *
16 * Thrown when HTTP communication with a remote client SaaS instance fails.
17 *
18 * @package App\Core\Instance\Domain\Exception
19 */
20final class RemoteInstanceApiException extends RuntimeException
21{
22    /**
23     * Creates exception for HTTP error status codes.
24     *
25     * @param string $endpoint Target API endpoint.
26     * @param int    $statusCode HTTP response status code.
27     * @param string $responseBody Raw response body content.
28     * @return self New exception instance.
29     */
30    public static function forHttpError(string $endpoint, int $statusCode, string $responseBody): self
31    {
32        $truncated = mb_substr($responseBody, 0, 250);
33        return new self(
34            sprintf("Remote API call to '%s' failed with HTTP %d: %s", $endpoint, $statusCode, $truncated),
35            $statusCode
36        );
37    }
38
39    /**
40     * Creates exception for network transport or connection failures.
41     *
42     * @param string $endpoint Target API endpoint.
43     * @param string $errorMessage Network error description.
44     * @return self New exception instance.
45     */
46    public static function forConnectionFailure(string $endpoint, string $errorMessage): self
47    {
48        return new self(
49            sprintf("Failed to connect to remote instance API at '%s': %s", $endpoint, $errorMessage),
50            0
51        );
52    }
53}