Files

53 lines
1.4 KiB
PHP
Raw Permalink Normal View History

<?php
2025-01-28 17:33:54 -06:00
declare(strict_types=1);
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'));
2025-01-28 17:14:49 -06:00
foreach ($providers as $provider) {
$provider = trim($provider);
2025-04-09 19:25:15 -05:00
$symbol = $arguments[0];
try {
2025-04-09 19:25:15 -05:00
Log::info("Calling method {$method} for {$symbol} ({$provider})");
2025-01-28 17:14:49 -06:00
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) {
2025-01-28 17:14:49 -06:00
$this->latest_error = $e->getMessage();
2025-04-09 19:25:15 -05:00
Log::error("Failed calling method {$method} for {$symbol} ({$provider}): {$this->latest_error}");
}
}
2025-04-09 19:25:15 -05:00
// don't need to throw error if calling exists method...
if ($method == 'exists') {
// symbol prob just doesn't exist
return false;
}
2025-04-09 19:25:15 -05:00
throw new \Exception("Could not get market data calling method {$method}: {$this->latest_error}");
}
}