NetworkException   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 52
c 3
b 1
f 0
dl 0
loc 74
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A createByCode() 0 14 3
1
<?php
2
3
namespace Transmission\Exception;
4
5
/**
6
 * NetworkException.
7
 */
8
class NetworkException extends \Exception
9
{
10
    public const CONFLICT = 409;
11
12
    public static $statusCodes = [
13
        // 4xx: Client Error - The request contains bad syntax or cannot be fulfilled.
14
        400 => 'Bad Request',
15
        401 => 'Unauthorized',
16
        402 => 'Payment Required',
17
        403 => 'Forbidden',
18
        404 => 'Not Found',
19
        405 => 'Method Not Allowed',
20
        406 => 'Not Acceptable',
21
        407 => 'Proxy Authentication Required',
22
        408 => 'Request Timeout',
23
        409 => 'Conflict',
24
        410 => 'Gone',
25
        411 => 'Length Required',
26
        412 => 'Precondition Failed',
27
        413 => 'Payload Too Large',
28
        414 => 'Request-URI Too Long',
29
        415 => 'Unsupported Media Type',
30
        416 => 'Requested Range Not Satisfiable',
31
        417 => 'Expectation Failed',
32
        418 => 'I\'m a teapot',
33
        421 => 'Misdirected Request',
34
        422 => 'Unprocessable Entity',
35
        423 => 'Locked',
36
        424 => 'Failed Dependency',
37
        426 => 'Upgrade Required',
38
        428 => 'Precondition Required',
39
        429 => 'Too Many Requests',
40
        431 => 'Request Header Fields Too Large',
41
        444 => 'Connection Closed Without Response',
42
        451 => 'Unavailable For Legal Reasons',
43
        499 => 'Client Closed Request',
44
45
        // 5xx: Server Error - The server failed to fulfill an apparently valid request.
46
        500 => 'Internal Server Error',
47
        501 => 'Not Implemented',
48
        502 => 'Bad Gateway',
49
        503 => 'Service Unavailable',
50
        504 => 'Gateway Timeout',
51
        505 => 'HTTP Version Not Supported',
52
        506 => 'Variant Also Negotiates',
53
        507 => 'Insufficient Storage',
54
        508 => 'Loop Detected',
55
        510 => 'Not Extended',
56
        511 => 'Network Authentication Required',
57
        599 => 'Network Connect Timeout Error',
58
    ];
59
60
    /**
61
     * Create Exception by Network Code.
62
     *
63
     * @param int         $statusCode
64
     * @param null|string $message
65
     *
66
     * @return static
67
     */
68
    public static function createByCode(int $statusCode, string $message = null): self
69
    {
70
        $errorMessage = null;
71
        if (isset(static::$statusCodes[$statusCode])) {
72
            $errorMessage = static::$statusCodes[$statusCode];
73
74
            if (filled($message)) {
75
                $errorMessage = $errorMessage.' - '.$message;
76
            }
77
        }
78
79
        $message = sprintf('%d: %s', $statusCode, $errorMessage ?? $message);
80
81
        return new static($message, $statusCode);
82
    }
83
}
84