Repository navigation
Expand file tree
/
Copy pathEfficientFileManagement.php
More file actions
148 lines (121 loc) · 3.79 KB
/
Copy pathEfficientFileManagement.php
File metadata and controls
148 lines (121 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
class EfficientFileManagement
{
/** @var string */
private string $baseFolderPath = '';
/** @var array */
private array $allowedExtensions = [];
/** @var int */
private int $divisionNumber = 0;
public function __construct(string $baseFolderPath, array $allowedExtensions, int $divisionNumber)
{
$this->setBaseFolderPath($baseFolderPath);
$this->allowedExtensions = $allowedExtensions;
$this->setDivisionNumber($divisionNumber);
}
private function setBaseFolderPath(string $baseFolderPath): void
{
if (empty($baseFolderPath)) {
throw new \InvalidArgumentException('The base folder path must be set');
}
$this->baseFolderPath = $baseFolderPath;
}
private function setDivisionNumber(int $divisionNumber): void
{
if ($divisionNumber <= 0) {
throw new \InvalidArgumentException('The division number must be greater than 0');
}
$this->divisionNumber = $divisionNumber;
}
/**
* @param int $id
* @param string $extension
*
* @return string
*/
private function getFolderPath(int $id, string $extension): string
{
return $this->baseFolderPath . $extension . '/' . floor($id / $this->divisionNumber) . '/';
}
/**
* @param int $id
* @param string $extension
*
* @return string|null
*/
private function getFilePath(int $id, string $extension): ?string
{
if (!in_array($extension, $this->allowedExtensions)) {
throw new \InvalidArgumentException('The extension is not allowed');
}
$folderPath = $this->getFolderPath($id, $extension);
$fileName = $id . '.' . $extension;
if (file_exists($folderPath . $fileName)) {
return $folderPath . $fileName;
}
return null;
}
/**
* @param int $id
* @param string $extension
*
* @return string|null
*/
public function getContent(int $id, string $extension): ?string
{
$filePath = $this->getFilePath($id, $extension);
if (empty($filePath)) {
return null;
}
if (!is_readable($filePath)) {
throw new \RuntimeException('The file is not readable');
}
return file_get_contents($filePath);
}
/**
* @param int $id
* @param string $extension
* @param string $content
*
* @return bool
*/
public function saveContent(int $id, string $extension, string $content): bool
{
if (empty($content)) {
throw new \InvalidArgumentException('The content must be set');
}
if (!in_array($extension, $this->allowedExtensions)) {
throw new \InvalidArgumentException('The extension is not allowed');
}
$folderPath = $this->getFolderPath($id, $extension);
$fileName = $id . '.' . $extension;
if (!file_exists($folderPath)) {
if (!is_writable($folderPath)) {
throw new \RuntimeException('The folder is not writable');
}
mkdir($folderPath, 0777, true);
}
$filePath = $folderPath . $fileName;
if (!is_writable($filePath)) {
throw new \RuntimeException('The file is not writable');
}
return file_put_contents($filePath, $content);
}
/**
* @param int $id
* @param string $extension
*
* @return bool
*/
public function deleteContent(int $id, string $extension): bool
{
$filePath = $this->getFilePath($id, $extension);
if (empty($filePath)) {
return false;
}
if (!is_writable($filePath)) {
throw new \RuntimeException('The file is not writable');
}
return unlink($filePath);
}
}