diff --git a/php/src/vaas/Options/ForFileOptions.php b/php/src/vaas/Options/ForFileOptions.php index 2ae37b45..87989f8f 100644 --- a/php/src/vaas/Options/ForFileOptions.php +++ b/php/src/vaas/Options/ForFileOptions.php @@ -4,19 +4,19 @@ class ForFileOptions { - public bool $useCache; - public bool $useHashLookup; - public ?string $vaasRequestId; + const DEFAULT_REQUEST_ID = null; + + public function __construct( + public bool $useCache = true, + public bool $useHashLookup = true, + public ?string $vaasRequestId = self::DEFAULT_REQUEST_ID) {} - public function __construct(array $options = []) + public static function fromVaasOptions(VaasOptions $options): self { - $this->useCache = $options['useCache'] ?? true; - $this->useHashLookup = $options['useHashLookup'] ?? true; - $this->vaasRequestId = $options['vaasRequestId'] ?? null; - } - - public static function default(): self - { - return new self(); + return new self( + $options->useCache, + $options->useHashLookup, + self::DEFAULT_REQUEST_ID + ); } } \ No newline at end of file diff --git a/php/src/vaas/Options/ForSha256Options.php b/php/src/vaas/Options/ForSha256Options.php index 04aea15f..2eb7800a 100644 --- a/php/src/vaas/Options/ForSha256Options.php +++ b/php/src/vaas/Options/ForSha256Options.php @@ -4,19 +4,19 @@ class ForSha256Options { - public bool $useCache; - public bool $useHashLookup; - public ?string $vaasRequestId; + const DEFAULT_REQUEST_ID = null; - public function __construct(array $options = []) - { - $this->useCache = $options['useCache'] ?? true; - $this->useHashLookup = $options['useHashLookup'] ?? true; - $this->vaasRequestId = $options['vaasRequestId'] ?? null; - } + public function __construct( + public bool $useCache = true, + public bool $useHashLookup = true, + public ?string $vaasRequestId = self::DEFAULT_REQUEST_ID) {} - public static function default(): self + public static function fromVaasOptions(VaasOptions $options): self { - return new self(); + return new self( + $options->useCache, + $options->useHashLookup, + self::DEFAULT_REQUEST_ID + ); } } \ No newline at end of file diff --git a/php/src/vaas/Options/ForStreamOptions.php b/php/src/vaas/Options/ForStreamOptions.php index 6fd46a02..06162310 100644 --- a/php/src/vaas/Options/ForStreamOptions.php +++ b/php/src/vaas/Options/ForStreamOptions.php @@ -4,19 +4,20 @@ class ForStreamOptions { - public bool $useHashLookup; - public int $timeout; - public ?string $vaasRequestId; + const DEFAULT_TIMEOUT = 300; + const DEFAULT_REQUEST_ID = null; - public function __construct(array $options = []) - { - $this->useHashLookup = $options['useHashLookup'] ?? true; - $this->timeout = $options['timeout'] ?? 300; - $this->vaasRequestId = $options['vaasRequestId'] ?? null; - } + public function __construct( + public bool $useHashLookup = true, + public int $timeout = self::DEFAULT_TIMEOUT, + public ?string $vaasRequestId = self::DEFAULT_REQUEST_ID) {} - public static function default(): self + public static function fromVaasOptions(VaasOptions $options): self { - return new self(); + return new self( + $options->useCache, + self::DEFAULT_TIMEOUT, + self::DEFAULT_REQUEST_ID + ); } } \ No newline at end of file diff --git a/php/src/vaas/Options/ForUrlOptions.php b/php/src/vaas/Options/ForUrlOptions.php index b7c63f3a..08248cdc 100644 --- a/php/src/vaas/Options/ForUrlOptions.php +++ b/php/src/vaas/Options/ForUrlOptions.php @@ -4,17 +4,17 @@ class ForUrlOptions { - public bool $useHashLookup; - public ?string $vaasRequestId; + const DEFAULT_REQUEST_ID = null; - public function __construct(array $options = []) - { - $this->useHashLookup = $options['useHashLookup'] ?? true; - $this->vaasRequestId = $options['vaasRequestId'] ?? null; - } + public function __construct( + public bool $useHashLookup = true, + public ?string $vaasRequestId = self::DEFAULT_REQUEST_ID) {} - public static function default(): self + public static function fromVaasOptions(VaasOptions $options): self { - return new self(); + return new self( + $options->useHashLookup, + self::DEFAULT_REQUEST_ID + ); } } \ No newline at end of file diff --git a/php/src/vaas/Vaas.php b/php/src/vaas/Vaas.php index e02d8512..3bcf207b 100644 --- a/php/src/vaas/Vaas.php +++ b/php/src/vaas/Vaas.php @@ -81,15 +81,8 @@ public function forSha256Async(Sha256 $sha256, ?ForSha256Options $options = null return async(function () use ($sha256, $options, $cancellation) { $this->logger->debug("Requesting verdict for SHA256: $sha256"); - if ($options === null) { - $options = new ForSha256Options( - [ - 'vaasRequestId' => null, - 'useHashLookup' => $this->options->useHashLookup ?? true, - 'useCache' => $this->options->useCache ?? true, - ] - ); - } + $options = $options ?? ForSha256Options::fromVaasOptions($this->options); + $url = sprintf('%s/files/%s/report/?useCache=%s&useHashLookup=%s', $this->options->vaasUrl, $sha256, @@ -146,22 +139,11 @@ public function forFileAsync(string $path, ?ForFileOptions $options = null, ?Can throw new VaasClientException('File does not exist'); } - if ($options === null) { - $options = new ForFileOptions( - [ - 'vaasRequestId' => null, - 'useHashLookup' => $this->options->useHashLookup ?? true, - 'useCache' => $this->options->useCache ?? true, - ] - ); - } + $options = $options ?? ForFileOptions::fromVaasOptions($this->options); if ($options->useCache || $options->useHashLookup) { - $forSha256Options = new ForSha256Options([ - 'vaasRequestId' => $options->vaasRequestId, - 'useHashLookup' => $options->useHashLookup, - 'useCache' => $options->useCache, - ]); + $forSha256Options = new ForSha256Options( + $options->useCache, $options->useHashLookup, $options->vaasRequestId); $sha256 = Sha256::TryFromFile($path); $this->logger->debug("Check if file $path is already known by its SHA256: $sha256"); $response = $this->forSha256Async($sha256, $forSha256Options, $cancellation)->await(); @@ -174,10 +156,7 @@ public function forFileAsync(string $path, ?ForFileOptions $options = null, ?Can $stream = openFile($path, 'r'); - $forStreamOptions = new ForStreamOptions([ - 'vaasRequestId' => $options->vaasRequestId, - 'useHashLookup' => $options->useHashLookup, - ]); + $forStreamOptions = new ForStreamOptions($options->useHashLookup, 300, $options->vaasRequestId); $this->logger->debug("Requesting verdict for $path as file stream"); return $this->forStreamAsync($stream, filesize($path), $forStreamOptions)->await(); @@ -203,15 +182,8 @@ public function forStreamAsync(ReadableStream $stream, int $fileSize, ?ForStream throw new VaasClientException('Stream is not readable'); } - if ($options === null) { - $options = new ForStreamOptions( - [ - 'vaasRequestId' => null, - 'timeout' => $this->options->timeout, - 'useHashLookup' => $this->options->useHashLookup ?? true, - ] - ); - } + $options = $options ?? ForStreamOptions::fromVaasOptions($this->options); + $url = sprintf('%s/files?useHashLookup=%s', $this->options->vaasUrl, json_encode($options->useHashLookup)); $request = new Request($url, 'POST'); @@ -243,10 +215,7 @@ public function forStreamAsync(ReadableStream $stream, int $fileSize, ?ForStream throw $this->parseVaasError($response); } - $forSha256Options = new ForSha256Options([ - 'vaasRequestId' => $options->vaasRequestId, - 'useHashLookup' => $options->useHashLookup, - ]); + $forSha256Options = new ForSha256Options(true, $options->useHashLookup, $options->vaasRequestId); if (!isset($fileAnalysisStarted['sha256'])) { $this->logger->error("Unexpected response from the server for stream"); @@ -272,14 +241,8 @@ public function forUrlAsync(string $uri, ?ForUrlOptions $options = null, ?Cancel $this->logger->debug("Requesting verdict for URL: $uri"); $uri = Vaas::validUri($uri); - if ($options === null) { - $options = new ForUrlOptions( - [ - 'vaasRequestId' => null, - 'useHashLookup' => $this->options->useHashLookup ?? true, - ] - ); - } + $options = $options ?? ForUrlOptions::fromVaasOptions($this->options); + $urlAnalysisUri = sprintf('%s/urls', $this->options->vaasUrl); $urlAnalysisRequest = new Request($urlAnalysisUri, 'POST');