fix: strongly type symbol for market data and quote
This commit is contained in:
@@ -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().')');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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([
|
||||
|
||||
Reference in New Issue
Block a user