1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace rdx\filemanager; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Database\ConnectionInterface; |
7
|
|
|
use rdx\filemanager\ManagedFile; |
8
|
|
|
|
9
|
|
|
class FileManagerDatabaseStorage implements FileManagerStorage { |
10
|
|
|
|
11
|
|
|
protected $connection; |
12
|
|
|
protected $options = []; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
public function __construct(ConnectionInterface $connection, array $options = []) { |
18
|
|
|
$this->connection = $connection; |
19
|
|
|
$this->options = $options; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* |
24
|
|
|
*/ |
25
|
|
|
public function getUsageCount(ManagedFile $file) { |
26
|
|
|
return $this->connection |
27
|
|
|
->table($this->options['usage_table']) |
28
|
|
|
->where('file_id', $file->id) |
29
|
|
|
->count(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* |
34
|
|
|
*/ |
35
|
|
|
public function getUsages(ManagedFile $file) { |
36
|
|
|
return $this->connection |
37
|
|
|
->table($this->options['usage_table']) |
38
|
|
|
->where('file_id', $file->id) |
39
|
|
|
->get(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* |
44
|
|
|
*/ |
45
|
|
|
public function getFiles(array $conditions = [], array $options = []) { |
46
|
|
|
$query = $this->connection->table($this->options['files_table']); |
47
|
|
|
|
48
|
|
|
foreach ($conditions as $column => $value) { |
49
|
|
|
$query->where($column, $value); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $query->get(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* |
57
|
|
|
*/ |
58
|
|
|
public function getFileByPath($path) { |
59
|
|
|
return $this->connection |
60
|
|
|
->table($this->options['files_table']) |
61
|
|
|
->where('filepath', $path) |
62
|
|
|
->first(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* |
67
|
|
|
*/ |
68
|
|
|
public function getFile($id) { |
69
|
|
|
return $this->connection |
70
|
|
|
->table($this->options['files_table']) |
71
|
|
|
->where('id', $id) |
72
|
|
|
->first(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
public function addFile(ManagedFile $file) { |
|
|
|
|
79
|
|
|
$file->id = $this->connection->table($this->options['files_table'])->insertGetId([ |
80
|
|
|
'filepath' => $file->filepath, |
81
|
|
|
'filename' => $file->filename, |
82
|
|
|
'created_at' => new Carbon, |
83
|
|
|
'created_by' => \Auth::id(), |
84
|
|
|
]); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* |
89
|
|
|
*/ |
90
|
|
|
public function removeFile(ManagedFile $file) { |
91
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* |
96
|
|
|
*/ |
97
|
|
View Code Duplication |
public function addUsage(ManagedFile $file, array $params) { |
|
|
|
|
98
|
|
|
$params = [ |
99
|
|
|
'file_id' => $file->id, |
100
|
|
|
'created_at' => new Carbon, |
101
|
|
|
'created_by' => \Auth::id(), |
102
|
|
|
] + $params; |
103
|
|
|
|
104
|
|
|
return $this->connection->table($this->options['usage_table'])->insert($params); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* |
109
|
|
|
*/ |
110
|
|
|
public function removeUsage(ManagedFile $file = null, array $params) { |
111
|
|
|
$query = $this->connection->table($this->options['usage_table']); |
112
|
|
|
|
113
|
|
|
if ($file) { |
114
|
|
|
$query->where('file_id', $file->id); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
foreach ($params as $column => $value) { |
118
|
|
|
$query->where($column, $value); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $query->delete(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.