From 8ad14c862ea050bfe6a8cff9e4f0bed02db3a031 Mon Sep 17 00:00:00 2001 From: PT-ATA No One Date: Wed, 18 Dec 2024 09:05:50 +0000 Subject: [PATCH 1/2] refactors the forXXX-options --- php/src/vaas/Options/ForFileOptions.php | 14 ++---- php/src/vaas/Options/ForSha256Options.php | 14 ++---- php/src/vaas/Options/ForStreamOptions.php | 14 ++---- php/src/vaas/Options/ForUrlOptions.php | 11 ++--- php/src/vaas/Vaas.php | 60 +++++------------------ 5 files changed, 27 insertions(+), 86 deletions(-) diff --git a/php/src/vaas/Options/ForFileOptions.php b/php/src/vaas/Options/ForFileOptions.php index 2ae37b45..79ea6dff 100644 --- a/php/src/vaas/Options/ForFileOptions.php +++ b/php/src/vaas/Options/ForFileOptions.php @@ -4,16 +4,10 @@ class ForFileOptions { - public bool $useCache; - public bool $useHashLookup; - public ?string $vaasRequestId; - - 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 = null) {} public static function default(): self { diff --git a/php/src/vaas/Options/ForSha256Options.php b/php/src/vaas/Options/ForSha256Options.php index 04aea15f..4b6c7725 100644 --- a/php/src/vaas/Options/ForSha256Options.php +++ b/php/src/vaas/Options/ForSha256Options.php @@ -4,16 +4,10 @@ class ForSha256Options { - public bool $useCache; - public bool $useHashLookup; - public ?string $vaasRequestId; - - 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 = null) {} public static function default(): self { diff --git a/php/src/vaas/Options/ForStreamOptions.php b/php/src/vaas/Options/ForStreamOptions.php index 6fd46a02..210242df 100644 --- a/php/src/vaas/Options/ForStreamOptions.php +++ b/php/src/vaas/Options/ForStreamOptions.php @@ -4,16 +4,10 @@ class ForStreamOptions { - public bool $useHashLookup; - public int $timeout; - public ?string $vaasRequestId; - - 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 = 300, + public ?string $vaasRequestId = null) {} public static function default(): self { diff --git a/php/src/vaas/Options/ForUrlOptions.php b/php/src/vaas/Options/ForUrlOptions.php index b7c63f3a..4779eb91 100644 --- a/php/src/vaas/Options/ForUrlOptions.php +++ b/php/src/vaas/Options/ForUrlOptions.php @@ -4,14 +4,9 @@ class ForUrlOptions { - public bool $useHashLookup; - public ?string $vaasRequestId; - - 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 = null) {} public static function default(): self { diff --git a/php/src/vaas/Vaas.php b/php/src/vaas/Vaas.php index e02d8512..94fb4aee 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::default(); + $url = sprintf('%s/files/%s/report/?useCache=%s&useHashLookup=%s', $this->options->vaasUrl, $sha256, @@ -146,22 +139,12 @@ 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::default(); + 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 +157,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 +183,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::default(); + $url = sprintf('%s/files?useHashLookup=%s', $this->options->vaasUrl, json_encode($options->useHashLookup)); $request = new Request($url, 'POST'); @@ -243,10 +216,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 +242,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::default(); + $urlAnalysisUri = sprintf('%s/urls', $this->options->vaasUrl); $urlAnalysisRequest = new Request($urlAnalysisUri, 'POST'); From 926e18e715c2ab3134ce01c2595c99ee5f081211 Mon Sep 17 00:00:00 2001 From: PT-ATA No One Date: Wed, 18 Dec 2024 15:41:36 +0000 Subject: [PATCH 2/2] refactors the options * adds fromVaasOptions function for the forXXXOptions * removes the default() function as it is not used anywhere --- php/src/vaas/Options/ForFileOptions.php | 12 +++++++++--- php/src/vaas/Options/ForSha256Options.php | 12 +++++++++--- php/src/vaas/Options/ForStreamOptions.php | 15 +++++++++++---- php/src/vaas/Options/ForUrlOptions.php | 11 ++++++++--- php/src/vaas/Vaas.php | 9 ++++----- 5 files changed, 41 insertions(+), 18 deletions(-) diff --git a/php/src/vaas/Options/ForFileOptions.php b/php/src/vaas/Options/ForFileOptions.php index 79ea6dff..87989f8f 100644 --- a/php/src/vaas/Options/ForFileOptions.php +++ b/php/src/vaas/Options/ForFileOptions.php @@ -4,13 +4,19 @@ class ForFileOptions { + const DEFAULT_REQUEST_ID = null; + public function __construct( public bool $useCache = true, public bool $useHashLookup = true, - public ?string $vaasRequestId = null) {} + 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/ForSha256Options.php b/php/src/vaas/Options/ForSha256Options.php index 4b6c7725..2eb7800a 100644 --- a/php/src/vaas/Options/ForSha256Options.php +++ b/php/src/vaas/Options/ForSha256Options.php @@ -4,13 +4,19 @@ class ForSha256Options { + const DEFAULT_REQUEST_ID = null; + public function __construct( public bool $useCache = true, public bool $useHashLookup = true, - public ?string $vaasRequestId = null) {} + 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 210242df..06162310 100644 --- a/php/src/vaas/Options/ForStreamOptions.php +++ b/php/src/vaas/Options/ForStreamOptions.php @@ -4,13 +4,20 @@ class ForStreamOptions { + const DEFAULT_TIMEOUT = 300; + const DEFAULT_REQUEST_ID = null; + public function __construct( public bool $useHashLookup = true, - public int $timeout = 300, - public ?string $vaasRequestId = null) {} + 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 4779eb91..08248cdc 100644 --- a/php/src/vaas/Options/ForUrlOptions.php +++ b/php/src/vaas/Options/ForUrlOptions.php @@ -4,12 +4,17 @@ class ForUrlOptions { + const DEFAULT_REQUEST_ID = null; + public function __construct( public bool $useHashLookup = true, - public ?string $vaasRequestId = null) {} + 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 94fb4aee..3bcf207b 100644 --- a/php/src/vaas/Vaas.php +++ b/php/src/vaas/Vaas.php @@ -81,7 +81,7 @@ public function forSha256Async(Sha256 $sha256, ?ForSha256Options $options = null return async(function () use ($sha256, $options, $cancellation) { $this->logger->debug("Requesting verdict for SHA256: $sha256"); - $options = $options ?? ForSha256Options::default(); + $options = $options ?? ForSha256Options::fromVaasOptions($this->options); $url = sprintf('%s/files/%s/report/?useCache=%s&useHashLookup=%s', $this->options->vaasUrl, @@ -139,8 +139,7 @@ public function forFileAsync(string $path, ?ForFileOptions $options = null, ?Can throw new VaasClientException('File does not exist'); } - $options = $options ?? ForFileOptions::default(); - + $options = $options ?? ForFileOptions::fromVaasOptions($this->options); if ($options->useCache || $options->useHashLookup) { $forSha256Options = new ForSha256Options( @@ -183,7 +182,7 @@ public function forStreamAsync(ReadableStream $stream, int $fileSize, ?ForStream throw new VaasClientException('Stream is not readable'); } - $options = $options ?? ForStreamOptions::default(); + $options = $options ?? ForStreamOptions::fromVaasOptions($this->options); $url = sprintf('%s/files?useHashLookup=%s', $this->options->vaasUrl, json_encode($options->useHashLookup)); @@ -242,7 +241,7 @@ public function forUrlAsync(string $uri, ?ForUrlOptions $options = null, ?Cancel $this->logger->debug("Requesting verdict for URL: $uri"); $uri = Vaas::validUri($uri); - $options = $options ?? ForUrlOptions::default(); + $options = $options ?? ForUrlOptions::fromVaasOptions($this->options); $urlAnalysisUri = sprintf('%s/urls', $this->options->vaasUrl);