fix: upgrade the exists() market data provider method

This commit is contained in:
hackerESQ
2025-01-28 20:32:43 -06:00
parent 0f135f4024
commit 4ece09368e
7 changed files with 49 additions and 42 deletions
@@ -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
+11 -23
View File
@@ -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,
]);
}
+1
View File
@@ -143,6 +143,7 @@ class Transaction extends Model
public function refreshMarketData(): void
{
MarketData::getMarketData($this->attributes['symbol']);
}
+2 -1
View File
@@ -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');
}
+26
View File
@@ -77,4 +77,30 @@ class FallbackInterfaceTest extends TestCase
Log::shouldHaveReceived('warning')->with('Failed calling method quote (yahoo): Yahoo failed');
Log::shouldHaveReceived('warning')->with('Failed calling method quote (alpha): Alpha failed');
}
public function test_exists_method_fails_without_exception()
{
config()->set('investbrain.provider', 'yahoo,alpha');
config()->set('investbrain.interfaces', [
'yahoo' => YahooMarketData::class,
'alphavantage' => AlphaVantageMarketData::class,
]);
$yahooMock = Mockery::mock(YahooMarketData::class);
$yahooMock->shouldReceive('exists')
->andThrow(new \Exception('Yahoo failed'));
$alphaMock = Mockery::mock(AlphaVantageMarketData::class);
$alphaMock->shouldReceive('exists')
->andThrow(new \Exception('Alpha failed'));
$this->app->instance(YahooMarketData::class, $yahooMock);
$this->app->instance(AlphaVantageMarketData::class, $alphaMock);
$fallbackInterface = new FallbackInterface;
$result = $fallbackInterface->exists('ZZZ');
$this->assertFalse($result);
}
}