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