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

Response::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 18
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 22
rs 9.6666
1
<?php
2
3
namespace kalanis\UploadPerPartes\Target\Remote\Psr;
4
5
6
use JsonException;
7
use kalanis\UploadPerPartes\Interfaces;
8
use kalanis\UploadPerPartes\Responses;
9
use kalanis\UploadPerPartes\Traits\TLang;
10
use kalanis\UploadPerPartes\UploadException;
11
use Psr\Http\Message\ResponseInterface;
12
use stdClass;
13
14
15
/**
16
 * Class Response
17
 * @package kalanis\UploadPerPartes\Target\Remote\Psr
18
 * Process responses from PSR
19
 */
20
class Response
21
{
22
    use TLang;
23
24
    protected Responses\Factory $responseFactory;
25
26
    public function __construct(Responses\Factory $responseFactory, ?Interfaces\IUppTranslations $lang = null)
27
    {
28
        $this->responseFactory = $responseFactory;
29
        $this->setUppLang($lang);
30
    }
31
32
    /**
33
     * @param ResponseInterface $response
34
     * @param string $clientData
35
     * @throws UploadException
36
     * @return Responses\BasicResponse
37
     */
38
    public function init(ResponseInterface $response, string $clientData): Responses\BasicResponse
39
    {
40
        $parsed = $this->parseResponse($response);
41
        if (Responses\BasicResponse::STATUS_OK == $parsed->status) {
42
            $data = $this->responseFactory->getResponse(Responses\Factory::RESPONSE_INIT);
43
            /** @var Responses\InitResponse $data */
44
            return $data->setPassedInitData(
45
                strval($parsed->name ?? null),
46
                intval(max(0, $parsed->totalParts ?? 0)),
47
                intval(max(0,$parsed->lastKnownPart ?? 0)),
48
                intval(max(0, $parsed->partSize ?? 0)),
49
                strval($parsed->encoder ?? 'base64'),
50
                strval($parsed->check ?? 'md5')
51
            )->setBasics(
52
                strval($parsed->serverKey ?? ''),
53
                $clientData
54
            );
55
        } else {
56
            return $this->responseError(
57
                strval($parsed->message ?? ''),
58
                strval($parsed->serverKey ?? ''),
59
                $clientData
60
            );
61
        }
62
    }
63
64
    /**
65
     * @param ResponseInterface $response
66
     * @param string $clientData
67
     * @throws UploadException
68
     * @return Responses\BasicResponse
69
     */
70
    public function check(ResponseInterface $response, string $clientData): Responses\BasicResponse
71
    {
72
        $parsed = $this->parseResponse($response);
73
        if (Responses\BasicResponse::STATUS_OK == $parsed->status) {
74
            $data = $this->responseFactory->getResponse(Responses\Factory::RESPONSE_CHECK);
75
            /** @var Responses\CheckResponse $data */
76
            return $data->setChecksum(
77
                strval($parsed->method ?? ''),
78
                strval($parsed->checksum ?? '')
79
            )->setBasics(
80
                strval($parsed->serverKey ?? ''),
81
                $clientData
82
            );
83
        } else {
84
            return $this->responseError(
85
                strval($parsed->message ?? ''),
86
                strval($parsed->serverKey ?? ''),
87
                $clientData
88
            );
89
        }
90
    }
91
92
    /**
93
     * @param ResponseInterface $response
94
     * @param string $clientData
95
     * @throws UploadException
96
     * @return Responses\BasicResponse
97
     */
98
    public function truncate(ResponseInterface $response, string $clientData): Responses\BasicResponse
99
    {
100
        $parsed = $this->parseResponse($response);
101
        if (Responses\BasicResponse::STATUS_OK == $parsed->status) {
102
            $data = $this->responseFactory->getResponse(Responses\Factory::RESPONSE_TRUNCATE);
103
            /** @var Responses\LastKnownResponse $data */
104
            return $data->setLastKnown(
105
                intval(max(0, $parsed->lastKnown ?? 0))
106
            )->setBasics(
107
                strval($parsed->serverKey ?? ''),
108
                $clientData
109
            );
110
        } else {
111
            return $this->responseError(
112
                strval($parsed->message ?? ''),
113
                strval($parsed->serverKey ?? ''),
114
                $clientData
115
            );
116
        }
117
    }
118
119
    /**
120
     * @param ResponseInterface $response
121
     * @param string $clientData
122
     * @throws UploadException
123
     * @return Responses\BasicResponse
124
     */
125
    public function upload(ResponseInterface $response, string $clientData): Responses\BasicResponse
126
    {
127
        $parsed = $this->parseResponse($response);
128
        if (Responses\BasicResponse::STATUS_OK == $parsed->status) {
129
            $data = $this->responseFactory->getResponse(Responses\Factory::RESPONSE_UPLOAD);
130
            /** @var Responses\LastKnownResponse $data */
131
            return $data->setLastKnown(
132
                intval(max(0, $parsed->lastKnown ?? 0))
133
            )->setBasics(
134
                strval($parsed->serverKey ?? ''),
135
                $clientData
136
            );
137
        } else {
138
            return $this->responseError(
139
                strval($parsed->message ?? ''),
140
                strval($parsed->serverKey ?? ''),
141
                $clientData
142
            );
143
        }
144
    }
145
146
    /**
147
     * @param ResponseInterface $response
148
     * @param string $clientData
149
     * @throws UploadException
150
     * @return Responses\BasicResponse
151
     */
152
    public function done(ResponseInterface $response, string $clientData): Responses\BasicResponse
153
    {
154
        $parsed = $this->parseResponse($response);
155
        if (Responses\BasicResponse::STATUS_OK == $parsed->status) {
156
            $data = $this->responseFactory->getResponse(Responses\Factory::RESPONSE_DONE);
157
            /** @var Responses\DoneResponse $data */
158
            return $data->setFinalName(
159
                strval($parsed->name ?? '')
160
            )->setBasics(
161
                strval($parsed->serverKey ?? ''),
162
                $clientData
163
            );
164
        } else {
165
            return $this->responseError(
166
                strval($parsed->message ?? ''),
167
                strval($parsed->serverKey ?? ''),
168
                $clientData
169
            );
170
        }
171
    }
172
173
    /**
174
     * @param ResponseInterface $response
175
     * @param string $clientData
176
     * @throws UploadException
177
     * @return Responses\BasicResponse
178
     */
179
    public function cancel(ResponseInterface $response, string $clientData): Responses\BasicResponse
180
    {
181
        $parsed = $this->parseResponse($response);
182
        if (Responses\BasicResponse::STATUS_OK == $parsed->status) {
183
            $data = $this->responseFactory->getResponse(Responses\Factory::RESPONSE_CANCEL);
184
            /** @var Responses\BasicResponse $data */
185
            return $data->setBasics(
186
                strval($parsed->serverKey ?? ''),
187
                $clientData
188
            );
189
        } else {
190
            return $this->responseError(
191
                strval($parsed->message ?? ''),
192
                strval($parsed->serverKey ?? ''),
193
                $clientData
194
            );
195
        }
196
    }
197
198
    /**
199
     * @param string $message
200
     * @param string $serverKey
201
     * @param string $clientData
202
     * @throws UploadException
203
     * @return Responses\BasicResponse
204
     */
205
    protected function responseError(string $message, string $serverKey, string $clientData): Responses\BasicResponse
206
    {
207
        $data = $this->responseFactory->getResponse(Responses\Factory::RESPONSE_ERROR);
208
        /** @var Responses\ErrorResponse $data */
209
        return $data->setErrorMessage($message)->setBasics($serverKey, $clientData);
210
    }
211
212
    /**
213
     * @param ResponseInterface $response
214
     * @throws UploadException
215
     * @return stdClass
216
     */
217
    protected function parseResponse(ResponseInterface $response): stdClass
218
    {
219
        try {
220
            $body = $response->getBody();
221
            $body->rewind();
222
            $parsed = json_decode($body->getContents(), false, 2, JSON_THROW_ON_ERROR);
223
        } catch (JsonException $ex) {
224
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
225
        }
226
        if (!$parsed instanceof stdClass) {
227
            // @codeCoverageIgnoreStart
228
            // this one is on phpstan
229
            throw new UploadException($this->getUppLang()->uppBadResponse(''));
230
        }
231
        // @codeCoverageIgnoreEnd
232
        return $parsed;
233
    }
234
}
235