Skip to content

Commit 8bf6a3d

Browse files
committed
more detailed operation result
1 parent 9e095a6 commit 8bf6a3d

File tree

3 files changed

+54
-12
lines changed

3 files changed

+54
-12
lines changed

Diff for: src/Result.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace PhpRetention;
4+
5+
use DateTimeImmutable;
6+
use JsonSerializable;
7+
use ReflectionClass;
8+
9+
class Result implements JsonSerializable
10+
{
11+
public function __construct(
12+
public readonly array $keepList,
13+
public readonly array $pruneList,
14+
public readonly int $startTime,
15+
public readonly int $endTime
16+
)
17+
{}
18+
19+
public function jsonSerialize(): array
20+
{
21+
$reflection = new ReflectionClass($this);
22+
$props = $reflection->getProperties();
23+
$data = [];
24+
foreach ($props as $prop) {
25+
if ($prop->isPublic()) {
26+
$data[$prop->getName()] = $prop->getValue($this);
27+
}
28+
}
29+
return $data;
30+
}
31+
}

Diff for: src/Retention.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,10 @@ public function setExcludePattern(string $pattern)
161161
* @param string $baseDir
162162
* @return array
163163
*/
164-
public function apply(string $baseDir)
164+
public function apply(string $baseDir): Result
165165
{
166+
$startTime = time();
167+
166168
$files = $this->findFiles($baseDir);
167169
$result = $this->checkPolicy($files);
168170
$keepList = $result['keep'];
@@ -198,7 +200,14 @@ public function apply(string $baseDir)
198200
}
199201
}
200202

201-
return $keepList;
203+
$endTime = time();
204+
205+
return new Result(
206+
keepList: $keepList,
207+
pruneList: $pruneList,
208+
startTime: $startTime,
209+
endTime: $endTime
210+
);
202211
}
203212

204213
/**

Diff for: tests/RetentionTest.php

+12-10
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ public function testPrunePolicy(array $policy, array $expectedKeepList)
142142

143143
/** @var Retention $retention */
144144
$retention->setPolicyConfig($policy);
145-
$actualKeepList = $retention->apply('');
145+
$result = $retention->apply('');
146+
$actualKeepList = $result->keepList;
146147

147148
self::assertSameSize($expectedKeepList, $actualKeepList);
148149

@@ -266,25 +267,25 @@ private function getDummyFileData(): array
266267

267268
public function testGrouping()
268269
{
269-
$testFiles = [
270-
'/backup/tenant/mysql-20240106.tar.gz',
271-
'/backup/tenant/files-20240106.tar.gz',
272-
'/backup/tenant/mysql-20240107.tar.gz',
273-
'/backup/tenant/files-20240107.tar.gz'
274-
];
275270
$expectedKeptFiles = [
276271
'/backup/tenant/mysql-20240107.tar.gz',
277272
'/backup/tenant/files-20240107.tar.gz'
278273
];
279274

280275
$retention = new Retention();
281276
$retention->setPolicyConfig(['keep-last' => 1]);
282-
$retention->setPruneHandler(function (FileInfo $fileInfo) {
277+
$retention->setPruneHandler(function () {
283278
// simulate pruning
284279
return true;
285280
});
286-
$retention->setFindHandler(function (string $targetDir) use ($testFiles) {
281+
$retention->setFindHandler(function () {
287282
$files = [];
283+
$testFiles = [
284+
'/backup/tenant/mysql-20240106.tar.gz',
285+
'/backup/tenant/files-20240106.tar.gz',
286+
'/backup/tenant/mysql-20240107.tar.gz',
287+
'/backup/tenant/files-20240107.tar.gz'
288+
];
288289
foreach ($testFiles as $filepath) {
289290
if (preg_match('/\-([0-9]{4})([0-9]{2})([0-9]{2})/', $filepath, $matches)) {
290291
$year = intval($matches[1]);
@@ -312,7 +313,8 @@ public function testGrouping()
312313

313314
return null;
314315
});
315-
$kept = $retention->apply('/backup/tenant');
316+
$result = $retention->apply('/backup/tenant');
317+
$kept = $result->keepList;
316318

317319
$actualKeptFiles = [];
318320
foreach ($kept as $f) {

0 commit comments

Comments
 (0)