@@ -12,21 +12,58 @@ class RmQ
12
12
{
13
13
private string $ instance ;
14
14
15
+ private bool $ useArray = false ;
16
+
17
+ /** @var string[] */
18
+ private array $ store = [];
19
+
15
20
public function __construct ()
16
21
{
17
22
$ this ->instance = (new Ulid )->toRfc4122 ();
18
23
}
19
24
25
+ public function useArray (): static
26
+ {
27
+ $ this ->useArray = true ;
28
+
29
+ return $ this ;
30
+ }
31
+
20
32
/** @param string[]|string $paths */
21
33
public function stage (array |string $ paths ): void
22
34
{
23
- // TODO: take query builder with one selected column
35
+ // TODO: take query builder with one selected column => force stageInDb
24
36
// ? validate not empty or exists?
25
- // ? Stage in array and persist by the end of the process. The middleware can parametrize the singleton
26
37
38
+ $ this ->useArray ?
39
+ $ this ->stageInArray ($ paths ) :
40
+ $ this ->stageInDb ($ paths );
41
+ }
42
+
43
+ /** @param string[]|string $paths */
44
+ private function stageInArray (array |string $ paths ): void
45
+ {
46
+ if (is_string ($ paths )) {
47
+ $ this ->store [] = $ paths ;
48
+
49
+ return ;
50
+ }
51
+
52
+ /** @var string[] */
53
+ $ newPaths = collect ($ paths )
54
+ ->filter (fn (mixed $ path ) => is_string ($ path ))
55
+ ->toArray ();
56
+
57
+ $ this ->store = array_merge ($ this ->store , $ newPaths );
58
+ }
59
+
60
+ /** @param string[]|string $paths */
61
+ private function stageInDb (array |string $ paths ): void
62
+ {
27
63
$ data = match (true ) {
28
64
is_string ($ paths ) => $ this ->pathToRecord ($ paths ),
29
65
is_array ($ paths ) => collect ($ paths )
66
+ ->filter (fn (mixed $ path ) => is_string ($ path ))
30
67
->map (fn (string $ path ) => $ this ->pathToRecord ($ path ))
31
68
->toArray (),
32
69
};
@@ -36,7 +73,9 @@ public function stage(array|string $paths): void
36
73
37
74
public function delete (): void
38
75
{
39
- $ this ->performDelete (true );
76
+ $ this ->useArray ?
77
+ $ this ->performDeleteUsingArray () :
78
+ $ this ->performDeleteUsingDb (true );
40
79
}
41
80
42
81
public function deleteAll (): void
@@ -47,19 +86,20 @@ public function deleteAll(): void
47
86
throw new TypeError ('rm-q.after must be an integer ' );
48
87
}
49
88
50
- $ this ->performDelete (false , $ after );
89
+ $ this ->performDeleteUsingDb (false , $ after );
51
90
}
52
91
53
92
/** @return array{path: string, instance: string} */
54
- private function pathToRecord (string $ path ): array
93
+ private function pathToRecord (string $ path, int $ status = RmqFile:: STAGED ): array
55
94
{
56
95
return [
57
96
'path ' => $ path ,
58
97
'instance ' => $ this ->instance ,
98
+ 'status ' => $ status ,
59
99
];
60
100
}
61
101
62
- private function performDelete (bool $ filterInstance = false , int $ beforeSeconds = 0 ): void
102
+ private function performDeleteUsingDb (bool $ filterInstance = false , int $ beforeSeconds = 0 ): void
63
103
{
64
104
$ now = Date::now ();
65
105
@@ -92,4 +132,38 @@ private function performDelete(bool $filterInstance = false, int $beforeSeconds
92
132
]);
93
133
}
94
134
}
135
+
136
+ private function performDeleteUsingArray (): void
137
+ {
138
+ $ now = Date::now ();
139
+
140
+ $ data = [];
141
+
142
+ foreach ($ this ->store as $ path ) {
143
+ if (@unlink ($ path )) {
144
+ $ data [] = [
145
+ 'path ' => $ path ,
146
+ 'instance ' => $ this ->instance ,
147
+ 'status ' => RmqFile::DELETED ,
148
+ 'processed_at ' => $ now ,
149
+ 'deleted_at ' => $ now ,
150
+ ];
151
+ } else {
152
+ $ data [] = [
153
+ 'path ' => $ path ,
154
+ 'instance ' => $ this ->instance ,
155
+ 'status ' => RmqFile::FAILED ,
156
+ 'processed_at ' => $ now ,
157
+ ];
158
+ }
159
+ }
160
+
161
+ RmqFile::insert ($ data );
162
+ }
163
+
164
+ /** @return string[] */
165
+ public function getStore (): array
166
+ {
167
+ return $ this ->store ;
168
+ }
95
169
}
0 commit comments