fix: upgrade the exists() market data provider method
This commit is contained in:
@@ -18,15 +18,7 @@ class AlphaVantageMarketData implements MarketDataInterface
|
||||
public function exists(string $symbol): bool
|
||||
{
|
||||
|
||||
try {
|
||||
$this->quote($symbol);
|
||||
|
||||
return true;
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
|
||||
return false;
|
||||
}
|
||||
return (bool) $this->quote($symbol);
|
||||
}
|
||||
|
||||
public function quote(string $symbol): Quote
|
||||
|
||||
@@ -39,6 +39,13 @@ class FallbackInterface
|
||||
}
|
||||
}
|
||||
|
||||
// don't need to throw error if calling exists
|
||||
if ($method == 'exists') {
|
||||
|
||||
// symbol prob just doesn't exist
|
||||
return false;
|
||||
}
|
||||
|
||||
throw new \Exception("Could not get market data: {$this->latest_error}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,15 +28,7 @@ class FinnhubMarketData implements MarketDataInterface
|
||||
public function exists(string $symbol): bool
|
||||
{
|
||||
|
||||
try {
|
||||
$this->quote($symbol);
|
||||
|
||||
return true;
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
|
||||
return false;
|
||||
}
|
||||
return (bool) $this->quote($symbol);
|
||||
}
|
||||
|
||||
public function quote(string $symbol): Quote
|
||||
|
||||
@@ -26,15 +26,7 @@ class YahooMarketData implements MarketDataInterface
|
||||
public function exists(string $symbol): bool
|
||||
{
|
||||
|
||||
try {
|
||||
$this->quote($symbol);
|
||||
|
||||
return true;
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
|
||||
return false;
|
||||
}
|
||||
return (bool) $this->quote($symbol);
|
||||
}
|
||||
|
||||
public function quote(string $symbol): Quote
|
||||
@@ -42,22 +34,18 @@ class YahooMarketData implements MarketDataInterface
|
||||
|
||||
$quote = $this->client->getQuote($symbol);
|
||||
|
||||
if (is_null($quote)) {
|
||||
throw new \Exception('Symbol ('.$symbol.') does not exist');
|
||||
}
|
||||
|
||||
return new Quote([
|
||||
'name' => $quote->getLongName() ?? $quote->getShortName(),
|
||||
'name' => $quote?->getLongName() ?? $quote?->getShortName(),
|
||||
'symbol' => $symbol,
|
||||
'market_value' => $quote->getRegularMarketPrice(),
|
||||
'fifty_two_week_high' => $quote->getFiftyTwoWeekHigh(),
|
||||
'fifty_two_week_low' => $quote->getFiftyTwoWeekLow(),
|
||||
'forward_pe' => $quote->getForwardPE(),
|
||||
'trailing_pe' => $quote->getTrailingPE(),
|
||||
'market_cap' => $quote->getMarketCap(),
|
||||
'book_value' => $quote->getBookValue(),
|
||||
'last_dividend_date' => $quote->getDividendDate(),
|
||||
'dividend_yield' => $quote->getTrailingAnnualDividendYield() * 100,
|
||||
'market_value' => $quote?->getRegularMarketPrice(),
|
||||
'fifty_two_week_high' => $quote?->getFiftyTwoWeekHigh(),
|
||||
'fifty_two_week_low' => $quote?->getFiftyTwoWeekLow(),
|
||||
'forward_pe' => $quote?->getForwardPE(),
|
||||
'trailing_pe' => $quote?->getTrailingPE(),
|
||||
'market_cap' => $quote?->getMarketCap(),
|
||||
'book_value' => $quote?->getBookValue(),
|
||||
'last_dividend_date' => $quote?->getDividendDate(),
|
||||
'dividend_yield' => $quote?->getTrailingAnnualDividendYield() * 100,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -143,6 +143,7 @@ class Transaction extends Model
|
||||
|
||||
public function refreshMarketData(): void
|
||||
{
|
||||
|
||||
MarketData::getMarketData($this->attributes['symbol']);
|
||||
}
|
||||
|
||||
|
||||
@@ -29,12 +29,13 @@ class SymbolValidationRule implements ValidationRule
|
||||
{
|
||||
$this->symbol = $value;
|
||||
|
||||
// Check if the symbol exists in the Market Data table first (avoid API call)
|
||||
if (MarketData::find($this->symbol)) {
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if the symbol exists in the Market Data table first (avoid API call)
|
||||
// Then check against market data provider
|
||||
if (! app(MarketDataInterface::class)->exists($value)) {
|
||||
$fail('The symbol provided ('.$this->symbol.') is not valid');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user