Passed
Push — master ( 87c83f...ac88b3 )
by Sam
03:00
created

DecodeException::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Badcow DNS Library.
7
 *
8
 * (c) Samuel Williams <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Badcow\DNS\Rdata;
15
16
class DecodeException extends \Exception
17
{
18 2
    public function __construct(string $type, string $rdata, int $code = 0, \Throwable $previous = null)
19
    {
20 2
        $message = sprintf('Unable to decode %s record rdata from binary data "%s"', $type, $this->binaryToHex($rdata));
21 2
        parent::__construct($message, $code, $previous);
22 2
    }
23
24
    /**
25
     * Convert a binary string into hexadecimal values.
26
     *
27
     * @param string $rdata
28
     *
29
     * @return string
30
     */
31 2
    public function binaryToHex(string $rdata): string
32
    {
33
        $hex = array_map(function ($char) {
34 2
            return sprintf('0x%02x', ord($char));
35 2
        }, str_split($rdata));
36
37 2
        return implode(' ', $hex);
38
    }
39
}
40