DownloadResult   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 143
c 0
b 0
f 0
rs 10
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A cleanChunks() 0 6 2
A save() 0 10 3
A mergeChunks() 0 7 2
A saveChunk() 0 4 2
A addChunk() 0 18 3
1
<?php
2
3
namespace EasyHttp\Model;
4
5
/**
6
 * DownloadResult class
7
 *
8
 * @link    https://github.com/shahradelahi/easy-http
9
 * @author  Shahrad Elahi (https://github.com/shahradelahi)
10
 * @license https://github.com/shahradelahi/easy-http/blob/master/LICENSE (MIT License)
11
 */
12
class DownloadResult
13
{
14
15
	/**
16
	 * The unique identifier of the download
17
	 *
18
	 * @var string
19
	 */
20
	public string $id;
21
22
	/**
23
	 * The final size of the downloaded file
24
	 *
25
	 * @var int
26
	 */
27
	public int $fileSize;
28
29
	/**
30
	 * The path of the downloaded chunks
31
	 *
32
	 * @var string
33
	 */
34
	public string $chunksPath;
35
36
	/**
37
	 * The size of each chunk
38
	 *
39
	 * @var int
40
	 */
41
	public int $chunkSize;
42
43
	/**
44
	 * The count of chunks
45
	 *
46
	 * @var int
47
	 */
48
	public int $chunks;
49
50
	/**
51
	 * Start time of the download in timestamp.
52
	 *
53
	 * @var int
54
	 */
55
	public int $startTime;
56
57
	/**
58
	 * End time of the download in timestamp.
59
	 *
60
	 * @var int
61
	 */
62
	public int $endTime;
63
64
	/**
65
	 * The downloaded chunks
66
	 *
67
	 * @var array [{id, size, location, elapsedTime}, ...]
68
	 */
69
	public array $downloads;
70
71
	/**
72
	 * Add a chunk to the download result
73
	 *
74
	 * @param string $id The unique identifier of the chunk
75
	 * @param ?string $body The chunk body
76
	 * @param float $elapsedTime in microseconds
77
	 * @return void
78
	 */
79
	public function addChunk(string $id, ?string $body, float $elapsedTime): void
80
	{
81
		$data = [
82
			'id' => $id,
83
			'location' => null,
84
			'size' => 0,
85
			'elapsed_time' => $elapsedTime,
86
			'status' => 'failed',
87
		];
88
		if ($body !== null) {
89
			$save = $this->saveChunk($id, $body);
90
			$data = array_merge($data, [
91
				'location' => $save ?? null,
92
				'size' => strlen($body),
93
				'status' => $save ? 'saved' : 'failed',
94
			]);
95
		}
96
		$this->downloads[] = $data;
97
	}
98
99
	/**
100
	 * Save the chunks to the temp directory
101
	 *
102
	 * @param string $id The unique identifier of the chunk
103
	 * @param string $body The body of the chunk
104
	 * @return string|bool
105
	 */
106
	private function saveChunk(string $id, string $body): string|bool
107
	{
108
		$path = $this->chunksPath . DIRECTORY_SEPARATOR . $id;
109
		return file_put_contents($path, $body) ? $path : false;
110
	}
111
112
	/**
113
	 * Merge the chunks into a single string
114
	 *
115
	 * @return string
116
	 */
117
	public function mergeChunks(): string
118
	{
119
		$result = '';
120
		foreach ($this->downloads as $chunk) {
121
			$result .= file_get_contents($chunk['location']);
122
		}
123
		return $result;
124
	}
125
126
	/**
127
	 * Save the merged chunks to a file
128
	 *
129
	 * @param string $filePath The path/to/file.ext
130
	 * @return bool
131
	 */
132
	public function save(string $filePath): bool
133
	{
134
		$pathInfo = pathinfo($filePath, PATHINFO_DIRNAME);
135
		if (gettype($pathInfo) != "string") $pathInfo = $pathInfo['dirname'];
136
		if (!file_exists($pathInfo)) {
137
			throw new \InvalidArgumentException('The directory does not exist');
138
		}
139
		$result = $this->mergeChunks();
140
		$this->cleanChunks();
141
		return file_put_contents($filePath, $result) !== false;
142
	}
143
144
	/**
145
	 * Clean the directory of the chunks
146
	 *
147
	 * @return void
148
	 */
149
	public function cleanChunks(): void
150
	{
151
		foreach (glob($this->chunksPath . DIRECTORY_SEPARATOR . '*') as $file) {
152
			unlink($file);
153
		}
154
		rmdir($this->chunksPath);
155
	}
156
157
}