1 | <?php |
||
2 | |||
3 | namespace kalanis\UploadPerPartes\Responses; |
||
4 | |||
5 | |||
6 | use kalanis\UploadPerPartes\Traits\TLangInit; |
||
7 | use kalanis\UploadPerPartes\UploadException; |
||
8 | use ReflectionClass; |
||
9 | use ReflectionException; |
||
10 | |||
11 | |||
12 | /** |
||
13 | * Class Factory |
||
14 | * @package kalanis\UploadPerPartes\Responses |
||
15 | * Responses from server to client |
||
16 | */ |
||
17 | class Factory |
||
18 | { |
||
19 | use TLangInit; |
||
20 | |||
21 | public const RESPONSE_INIT = 'init'; |
||
22 | public const RESPONSE_CHECK = 'check'; |
||
23 | public const RESPONSE_TRUNCATE = 'truncate'; |
||
24 | public const RESPONSE_UPLOAD = 'upload'; |
||
25 | public const RESPONSE_DONE = 'done'; |
||
26 | public const RESPONSE_CANCEL = 'cancel'; |
||
27 | public const RESPONSE_ERROR = 'error'; |
||
28 | |||
29 | /** @var array<string, class-string<BasicResponse>> */ |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
30 | protected array $responses = [ |
||
31 | self::RESPONSE_INIT => InitResponse::class, |
||
32 | self::RESPONSE_CHECK => CheckResponse::class, |
||
33 | self::RESPONSE_TRUNCATE => LastKnownResponse::class, |
||
34 | self::RESPONSE_UPLOAD => LastKnownResponse::class, |
||
35 | self::RESPONSE_DONE => DoneResponse::class, |
||
36 | self::RESPONSE_CANCEL => BasicResponse::class, |
||
37 | self::RESPONSE_ERROR => ErrorResponse::class, |
||
38 | ]; |
||
39 | |||
40 | /** |
||
41 | * @param string $type |
||
42 | * @throws UploadException |
||
43 | * @return BasicResponse |
||
44 | */ |
||
45 | 57 | public function getResponse(string $type): BasicResponse |
|
46 | { |
||
47 | try { |
||
48 | 57 | if (isset($this->responses[$type])) { |
|
49 | 57 | $reflection = new ReflectionClass($this->responses[$type]); |
|
50 | 56 | $class = $reflection->newInstance(); |
|
51 | 56 | if ($class instanceof BasicResponse) { |
|
52 | 55 | return $class; |
|
53 | } |
||
54 | } |
||
55 | 1 | throw new UploadException($this->getUppLang()->uppBadResponse($type)); |
|
56 | 2 | } catch (ReflectionException $ex) { |
|
57 | 1 | throw new UploadException($ex->getMessage(), $ex->getCode(), $ex); |
|
58 | } |
||
59 | } |
||
60 | } |
||
61 |