Request   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 77
ccs 33
cts 33
cp 1
rs 10
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A done() 0 3 1
A truncate() 0 3 1
A cancel() 0 3 1
A __construct() 0 4 1
A upload() 0 3 1
A check() 0 3 1
A processQuery() 0 31 3
A init() 0 3 1
1
<?php
2
3
namespace kalanis\UploadPerPartes\Target\Remote\Internals;
4
5
6
use kalanis\UploadPerPartes\Target\Remote;
7
8
9
/**
10
 * Class Request
11
 * @package kalanis\UploadPerPartes\Target\Remote\Internals
12
 * Create requests for internal functions
13
 */
14
class Request
15
{
16
    protected Remote\Config $config;
17
    protected RequestData $data;
18
19 13
    public function __construct(Remote\Config $config, RequestData $data)
20
    {
21 13
        $this->config = $config;
22 13
        $this->data = $data;
23 13
    }
24
25 2
    public function init(string $targetPath, string $fileName, int $fileSize): RequestData
26
    {
27 2
        return $this->processQuery($this->config->initPath, compact('targetPath', 'fileName', 'fileSize'));
28
    }
29
30 2
    public function check(string $serverData, int $segment, string $method): RequestData
31
    {
32 2
        return $this->processQuery($this->config->checkPath, compact('serverData', 'segment', 'method'));
33
    }
34
35 2
    public function truncate(string $serverData, int $segment): RequestData
36
    {
37 2
        return $this->processQuery($this->config->truncatePath, compact('serverData', 'segment'));
38
    }
39
40 2
    public function upload(string $serverData, string $content, string $method): RequestData
41
    {
42 2
        return $this->processQuery($this->config->uploadPath, compact('serverData', 'content', 'method'));
43
    }
44
45 2
    public function done(string $serverData): RequestData
46
    {
47 2
        return $this->processQuery($this->config->donePath, compact('serverData'));
48
    }
49
50 2
    public function cancel(string $serverData): RequestData
51
    {
52 2
        return $this->processQuery($this->config->cancelPath, compact('serverData'));
53
    }
54
55
    /**
56
     * @param string $path
57
     * @param array<string, string|int> $params
58
     * @return RequestData
59
     */
60 12
    protected function processQuery(string $path, array $params): RequestData
61
    {
62 12
        $data = clone $this->data;
63 12
        $remotePath = $this->config->targetHost;
64 12
        if (!empty($this->config->targetPort) && (80 != $this->config->targetPort)) {
65 6
            $remotePath .= ':' . $this->config->targetPort;
66
        }
67 12
        $remotePath .= $this->config->pathPrefix . $path;
68
69 12
        $data->path = $remotePath;
70
        // ultra-idiotic variant with only building a query
71 12
        $content = http_build_query($params);
72
        $header = [
73 12
            'Content-type: application/x-www-form-urlencoded',
74 12
            'Content-length: ' . mb_strlen($content),
75
        ];
76 12
        $data->context = [
77
            'ssl' => [
78
//                'verify_peer' => false,
79
//                'verify_peer_name' => false,
80
//                'allow_self_signed' => true,
81 12
            ],
82
            'http' => [
83 12
                'method' => 'POST',
84 12
                'header' => implode("\r\n", $header),
85 12
                'timeout' => 30,
86 12
                'content' => $content,
87
            ]
88
        ];
89
90 12
        return $data;
91
    }
92
}
93