From 399858d09b97272a8c457d043206e7549ba4d3e2 Mon Sep 17 00:00:00 2001 From: hackerESQ Date: Tue, 28 Jan 2025 19:35:15 -0600 Subject: [PATCH] fix: strongly type symbol for market data and quote --- app/Console/Commands/RefreshMarketData.php | 7 ++++++- .../MarketData/AlphaVantageMarketData.php | 14 +++++++++----- app/Interfaces/MarketData/FallbackInterface.php | 1 + app/Interfaces/MarketData/FinnhubMarketData.php | 14 +++++++++----- app/Interfaces/MarketData/Types/Quote.php | 4 ++-- app/Interfaces/MarketData/YahooMarketData.php | 14 +++++++++++--- 6 files changed, 38 insertions(+), 16 deletions(-) diff --git a/app/Console/Commands/RefreshMarketData.php b/app/Console/Commands/RefreshMarketData.php index 68325b1..5518581 100644 --- a/app/Console/Commands/RefreshMarketData.php +++ b/app/Console/Commands/RefreshMarketData.php @@ -7,6 +7,7 @@ namespace App\Console\Commands; use App\Models\Holding; use App\Models\MarketData; use Illuminate\Console\Command; +use Illuminate\Support\Facades\Log; class RefreshMarketData extends Command { @@ -57,7 +58,11 @@ class RefreshMarketData extends Command foreach ($holdings->get() as $holding) { $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().')'); + } } } } diff --git a/app/Interfaces/MarketData/AlphaVantageMarketData.php b/app/Interfaces/MarketData/AlphaVantageMarketData.php index a139a60..524a587 100644 --- a/app/Interfaces/MarketData/AlphaVantageMarketData.php +++ b/app/Interfaces/MarketData/AlphaVantageMarketData.php @@ -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, diff --git a/app/Interfaces/MarketData/FallbackInterface.php b/app/Interfaces/MarketData/FallbackInterface.php index 194758a..ec6bcad 100644 --- a/app/Interfaces/MarketData/FallbackInterface.php +++ b/app/Interfaces/MarketData/FallbackInterface.php @@ -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', [])))) { diff --git a/app/Interfaces/MarketData/FinnhubMarketData.php b/app/Interfaces/MarketData/FinnhubMarketData.php index 8c16554..244fa14 100644 --- a/app/Interfaces/MarketData/FinnhubMarketData.php +++ b/app/Interfaces/MarketData/FinnhubMarketData.php @@ -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, diff --git a/app/Interfaces/MarketData/Types/Quote.php b/app/Interfaces/MarketData/Types/Quote.php index f8db045..ae49470 100644 --- a/app/Interfaces/MarketData/Types/Quote.php +++ b/app/Interfaces/MarketData/Types/Quote.php @@ -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; diff --git a/app/Interfaces/MarketData/YahooMarketData.php b/app/Interfaces/MarketData/YahooMarketData.php index cb3c48f..c9830e1 100644 --- a/app/Interfaces/MarketData/YahooMarketData.php +++ b/app/Interfaces/MarketData/YahooMarketData.php @@ -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([