adds fallback capability to market data providers

This commit is contained in:
hackerESQ
2024-09-13 20:20:19 -05:00
parent d59b8447d0
commit 2b8fe3c3c2
5 changed files with 172 additions and 17 deletions
@@ -0,0 +1,42 @@
<?php
namespace App\Interfaces\MarketData;
use Illuminate\Support\Facades\Log;
class FallbackInterface
{
protected string $latest_error;
public function __call($method, $arguments)
{
$providers = explode(',', config('investbrain.provider', 'yahoo'));
foreach ($providers as $provider) {
$provider = trim($provider);
try {
if (!in_array($provider, array_keys(config('investbrain.interfaces', [])))) {
throw new \Exception("Provider [{$provider}] is not a valid market data interface.");
}
$provider_class_name = config("investbrain.interfaces.$provider");
return app()->make($provider_class_name)->$method(...$arguments);
} catch (\Throwable $e) {
$this->latest_error = $e->getMessage();
Log::warning("Failed calling method {$method} ({$provider}): {$this->latest_error}");
}
}
throw new \Exception("Could not get market data: {$this->latest_error}");
}
}