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\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
    public function __construct(RequestInterface $requestObject, Remote\Config $config)
21
    {
22
        $this->requestObject = $requestObject;
23
        $this->config = $config;
24
    }
25
26
    public function init(string $targetPath, string $fileName, int $fileSize): RequestInterface
27
    {
28
        return $this->processQuery($this->config->initPath, compact('targetPath', 'fileName', 'fileSize'));
29
    }
30
31
    public function check(string $serverData, int $segment, string $method): RequestInterface
32
    {
33
        return $this->processQuery($this->config->checkPath, compact('serverData', 'segment', 'method'));
34
    }
35
36
    public function truncate(string $serverData, int $segment): RequestInterface
37
    {
38
        return $this->processQuery($this->config->truncatePath, compact('serverData', 'segment'));
39
    }
40
41
    public function upload(string $serverData, string $content, string $method): RequestInterface
42
    {
43
        return $this->processQuery($this->config->uploadPath, compact('serverData', 'content', 'method'));
44
    }
45
46
    public function done(string $serverData): RequestInterface
47
    {
48
        return $this->processQuery($this->config->donePath, compact('serverData'));
49
    }
50
51
    public function cancel(string $serverData): RequestInterface
52
    {
53
        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
    protected function processQuery(string $path, array $params): RequestInterface
62
    {
63
        $request = clone $this->requestObject;
64
        // need to create POST with these (and other) params
65
        $uri = clone $request->getUri()
66
            ->withHost($this->config->targetHost)
67
            ->withPort($this->config->targetPort)
68
            ->withPath($this->config->pathPrefix . $path)
69
            ->withQuery('')
70
        ;
71
        $body = $request->getBody();
72
        $body->rewind();
73
        $body->write(http_build_query($params));
74
        $request = $request
75
            ->withUri($uri)
76
            ->withBody($body)
77
            ->withAddedHeader('Content-type', 'application/x-www-form-urlencoded');
78
        ;
79
        return $request;
80
    }
81
}
82