1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\UploadPerPartes\Target\Remote\Psr; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\UploadPerPartes\Target\Remote; |
7
|
|
|
use Psr\Http\Message\RequestInterface; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Request |
12
|
|
|
* @package kalanis\UploadPerPartes\Target\Remote\Psr |
13
|
|
|
* Create requests for PSR |
14
|
|
|
*/ |
15
|
|
|
class Request |
16
|
|
|
{ |
17
|
|
|
protected RequestInterface $requestObject; |
18
|
|
|
protected Remote\Config $config; |
19
|
|
|
|
20
|
19 |
|
public function __construct(RequestInterface $requestObject, Remote\Config $config) |
21
|
|
|
{ |
22
|
19 |
|
$this->requestObject = $requestObject; |
23
|
19 |
|
$this->config = $config; |
24
|
19 |
|
} |
25
|
|
|
|
26
|
3 |
|
public function init(string $targetPath, string $fileName, int $fileSize): RequestInterface |
27
|
|
|
{ |
28
|
3 |
|
return $this->processQuery($this->config->initPath, compact('targetPath', 'fileName', 'fileSize')); |
29
|
|
|
} |
30
|
|
|
|
31
|
3 |
|
public function check(string $serverData, int $segment, string $method): RequestInterface |
32
|
|
|
{ |
33
|
3 |
|
return $this->processQuery($this->config->checkPath, compact('serverData', 'segment', 'method')); |
34
|
|
|
} |
35
|
|
|
|
36
|
3 |
|
public function truncate(string $serverData, int $segment): RequestInterface |
37
|
|
|
{ |
38
|
3 |
|
return $this->processQuery($this->config->truncatePath, compact('serverData', 'segment')); |
39
|
|
|
} |
40
|
|
|
|
41
|
3 |
|
public function upload(string $serverData, string $content, string $method): RequestInterface |
42
|
|
|
{ |
43
|
3 |
|
return $this->processQuery($this->config->uploadPath, compact('serverData', 'content', 'method')); |
44
|
|
|
} |
45
|
|
|
|
46
|
3 |
|
public function done(string $serverData): RequestInterface |
47
|
|
|
{ |
48
|
3 |
|
return $this->processQuery($this->config->donePath, compact('serverData')); |
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
public function cancel(string $serverData): RequestInterface |
52
|
|
|
{ |
53
|
3 |
|
return $this->processQuery($this->config->cancelPath, compact('serverData')); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $path |
58
|
|
|
* @param array<string, string|int> $params |
59
|
|
|
* @return RequestInterface |
60
|
|
|
*/ |
61
|
18 |
|
protected function processQuery(string $path, array $params): RequestInterface |
62
|
|
|
{ |
63
|
18 |
|
$request = clone $this->requestObject; |
64
|
|
|
// need to create POST with these (and other) params |
65
|
18 |
|
$uri = clone $request->getUri() |
66
|
18 |
|
->withHost($this->config->targetHost) |
67
|
18 |
|
->withPort($this->config->targetPort) |
68
|
18 |
|
->withPath($this->config->pathPrefix . $path) |
69
|
18 |
|
->withQuery('') |
70
|
|
|
; |
71
|
18 |
|
$body = $request->getBody(); |
72
|
18 |
|
$body->rewind(); |
73
|
18 |
|
$body->write(http_build_query($params)); |
74
|
|
|
$request = $request |
75
|
18 |
|
->withUri($uri) |
76
|
18 |
|
->withBody($body) |
77
|
18 |
|
->withAddedHeader('Content-type', 'application/x-www-form-urlencoded'); |
78
|
|
|
; |
79
|
18 |
|
return $request; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|