GetDir::process()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 21
nc 4
nop 0
dl 0
loc 27
rs 9.584
c 0
b 0
f 0
ccs 20
cts 20
cp 1
crap 4
1
<?php
2
3
namespace kalanis\RemoteRequest\Protocols\Fsp\Answer;
4
5
6
/**
7
 * Class GetDir
8
 * @package kalanis\RemoteRequest\Protocols\Fsp\Answer
9
 * Process Get Directory
10
// struct RDIRENT {
11
//     struct HEADER {
12
//         long  time;
13
//         long  size;
14
//         byte  type;
15
//     }
16
//     ASCIIZ name;
17
// }
18
// padding - round struct to 4 bytes block
19
 *
20
 * Idea for v3: Liquid headers - at first used bytes and then actual content; because it's necessary to encode long
21
 * strings (like multibyte unicode) or long numbers (like 64bit date); this remove necessity for rounding
22
 * Also need to send file rights and do not determine them extra - type-r-w-x
23
 * And maybe sending files one-by-one. Because long name in chinese fill the string name really fast. And it's better
24
 * for seeking on client side.
25
 */
26
class GetDir extends AAnswer
27
{
28
    protected GetDir\FileInfo $singleFile;
29
    protected int $position = 0;
30
    /** @var GetDir\FileInfo[] */
31
    protected array $files = [];
32
33 2
    protected function customInit(): void
34
    {
35 2
        parent::customInit();
36 2
        $this->singleFile = new GetDir\FileInfo('');
37
    }
38
39 2
    public function process(): parent
40
    {
41 2
        $this->position = $this->answer->getFilePosition();
42 2
        $data = $this->answer->getContent();
43 2
        $dataLen = $this->answer->getDataLength();
44
        // on begining up to 12 chars and check by last one against 0x00 (NULL byte), then add by 4; read ends with overflowing the body size
45 2
        $startSeq = 0;
46 2
        $endSeq = 0;
47 2
        $newPacket = true;
48
        do {
49 2
            if ($newPacket) {
50 2
                $endSeq =+ 12;
51 2
                $newPacket = false;
52
            }
53 2
            $record = substr($data, $startSeq, $endSeq);
54 2
            if (chr(0) == substr($record, -1, 1)) {
55 2
                $file = clone $this->singleFile;
56 2
                $this->files[] = $file->setData($record);
57
58 2
                $startSeq += $endSeq;
59 2
                $endSeq = 0;
60 2
                $newPacket = true;
61
            } else {
62 2
                $endSeq += 4;
63
            }
64 2
        } while ($startSeq < $dataLen);
65 2
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type kalanis\RemoteRequest\Protocols\Fsp\Answer\GetDir which is incompatible with the type-hinted return parent.
Loading history...
66
    }
67
68
    /**
69
     * @return GetDir\FileInfo[]
70
     */
71 2
    public function getFiles(): array
72
    {
73 2
        return $this->files;
74
    }
75
}
76