Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactors the forXXX-options #682

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions php/src/vaas/Options/ForFileOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
14 changes: 4 additions & 10 deletions php/src/vaas/Options/ForSha256Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
lennartdohmann marked this conversation as resolved.
Show resolved Hide resolved

public static function default(): self
{
Expand Down
14 changes: 4 additions & 10 deletions php/src/vaas/Options/ForStreamOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
11 changes: 3 additions & 8 deletions php/src/vaas/Options/ForUrlOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
60 changes: 12 additions & 48 deletions php/src/vaas/Vaas.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
lennartdohmann marked this conversation as resolved.
Show resolved Hide resolved

$url = sprintf('%s/files/%s/report/?useCache=%s&useHashLookup=%s',
$this->options->vaasUrl,
$sha256,
Expand Down Expand Up @@ -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();
lennartdohmann marked this conversation as resolved.
Show resolved Hide resolved


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();
Expand All @@ -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();
Expand All @@ -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();
lennartdohmann marked this conversation as resolved.
Show resolved Hide resolved

$url = sprintf('%s/files?useHashLookup=%s', $this->options->vaasUrl, json_encode($options->useHashLookup));

$request = new Request($url, 'POST');
Expand Down Expand Up @@ -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");
Expand All @@ -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();
lennartdohmann marked this conversation as resolved.
Show resolved Hide resolved

$urlAnalysisUri = sprintf('%s/urls', $this->options->vaasUrl);

$urlAnalysisRequest = new Request($urlAnalysisUri, 'POST');
Expand Down