Skip to content

Commit

Permalink
add TimestampPattern
Browse files Browse the repository at this point in the history
  • Loading branch information
KurtThiemann committed Sep 27, 2024
1 parent 387fc92 commit 13b76ed
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 19 deletions.
41 changes: 22 additions & 19 deletions src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,29 @@ class Helper
* @param string $src
* @param string $dst
*/
static function copyDirectory(
string $src,
string $dst
): void {
static function copyDirectory(string $src, string $dst): void
{
if (is_link($src) || is_link($dst)) {
return;
}

$dir = opendir($src);
mkdir($dst, 0777, true);
mkdir($dst, recursive: true);
while (($file = readdir($dir)) !== false) {
if (
$file === static::CURRENT_DIRECTORY
|| $file === static::PARENT_DIRECTORY
) {
if ($file === static::CURRENT_DIRECTORY || $file === static::PARENT_DIRECTORY) {
continue;
}

$newSrc = $src . DIRECTORY_SEPARATOR . $file;
$newDst = $dst . DIRECTORY_SEPARATOR . $file;

if (is_dir($src . DIRECTORY_SEPARATOR . $file)) {
static::copyDirectory(
$src . DIRECTORY_SEPARATOR . $file,
$dst . DIRECTORY_SEPARATOR . $file
);
static::copyDirectory($newSrc, $newDst);
} else {
copy(
$src . DIRECTORY_SEPARATOR . $file,
$dst . DIRECTORY_SEPARATOR . $file
);
if (is_link($newSrc) || is_link($newDst)) {
continue;
}
copy($newSrc, $newDst);
}
}
closedir($dir);
Expand All @@ -53,20 +52,24 @@ static function copyDirectory(
*
* @param string $path
*/
static function removeDirectory(string $path)
static function removeDirectory(string $path): void
{
if (substr($path, -1) !== DIRECTORY_SEPARATOR) {
$path .= DIRECTORY_SEPARATOR;
}

if (!is_dir($path) || is_link($path)) {
return;
}

$directory = dir($path);
while ($file = $directory->read()) {
if (in_array($file, [static::CURRENT_DIRECTORY, static::PARENT_DIRECTORY])) {
continue;
}

$filePath = $path . $file;
if (is_dir($filePath)) {
if (is_dir($filePath) && !is_link($filePath)) {
static::removeDirectory($filePath);
} else {
unlink($filePath);
Expand Down
64 changes: 64 additions & 0 deletions src/Pattern/TimestampPattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Aternos\Thanos\Pattern;

use Aternos\Thanos\Chunk\ChunkInterface;

class TimestampPattern implements ChunkPatternInterface
{
/**
* @param int $minTimestamp
* @param bool $removeUnknownChunks
*/
public function __construct(
protected int $minTimestamp,
protected bool $removeUnknownChunks,
)
{
}

/**
* @inheritDoc
*/
public function matches(ChunkInterface $chunk): bool
{
$time = $chunk->getTimestamp();
return $time > $this->minTimestamp || ($time === -1 && !$this->removeUnknownChunks);
}

/**
* @return int
*/
public function getMinTimestamp(): int
{
return $this->minTimestamp;
}

/**
* @param int $minTimestamp
* @return $this
*/
public function setMinTimestamp(int $minTimestamp): TimestampPattern
{
$this->minTimestamp = $minTimestamp;
return $this;
}

/**
* @return bool
*/
public function getRemoveUnknownChunks(): bool
{
return $this->removeUnknownChunks;
}

/**
* @param bool $removeUnknownChunks
* @return $this
*/
public function setRemoveUnknownChunks(bool $removeUnknownChunks): TimestampPattern
{
$this->removeUnknownChunks = $removeUnknownChunks;
return $this;
}
}

0 comments on commit 13b76ed

Please sign in to comment.