Reader::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 2
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpIso\Descriptor;
6
7
use PhpIso\Descriptor;
8
use PhpIso\IsoFile;
9
use PhpIso\Util\Buffer;
10
11
class Reader
12
{
13 20
    public function __construct(protected IsoFile $isoFile)
14
    {
15 20
    }
16
17 20
    public function read(): ?Descriptor
18
    {
19 20
        $string = $this->isoFile->read(2048);
20
21 20
        if ($string === false) {
22
            return null;
23
        }
24
25
        /** @var array<int, int>|false $bytes */
26 20
        $bytes = unpack('C*', $string);
27
28 20
        if ($bytes === false) {
29
            return null;
30
        }
31
32 20
        $offset = 1;
33
34 20
        if (! isset($bytes[$offset])) {
35 1
            throw new Exception('Failed to read buffer entry ' . $offset);
36
        }
37
38 19
        $type = $bytes[$offset];
39 19
        $offset++;
40 19
        $stdId = Buffer::getString($bytes, 5, $offset);
41
42 19
        if (! isset($bytes[$offset])) {
43
            throw new Exception('Failed to read buffer entry ' . $offset);
44
        }
45
46 19
        $version = $bytes[$offset];
47 19
        $offset++;
48
49
        // Check for UDF-specific descriptors
50 19
        if ($type === Type::BOOT_RECORD_DESC) {
51 12
            $type = match ($stdId) {
52 18
                'CD001' => Type::BOOT_RECORD_DESC,
53 16
                UdfType::BEA01 => Type::UDF_BEA_VOLUME_DESC,
54 16
                UdfType::NSR02 => Type::UDF_NSR2_VOLUME_DESC,
55 16
                UdfType::NSR03 => Type::UDF_NSR3_VOLUME_DESC,
56 16
                UdfType::TEA01 => Type::UDF_TEA_VOLUME_DESC,
57 6
                default => throw new Exception('Failed to detect UDF'),
58 12
            };
59
        }
60
61 19
        $descriptor = Factory::create($type, $stdId, $version, $bytes);
62 19
        $descriptor->init($this->isoFile, $offset);
63
64 19
        return $descriptor;
65
    }
66
}
67