fix: strongly type symbol for market data and quote

This commit is contained in:
hackerESQ
2025-01-28 19:35:15 -06:00
parent d23d28afd8
commit 399858d09b
6 changed files with 38 additions and 16 deletions
@@ -18,7 +18,15 @@ class AlphaVantageMarketData implements MarketDataInterface
public function exists(string $symbol): bool
{
return $this->quote($symbol)->isNotEmpty();
try {
$this->quote($symbol);
return true;
} catch (\Throwable $e) {
return false;
}
}
public function quote(string $symbol): Quote
@@ -26,10 +34,6 @@ class AlphaVantageMarketData implements MarketDataInterface
$quote = Alphavantage::core()->quoteEndpoint($symbol);
$quote = Arr::get($quote, 'Global Quote', []);
if (empty($quote)) {
return new Quote;
}
$fundamental = cache()->remember(
'av-symbol-'.$symbol,
1440,
@@ -20,6 +20,7 @@ class FallbackInterface
$provider = trim($provider);
try {
Log::warning("Calling method {$method} ({$provider})");
if (! in_array($provider, array_keys(config('investbrain.interfaces', [])))) {
@@ -28,17 +28,21 @@ class FinnhubMarketData implements MarketDataInterface
public function exists(string $symbol): bool
{
return $this->quote($symbol)->isNotEmpty();
try {
$this->quote($symbol);
return true;
} catch (\Throwable $e) {
return false;
}
}
public function quote(string $symbol): Quote
{
$quote = $this->client->quote($symbol);
if (empty($quote)) {
return new Quote;
}
$fundamental = cache()->remember(
'fh-symbol-'.$symbol,
1440,
+2 -2
View File
@@ -9,7 +9,7 @@ use Illuminate\Support\Carbon;
class Quote extends MarketDataType
{
public function setName($name): self
public function setName(string $name): self
{
$this->items['name'] = (string) $name;
@@ -21,7 +21,7 @@ class Quote extends MarketDataType
return $this->items['name'] ?? '';
}
public function setSymbol($symbol): self
public function setSymbol(string $symbol): self
{
$this->items['symbol'] = (string) $symbol;
+11 -3
View File
@@ -26,7 +26,15 @@ class YahooMarketData implements MarketDataInterface
public function exists(string $symbol): bool
{
return $this->quote($symbol)->isNotEmpty();
try {
$this->quote($symbol);
return true;
} catch (\Throwable $e) {
return false;
}
}
public function quote(string $symbol): Quote
@@ -34,8 +42,8 @@ class YahooMarketData implements MarketDataInterface
$quote = $this->client->getQuote($symbol);
if (empty($quote)) {
return collect();
if (is_null($quote)) {
throw new \Exception('Symbol ('.$symbol.') does not exist');
}
return new Quote([