Psr::upload()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 4
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 2
rs 10
1
<?php
2
3
namespace kalanis\UploadPerPartes\Target\Remote;
4
5
6
use kalanis\UploadPerPartes\Interfaces\IOperations;
7
use kalanis\UploadPerPartes\Responses\BasicResponse;
8
use kalanis\UploadPerPartes\UploadException;
9
use Psr\Http\Client\ClientExceptionInterface;
10
use Psr\Http\Client\ClientInterface;
11
12
13
/**
14
 * Class Psr
15
 * @package kalanis\UploadPerPartes\Target\Remote
16
 * Process responses from PSR
17
 */
18
class Psr implements IOperations
19
{
20
    protected ClientInterface $client;
21
    protected Psr\Request $request;
22
    protected Psr\Response $response;
23
24 13
    public function __construct(ClientInterface $client, Psr\Request $request, Psr\Response $response)
25
    {
26 13
        $this->client = $client;
27 13
        $this->request = $request;
28 13
        $this->response = $response;
29 13
    }
30
31 2
    public function init(string $targetPath, string $targetFileName, int $length, string $clientData = ''): BasicResponse
32
    {
33
        try {
34 2
            return $this->response->init(
35 2
                $this->client->sendRequest(
36 2
                    $this->request->init($targetPath, $targetFileName, $length)
37
                ),
38
                $clientData
39
            );
40 1
        } catch (ClientExceptionInterface $ex) {
41 1
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
42
        }
43
    }
44
45 2
    public function check(string $serverData, int $segment, string $method, string $clientData = ''): BasicResponse
46
    {
47
        try {
48 2
            return $this->response->check(
49 2
                $this->client->sendRequest(
50 2
                    $this->request->check($serverData, $segment, $method)
51
                ),
52
                $clientData
53
            );
54 1
        } catch (ClientExceptionInterface $ex) {
55 1
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
56
        }
57
    }
58
59 2
    public function truncate(string $serverData, int $segment, string $clientData = ''): BasicResponse
60
    {
61
        try {
62 2
            return $this->response->truncate(
63 2
                $this->client->sendRequest(
64 2
                    $this->request->truncate($serverData, $segment)
65
                ),
66
                $clientData
67
            );
68 1
        } catch (ClientExceptionInterface $ex) {
69 1
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
70
        }
71
    }
72
73 2
    public function upload(string $serverData, string $content, string $method, string $clientData = ''): BasicResponse
74
    {
75
        try {
76 2
            return $this->response->upload(
77 2
                $this->client->sendRequest(
78 2
                    $this->request->upload($serverData, $content, $method)
79
                ),
80
                $clientData
81
            );
82 1
        } catch (ClientExceptionInterface $ex) {
83 1
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
84
        }
85
    }
86
87 2
    public function done(string $serverData, string $clientData = ''): BasicResponse
88
    {
89
        try {
90 2
            return $this->response->done(
91 2
                $this->client->sendRequest(
92 2
                    $this->request->done($serverData)
93
                ),
94
                $clientData
95
            );
96 1
        } catch (ClientExceptionInterface $ex) {
97 1
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
98
        }
99
    }
100
101 2
    public function cancel(string $serverData, string $clientData = ''): BasicResponse
102
    {
103
        try {
104 2
            return $this->response->cancel(
105 2
                $this->client->sendRequest(
106 2
                    $this->request->cancel($serverData)
107
                ),
108
                $clientData
109
            );
110 1
        } catch (ClientExceptionInterface $ex) {
111 1
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
112
        }
113
    }
114
}
115