adds fallback capability to market data providers
This commit is contained in:
@@ -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}");
|
||||
}
|
||||
}
|
||||
@@ -11,16 +11,9 @@ class AppServiceProvider extends ServiceProvider
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
if (!in_array(
|
||||
$interface = config('investbrain.default', 'yahoo'),
|
||||
array_keys(config('investbrain.interfaces', []))
|
||||
)) {
|
||||
throw new \Exception("Error: '$interface' is not a valid market data interface.");
|
||||
}
|
||||
|
||||
$this->app->bind(
|
||||
\App\Interfaces\MarketData\MarketDataInterface::class,
|
||||
config("investbrain.interfaces.$interface")
|
||||
\App\Interfaces\MarketData\FallbackInterface::class
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user