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 all commits
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
24 changes: 12 additions & 12 deletions php/src/vaas/Options/ForFileOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
22 changes: 11 additions & 11 deletions php/src/vaas/Options/ForSha256Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
23 changes: 12 additions & 11 deletions php/src/vaas/Options/ForStreamOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
18 changes: 9 additions & 9 deletions php/src/vaas/Options/ForUrlOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
59 changes: 11 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::fromVaasOptions($this->options);

$url = sprintf('%s/files/%s/report/?useCache=%s&useHashLookup=%s',
$this->options->vaasUrl,
$sha256,
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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');
Expand Down Expand Up @@ -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");
Expand All @@ -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');
Expand Down