2022-07-29 10:51:20 +00:00
|
|
|
<?php
|
|
|
|
|
2023-08-20 15:24:56 +00:00
|
|
|
namespace App\Values;
|
2022-07-29 10:51:20 +00:00
|
|
|
|
2023-12-25 17:15:49 +00:00
|
|
|
use Exception;
|
2022-07-29 10:51:20 +00:00
|
|
|
use Webmozart\Assert\Assert;
|
|
|
|
|
2024-01-04 21:51:32 +00:00
|
|
|
final class ScanResult
|
2022-07-29 10:51:20 +00:00
|
|
|
{
|
|
|
|
public const TYPE_SUCCESS = 1;
|
|
|
|
public const TYPE_ERROR = 2;
|
|
|
|
public const TYPE_SKIPPED = 3;
|
|
|
|
|
2023-12-25 17:15:49 +00:00
|
|
|
private function __construct(public string $path, public int $type, public ?string $error = null)
|
2022-07-29 10:51:20 +00:00
|
|
|
{
|
|
|
|
Assert::oneOf($type, [
|
2024-01-04 21:51:32 +00:00
|
|
|
ScanResult::TYPE_SUCCESS,
|
|
|
|
ScanResult::TYPE_ERROR,
|
|
|
|
ScanResult::TYPE_SKIPPED,
|
2022-07-29 10:51:20 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function success(string $path): self
|
|
|
|
{
|
|
|
|
return new self($path, self::TYPE_SUCCESS, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function skipped(string $path): self
|
|
|
|
{
|
|
|
|
return new self($path, self::TYPE_SKIPPED, null);
|
|
|
|
}
|
|
|
|
|
2023-12-25 17:15:49 +00:00
|
|
|
public static function error(string $path, ?string $error = null): self
|
2022-07-29 10:51:20 +00:00
|
|
|
{
|
|
|
|
return new self($path, self::TYPE_ERROR, $error);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isSuccess(): bool
|
|
|
|
{
|
|
|
|
return $this->type === self::TYPE_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isSkipped(): bool
|
|
|
|
{
|
|
|
|
return $this->type === self::TYPE_SKIPPED;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isError(): bool
|
|
|
|
{
|
|
|
|
return $this->type === self::TYPE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isValid(): bool
|
|
|
|
{
|
|
|
|
return $this->isSuccess() || $this->isSkipped();
|
|
|
|
}
|
2023-12-25 17:15:49 +00:00
|
|
|
|
|
|
|
public function __toString(): string
|
|
|
|
{
|
|
|
|
$type = match ($this->type) {
|
|
|
|
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() ? $str . ' - ' . $this->error : $str;
|
|
|
|
}
|
2022-07-29 10:51:20 +00:00
|
|
|
}
|