feat: convert result types to enums

This commit is contained in:
Phan An 2024-04-18 14:01:21 +02:00
parent 9d99728ab5
commit 4ecf947cc9
2 changed files with 24 additions and 28 deletions

View file

@ -0,0 +1,10 @@
<?php
namespace App\Enums;
enum ScanResultType: string
{
case SUCCESS = 'Success';
case ERROR = 'Error';
case SKIPPED = 'Skipped';
}

View file

@ -2,52 +2,45 @@
namespace App\Values; namespace App\Values;
use Exception; use App\Enums\ScanResultType;
use Webmozart\Assert\Assert;
final class ScanResult final class ScanResult
{ {
public const TYPE_SUCCESS = 1; private function __construct(
public const TYPE_ERROR = 2; public string $path,
public const TYPE_SKIPPED = 3; private readonly ScanResultType $type,
public ?string $error = null
private function __construct(public string $path, public int $type, public ?string $error = null) ) {
{
Assert::oneOf($type, [
ScanResult::TYPE_SUCCESS,
ScanResult::TYPE_ERROR,
ScanResult::TYPE_SKIPPED,
]);
} }
public static function success(string $path): self public static function success(string $path): self
{ {
return new self($path, self::TYPE_SUCCESS, null); return new self($path, ScanResultType::SUCCESS, null);
} }
public static function skipped(string $path): self public static function skipped(string $path): self
{ {
return new self($path, self::TYPE_SKIPPED, null); return new self($path, ScanResultType::SKIPPED, null);
} }
public static function error(string $path, ?string $error = null): self public static function error(string $path, ?string $error = null): self
{ {
return new self($path, self::TYPE_ERROR, $error); return new self($path, ScanResultType::ERROR, $error);
} }
public function isSuccess(): bool public function isSuccess(): bool
{ {
return $this->type === self::TYPE_SUCCESS; return $this->type === ScanResultType::SUCCESS;
} }
public function isSkipped(): bool public function isSkipped(): bool
{ {
return $this->type === self::TYPE_SKIPPED; return $this->type === ScanResultType::SKIPPED;
} }
public function isError(): bool public function isError(): bool
{ {
return $this->type === self::TYPE_ERROR; return $this->type === ScanResultType::ERROR;
} }
public function isValid(): bool public function isValid(): bool
@ -57,15 +50,8 @@ final class ScanResult
public function __toString(): string public function __toString(): string
{ {
$type = match ($this->type) { $name = $this->type->value . ': ' . $this->path;
self::TYPE_SUCCESS => 'Success',
self::TYPE_ERROR => 'Error',
self::TYPE_SKIPPED => 'Skipped',
default => throw new Exception('Invalid type'),
};
$str = $type . ': ' . $this->path; return $this->isError() ? $name . ' - ' . $this->error : $name;
return $this->isError() ? $str . ' - ' . $this->error : $str;
} }
} }