Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| IcapStatus | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
5 | |
100.00% |
1 / 1 |
| failure | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * SPDX-License-Identifier: EUPL-1.2 |
| 5 | * |
| 6 | * This file is part of icap-flow. |
| 7 | * |
| 8 | * Licensed under the EUPL, Version 1.2 only (the "Licence"); |
| 9 | * you may not use this work except in compliance with the Licence. |
| 10 | * You may obtain a copy of the Licence at: |
| 11 | * |
| 12 | * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the Licence is distributed on an "AS IS" basis, |
| 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | */ |
| 18 | |
| 19 | declare(strict_types=1); |
| 20 | |
| 21 | namespace Ndrstmr\Icap; |
| 22 | |
| 23 | use Ndrstmr\Icap\Exception\IcapClientException; |
| 24 | use Ndrstmr\Icap\Exception\IcapServerException; |
| 25 | |
| 26 | /** |
| 27 | * What an ICAP status says about a failure, in one place (N03): the scan path ({@see IcapClient}) and the preview |
| 28 | * strategy ({@see DefaultPreviewStrategy}) classify 4xx and 5xx alike, so a 503 is an {@see IcapServerException} |
| 29 | * whether it answers a preview, a continuation or a plain request. |
| 30 | * |
| 31 | * @internal |
| 32 | */ |
| 33 | final class IcapStatus |
| 34 | { |
| 35 | /** |
| 36 | * The exception for a 4xx (the request was refused) or a 5xx (the server failed), or null for any other status. |
| 37 | * The message carries the code and nothing the server sent. |
| 38 | */ |
| 39 | public static function failure(int $code): IcapClientException|IcapServerException|null |
| 40 | { |
| 41 | if ($code >= 400 && $code < 500) { |
| 42 | return new IcapClientException( |
| 43 | sprintf('ICAP client error (%d) — request rejected by server.', $code), |
| 44 | $code, |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | if ($code >= 500 && $code < 600) { |
| 49 | return new IcapServerException( |
| 50 | sprintf('ICAP server error (%d) — server failed to service the request.', $code), |
| 51 | $code, |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | return null; |
| 56 | } |
| 57 | } |