koel/app/Values/ScanResult.php

58 lines
1.2 KiB
PHP
Raw Normal View History

2022-07-29 10:51:20 +00:00
<?php
namespace App\Values;
2022-07-29 10:51:20 +00:00
2024-04-18 12:01:21 +00:00
use App\Enums\ScanResultType;
2022-07-29 10:51:20 +00:00
final class ScanResult
2022-07-29 10:51:20 +00:00
{
2024-04-18 12:01:21 +00:00
private function __construct(
public string $path,
private readonly ScanResultType $type,
public ?string $error = null
) {
2022-07-29 10:51:20 +00:00
}
public static function success(string $path): self
{
2024-04-18 12:01:21 +00:00
return new self($path, ScanResultType::SUCCESS, null);
2022-07-29 10:51:20 +00:00
}
public static function skipped(string $path): self
{
2024-04-18 12:01:21 +00:00
return new self($path, ScanResultType::SKIPPED, null);
2022-07-29 10:51:20 +00:00
}
public static function error(string $path, ?string $error = null): self
2022-07-29 10:51:20 +00:00
{
2024-04-18 12:01:21 +00:00
return new self($path, ScanResultType::ERROR, $error);
2022-07-29 10:51:20 +00:00
}
public function isSuccess(): bool
{
2024-04-18 12:01:21 +00:00
return $this->type === ScanResultType::SUCCESS;
2022-07-29 10:51:20 +00:00
}
public function isSkipped(): bool
{
2024-04-18 12:01:21 +00:00
return $this->type === ScanResultType::SKIPPED;
2022-07-29 10:51:20 +00:00
}
public function isError(): bool
{
2024-04-18 12:01:21 +00:00
return $this->type === ScanResultType::ERROR;
2022-07-29 10:51:20 +00:00
}
public function isValid(): bool
{
return $this->isSuccess() || $this->isSkipped();
}
public function __toString(): string
{
2024-04-18 12:01:21 +00:00
$name = $this->type->value . ': ' . $this->path;
2024-04-18 12:01:21 +00:00
return $this->isError() ? $name . ' - ' . $this->error : $name;
}
2022-07-29 10:51:20 +00:00
}