Skip to content

Commit

Permalink
more detailed operation result
Browse files Browse the repository at this point in the history
  • Loading branch information
ardabeyazoglu committed Feb 6, 2024
1 parent 9e095a6 commit 8bf6a3d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 12 deletions.
31 changes: 31 additions & 0 deletions src/Result.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace PhpRetention;

use DateTimeImmutable;
use JsonSerializable;
use ReflectionClass;

class Result implements JsonSerializable
{
public function __construct(
public readonly array $keepList,
public readonly array $pruneList,
public readonly int $startTime,
public readonly int $endTime
)
{}

public function jsonSerialize(): array
{
$reflection = new ReflectionClass($this);
$props = $reflection->getProperties();
$data = [];
foreach ($props as $prop) {
if ($prop->isPublic()) {
$data[$prop->getName()] = $prop->getValue($this);
}
}
return $data;
}
}
13 changes: 11 additions & 2 deletions src/Retention.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,10 @@ public function setExcludePattern(string $pattern)
* @param string $baseDir
* @return array
*/
public function apply(string $baseDir)
public function apply(string $baseDir): Result
{
$startTime = time();

$files = $this->findFiles($baseDir);
$result = $this->checkPolicy($files);
$keepList = $result['keep'];
Expand Down Expand Up @@ -198,7 +200,14 @@ public function apply(string $baseDir)
}
}

return $keepList;
$endTime = time();

return new Result(
keepList: $keepList,
pruneList: $pruneList,
startTime: $startTime,
endTime: $endTime
);
}

/**
Expand Down
22 changes: 12 additions & 10 deletions tests/RetentionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ public function testPrunePolicy(array $policy, array $expectedKeepList)

/** @var Retention $retention */
$retention->setPolicyConfig($policy);
$actualKeepList = $retention->apply('');
$result = $retention->apply('');
$actualKeepList = $result->keepList;

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

Expand Down Expand Up @@ -266,25 +267,25 @@ private function getDummyFileData(): array

public function testGrouping()
{
$testFiles = [
'/backup/tenant/mysql-20240106.tar.gz',
'/backup/tenant/files-20240106.tar.gz',
'/backup/tenant/mysql-20240107.tar.gz',
'/backup/tenant/files-20240107.tar.gz'
];
$expectedKeptFiles = [
'/backup/tenant/mysql-20240107.tar.gz',
'/backup/tenant/files-20240107.tar.gz'
];

$retention = new Retention();
$retention->setPolicyConfig(['keep-last' => 1]);
$retention->setPruneHandler(function (FileInfo $fileInfo) {
$retention->setPruneHandler(function () {
// simulate pruning
return true;
});
$retention->setFindHandler(function (string $targetDir) use ($testFiles) {
$retention->setFindHandler(function () {
$files = [];
$testFiles = [
'/backup/tenant/mysql-20240106.tar.gz',
'/backup/tenant/files-20240106.tar.gz',
'/backup/tenant/mysql-20240107.tar.gz',
'/backup/tenant/files-20240107.tar.gz'
];
foreach ($testFiles as $filepath) {
if (preg_match('/\-([0-9]{4})([0-9]{2})([0-9]{2})/', $filepath, $matches)) {
$year = intval($matches[1]);
Expand Down Expand Up @@ -312,7 +313,8 @@ public function testGrouping()

return null;
});
$kept = $retention->apply('/backup/tenant');
$result = $retention->apply('/backup/tenant');
$kept = $result->keepList;

$actualKeptFiles = [];
foreach ($kept as $f) {
Expand Down

0 comments on commit 8bf6a3d

Please sign in to comment.