Test Failed
Push — master ( e493d7...b1ea68 )
by Petr
11:01
created

Request::upload()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 3
rs 10
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 Data $data;
18
19
    public function __construct(Remote\Config $config, Data $data)
20
    {
21
        $this->config = $config;
22
        $this->data = $data;
23
    }
24
25
    public function init(string $targetPath, string $fileName, int $fileSize): Data
26
    {
27
        return $this->processQuery($this->config->initPath, compact('targetPath', 'fileName', 'fileSize'));
28
    }
29
30
    public function check(string $serverData, int $segment, string $method): Data
31
    {
32
        return $this->processQuery($this->config->checkPath, compact('serverData', 'segment', 'method'));
33
    }
34
35
    public function truncate(string $serverData, int $segment): Data
36
    {
37
        return $this->processQuery($this->config->truncatePath, compact('serverData', 'segment'));
38
    }
39
40
    public function upload(string $serverData, string $content, string $method): Data
41
    {
42
        return $this->processQuery($this->config->uploadPath, compact('serverData', 'content', 'method'));
43
    }
44
45
    public function done(string $serverData): Data
46
    {
47
        return $this->processQuery($this->config->donePath, compact('serverData'));
48
    }
49
50
    public function cancel(string $serverData): Data
51
    {
52
        return $this->processQuery($this->config->cancelPath, compact('serverData'));
53
    }
54
55
    /**
56
     * @param string $path
57
     * @param array<string, string|int> $params
58
     * @return Data
59
     */
60
    protected function processQuery(string $path, array $params): Data
61
    {
62
        $data = clone $this->data;
63
        $remotePath = $this->config->targetHost;
64
        if (!empty($this->config->targetPort) && (80 != $this->config->targetPort)) {
65
            $remotePath .= ':' . $this->config->targetPort;
66
        }
67
        $remotePath .= $this->config->pathPrefix . $path;
68
69
        $data->path = $remotePath;
70
        // ultra-idiotic variant with only building a query
71
        $content = http_build_query($params);
72
        $header = [
73
            'Content-type: application/x-www-form-urlencoded',
74
            'Content-length: ' . mb_strlen($content),
75
        ];
76
        $data->context = [
77
            'ssl' => [
78
//                'verify_peer' => false,
79
//                'verify_peer_name' => false,
80
//                'allow_self_signed' => true,
81
            ],
82
            'http' => [
83
                'method' => 'POST',
84
                'header' => implode("\r\n", $header),
85
                'timeout' => 30,
86
                'content' => $content,
87
            ]
88
        ];
89
90
        return $data;
91
    }
92
}
93