Uploader   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 29
c 2
b 0
f 0
dl 0
loc 131
ccs 28
cts 28
cp 1
rs 10
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A upload() 0 6 2
A cancel() 0 6 2
A check() 0 6 2
A truncateFrom() 0 6 2
A done() 0 6 2
A __construct() 0 7 1
A init() 0 6 2
1
<?php
2
3
namespace kalanis\UploadPerPartes;
4
5
6
use Psr\Container\ContainerInterface;
7
8
9
/**
10
 * Class Uploader
11
 * @package kalanis\UploadPerPartes
12
 * Main server library for drive upload per-partes
13
 */
14
class Uploader
15
{
16
    protected Interfaces\IOperations $target;
17
    protected Responses\ErrorResponse $errorResponse;
18
19
    /**
20
     * @param ContainerInterface|null $container
21
     * @param array{
22
     *              "calc_size"?: int|object|null,
23
     *              "temp_location"?: string|null,
24
     *              "target_location"?: string|null,
25
     *              "lang"?: string|object|null,
26
     *              "target"?: string|object|null,
27
     *              "data_encoder"?: string|object|null,
28
     *              "data_modifier"?: string|object|null,
29
     *              "key_encoder"?: string|object|null,
30
     *              "key_modifier"?: string|object|null,
31
     *              "driving_file"?: string|int|object|null,
32
     *              "temp_storage"?: string|int|object|null,
33
     *              "temp_encoder"?: string|int|object|null,
34
     *              "final_storage"?: string|int|object|null,
35
     *              "final_encoder"?: string|int|object|null,
36
     *              "checksum"?: string|null,
37
     *              "decoder"?: string|null,
38
     *              "can_continue"?: bool|null,
39
     *             } $params
40
     * @throws UploadException
41
     */
42 12
    public function __construct(
43
        ?ContainerInterface $container = null,
44
        array $params = []
45
    )
46
    {
47 12
        $this->errorResponse = new Responses\ErrorResponse();
48 12
        $this->target = (new Target\Factory(new Uploader\LangFactory(), $container))->getTarget(new Uploader\Config($params));
49 12
    }
50
51
    /**
52
     * Upload file by parts, create driving file
53
     * @param string $targetPath
54
     * @param string $remoteFileName posted file name
55
     * @param int<0, max> $length complete file size
56
     * @param string $clientData stored string from client
57
     * @return Responses\BasicResponse
58
     */
59 2
    public function init(string $targetPath, string $remoteFileName, int $length, string $clientData = '̈́'): Responses\BasicResponse
60
    {
61
        try {
62 2
            return $this->target->init($targetPath, $remoteFileName, $length, $clientData);
63 1
        } catch (UploadException $ex) {
64 1
            return $this->errorResponse->setError($ex)->setBasics('', $clientData);
65
        }
66
    }
67
68
    /**
69
     * Check already uploaded parts
70
     * @param string $serverData
71
     * @param int<0, max> $segment
72
     * @param string $method which method will be used on segment
73
     * @param string $clientData stored string from client
74
     * @return Responses\BasicResponse
75
     */
76 2
    public function check(string $serverData, int $segment, string $method, string $clientData = ''): Responses\BasicResponse
77
    {
78
        try {
79 2
            return $this->target->check($serverData, $segment, $method, $clientData);
80 1
        } catch (UploadException $ex) {
81 1
            return $this->errorResponse->setError($ex)->setBasics($serverData, $clientData);
82
        }
83
    }
84
85
    /**
86
     * Delete problematic segments
87
     * @param string $serverData stored string for server
88
     * @param int<0, max> $segment
89
     * @param string $clientData stored string from client
90
     * @return Responses\BasicResponse
91
     */
92 2
    public function truncateFrom(string $serverData, int $segment, string $clientData = ''): Responses\BasicResponse
93
    {
94
        try {
95 2
            return $this->target->truncate($serverData, $segment, $clientData);
96 1
        } catch (UploadException $ex) {
97 1
            return $this->errorResponse->setError($ex)->setBasics($serverData, $clientData);
98
        }
99
    }
100
101
    /**
102
     * Upload file by parts, use driving file
103
     * @param string $serverData stored string for server
104
     * @param string $content binary content
105
     * @param string $method how is content encoded
106
     * @param string $clientData stored string from client
107
     * @return Responses\BasicResponse
108
     */
109 2
    public function upload(string $serverData, string $content, string $method, string $clientData = ''): Responses\BasicResponse
110
    {
111
        try {
112 2
            return $this->target->upload($serverData, $content, $method, $clientData);
113 1
        } catch (UploadException $ex) {
114 1
            return $this->errorResponse->setError($ex)->setBasics($serverData, $clientData);
115
        }
116
    }
117
118
    /**
119
     * Upload file by parts, final status
120
     * @param string $serverData stored string for server
121
     * @param string $clientData stored string from client
122
     * @return Responses\BasicResponse
123
     */
124 2
    public function done(string $serverData, string $clientData = ''): Responses\BasicResponse
125
    {
126
        try {
127 2
            return $this->target->done($serverData, $clientData);
128 1
        } catch (UploadException $ex) {
129 1
            return $this->errorResponse->setError($ex)->setBasics($serverData, $clientData);
130
        }
131
    }
132
133
    /**
134
     * Upload file by parts, final status
135
     * @param string $serverData stored string for server
136
     * @param string $clientData stored string from client
137
     * @return Responses\BasicResponse
138
     */
139 2
    public function cancel(string $serverData, string $clientData = ''): Responses\BasicResponse
140
    {
141
        try {
142 2
            return $this->target->cancel($serverData, $clientData);
143 1
        } catch (UploadException $ex) {
144 1
            return $this->errorResponse->setError($ex)->setBasics($serverData, $clientData);
145
        }
146
    }
147
}
148