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
+6 -1
View File
@@ -7,6 +7,7 @@ namespace App\Console\Commands;
use App\Models\Holding; use App\Models\Holding;
use App\Models\MarketData; use App\Models\MarketData;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
class RefreshMarketData extends Command class RefreshMarketData extends Command
{ {
@@ -57,7 +58,11 @@ class RefreshMarketData extends Command
foreach ($holdings->get() as $holding) { foreach ($holdings->get() as $holding) {
$this->line('Refreshing '.$holding->symbol); $this->line('Refreshing '.$holding->symbol);
MarketData::getMarketData($holding->symbol, $force); try {
MarketData::getMarketData($holding->symbol, $force);
} catch (\Throwable $e) {
Log::error('Could not refresh '.$holding->symbol.' ('.$e->getMessage().')');
}
} }
} }
} }
@@ -18,7 +18,15 @@ class AlphaVantageMarketData implements MarketDataInterface
public function exists(string $symbol): bool 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 public function quote(string $symbol): Quote
@@ -26,10 +34,6 @@ class AlphaVantageMarketData implements MarketDataInterface
$quote = Alphavantage::core()->quoteEndpoint($symbol); $quote = Alphavantage::core()->quoteEndpoint($symbol);
$quote = Arr::get($quote, 'Global Quote', []); $quote = Arr::get($quote, 'Global Quote', []);
if (empty($quote)) {
return new Quote;
}
$fundamental = cache()->remember( $fundamental = cache()->remember(
'av-symbol-'.$symbol, 'av-symbol-'.$symbol,
1440, 1440,
@@ -20,6 +20,7 @@ class FallbackInterface
$provider = trim($provider); $provider = trim($provider);
try { try {
Log::warning("Calling method {$method} ({$provider})");
if (! in_array($provider, array_keys(config('investbrain.interfaces', [])))) { if (! in_array($provider, array_keys(config('investbrain.interfaces', [])))) {
@@ -28,17 +28,21 @@ class FinnhubMarketData implements MarketDataInterface
public function exists(string $symbol): bool 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 public function quote(string $symbol): Quote
{ {
$quote = $this->client->quote($symbol); $quote = $this->client->quote($symbol);
if (empty($quote)) {
return new Quote;
}
$fundamental = cache()->remember( $fundamental = cache()->remember(
'fh-symbol-'.$symbol, 'fh-symbol-'.$symbol,
1440, 1440,
+2 -2
View File
@@ -9,7 +9,7 @@ use Illuminate\Support\Carbon;
class Quote extends MarketDataType class Quote extends MarketDataType
{ {
public function setName($name): self public function setName(string $name): self
{ {
$this->items['name'] = (string) $name; $this->items['name'] = (string) $name;
@@ -21,7 +21,7 @@ class Quote extends MarketDataType
return $this->items['name'] ?? ''; return $this->items['name'] ?? '';
} }
public function setSymbol($symbol): self public function setSymbol(string $symbol): self
{ {
$this->items['symbol'] = (string) $symbol; $this->items['symbol'] = (string) $symbol;
+11 -3
View File
@@ -26,7 +26,15 @@ class YahooMarketData implements MarketDataInterface
public function exists(string $symbol): bool 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 public function quote(string $symbol): Quote
@@ -34,8 +42,8 @@ class YahooMarketData implements MarketDataInterface
$quote = $this->client->getQuote($symbol); $quote = $this->client->getQuote($symbol);
if (empty($quote)) { if (is_null($quote)) {
return collect(); throw new \Exception('Symbol ('.$symbol.') does not exist');
} }
return new Quote([ return new Quote([