Unreachable::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 4
dl 0
loc 5
ccs 0
cts 5
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Minotaur
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015-2017 Appertly
19
 * @license   Apache-2.0
20
 */
21
namespace Minotaur\Net\Exception;
22
23
/**
24
 * An exception for connection errors.
25
 */
26
class Unreachable extends \RuntimeException implements \Minotaur\Net\Exception
27
{
28
    private const ERRORS = [
29
        CURLE_OPERATION_TIMEOUTED, CURLE_COULDNT_RESOLVE_HOST,
30
        CURLE_COULDNT_RESOLVE_PROXY, CURLE_COULDNT_CONNECT, CURLE_SSL_CONNECT_ERROR,
31
        CURLE_SSL_PEER_CERTIFICATE, CURLE_SSL_CACERT, CURLE_SEND_ERROR,
32
        CURLE_RECV_ERROR, CURLE_GOT_NOTHING
33
    ];
34
35
    /**
36
     * @var array<string,mixed> The cURL handle info
37
     */
38
    private $info;
39
40
    /**
41
     * @param iterable<string,mixed> $info The cURL handle info
0 ignored issues
show
Documentation introduced by
The doc-type iterable<string,mixed> could not be parsed: Expected "|" or "end of type", but got "<" at position 8. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
42
     * @param $msg - The message
43
     * @param $code - The code
44
     * @param $previous - Any nested exception
45
     */
46
    public function __construct(iterable $info, string $msg, int $code = 0, \Exception $previous = null)
47
    {
48
        $this->info = is_array($info) ? $info : iterator_to_array($info, true);
49
        parent::__construct($msg, $code, $previous);
50
    }
51
52
    /**
53
     * Gets the cURL handle info.
54
     *
55
     * @return array<string,mixed> the handle info
56
     */
57
    public function getInfo(): array
58
    {
59
        return $this->info;
60
    }
61
62
    /**
63
     * Determines whether the code returned from cURL is one this class supports.
64
     *
65
     * @param int $code The code
66
     * @return bool whether this class should be used
67
     */
68
    public static function isUsable(int $code) : bool
69
    {
70
        return in_array($code, self::ERRORS, true);
71
    }
72
}
73