Line::pack()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 18
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 20
ccs 19
cts 19
cp 1
crap 1
rs 9.6666
1
<?php
2
3
namespace kalanis\UploadPerPartes\Target\Local\DrivingFile\DataEncoders;
4
5
6
use kalanis\UploadPerPartes\Uploader\Data;
7
use kalanis\UploadPerPartes\Uploader\RandomStrings;
8
9
10
/**
11
 * Class Line
12
 * @package kalanis\UploadPerPartes\ServerData\DataModifiers
13
 * Driver file - format string line
14
 */
15
class Line extends AEncoder
16
{
17
    private const DATA_SEPARATOR = '|';
18
19 13
    public function unpack(string $content): Data
20
    {
21 13
        $line = explode(self::DATA_SEPARATOR, $content);
22 13
        $libData = new Data();
23 13
        $libData->tempDir = strval($line[1]); // path to temp file
24 13
        $libData->tempName = strval($line[3]); // final file path
25 13
        $libData->targetDir = strval($line[5]);
26 13
        $libData->targetName = strval($line[7]);
27 13
        $libData->fileSize = max(0, intval($line[9])); // final size
28 13
        $libData->partsCount = max(0, intval($line[11])); // is on parts...
29 13
        $libData->bytesPerPart = max(0, intval($line[13])); // how long is single part
30 13
        $libData->lastKnownPart = max(0, intval($line[15])); // how many parts has been obtained
31
32 13
        return $libData->clear();
33
    }
34
35 15
    public function pack(Data $data): string
36
    {
37 15
        return implode(self::DATA_SEPARATOR, [
38 15
            RandomStrings::randomLength(),
39 15
            $data->tempDir,
40 15
            RandomStrings::randomLength(),
41 15
            $data->tempName,
42 15
            RandomStrings::randomLength(),
43 15
            $data->targetDir,
44 15
            RandomStrings::randomLength(),
45 15
            $data->targetName,
46 15
            RandomStrings::randomLength(),
47 15
            $data->fileSize,
48 15
            RandomStrings::randomLength(),
49 15
            $data->partsCount,
50 15
            RandomStrings::randomLength(),
51 15
            $data->bytesPerPart,
52 15
            RandomStrings::randomLength(),
53 15
            $data->lastKnownPart,
54 15
            RandomStrings::randomLength(),
55
        ]);
56
    }
57
}
58