Files

108 lines
2.6 KiB
PHP
Raw Permalink Normal View History

2024-08-10 13:30:19 -05:00
<?php
2025-01-28 17:33:54 -06:00
declare(strict_types=1);
2024-08-10 13:30:19 -05:00
namespace App\Models;
2025-04-09 19:25:15 -05:00
use App\Actions\CopyToBaseCurrency;
use App\Casts\BaseCurrency;
2024-08-10 13:30:19 -05:00
use App\Interfaces\MarketData\MarketDataInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
2025-01-28 17:14:49 -06:00
use Illuminate\Database\Eloquent\Model;
2025-04-09 19:25:15 -05:00
use Illuminate\Support\Facades\Pipeline;
2024-08-10 13:30:19 -05:00
class MarketData extends Model
{
use HasFactory;
protected $primaryKey = 'symbol';
2025-01-28 17:14:49 -06:00
2024-08-10 13:30:19 -05:00
protected $keyType = 'string';
2025-01-28 17:14:49 -06:00
2024-08-10 13:30:19 -05:00
public $incrementing = false;
protected $fillable = [
'symbol',
'name',
2025-04-09 19:25:15 -05:00
'currency',
2024-08-10 13:30:19 -05:00
'market_value',
2025-04-09 19:25:15 -05:00
'market_value_base',
2024-08-10 13:30:19 -05:00
'fifty_two_week_high',
'fifty_two_week_low',
2024-08-24 22:19:40 -05:00
'forward_pe',
'trailing_pe',
2024-09-01 15:34:55 -05:00
'market_cap',
'book_value',
'last_dividend_date',
2025-04-09 19:25:15 -05:00
'last_dividend_amount',
2025-01-28 17:14:49 -06:00
'dividend_yield',
2025-04-09 19:25:15 -05:00
'meta_data',
2024-08-27 21:17:54 -05:00
];
2024-09-01 16:06:29 -05:00
protected $casts = [
'market_value' => 'float',
2025-04-09 19:25:15 -05:00
'market_value_base' => BaseCurrency::class,
2024-09-01 16:06:29 -05:00
'fifty_two_week_high' => 'float',
'fifty_two_week_low' => 'float',
'forward_pe' => 'float',
'trailing_pe' => 'float',
2025-04-09 19:25:15 -05:00
'market_cap' => 'integer',
2024-09-01 16:06:29 -05:00
'book_value' => 'float',
2025-04-09 19:25:15 -05:00
'last_dividend_date' => 'datetime',
'last_dividend_amount' => 'float',
2025-01-28 17:14:49 -06:00
'dividend_yield' => 'float',
2025-04-09 19:25:15 -05:00
'meta_data' => 'json',
2024-09-01 16:06:29 -05:00
];
2025-04-09 19:25:15 -05:00
protected static function boot()
{
parent::boot();
static::saving(function ($market_data) {
$market_data = Pipeline::send($market_data)
->through([
CopyToBaseCurrency::class,
])
->then(fn (MarketData $market_data) => $market_data);
});
}
2025-01-28 17:14:49 -06:00
public function holdings()
2024-08-10 13:30:19 -05:00
{
2024-08-30 21:58:38 -05:00
return $this->hasMany(Holding::class, 'symbol', 'symbol');
}
2024-08-10 13:30:19 -05:00
2024-08-30 21:58:38 -05:00
public function scopeSymbol($query, $symbol)
{
return $query->where('symbol', $symbol);
2024-08-10 13:30:19 -05:00
}
2025-04-09 19:25:15 -05:00
public static function getMarketData($symbol, $force = false): self
2024-08-10 13:30:19 -05:00
{
$market_data = self::firstOrNew([
2025-01-28 17:14:49 -06:00
'symbol' => $symbol,
]);
2024-08-10 13:30:19 -05:00
// check if new or stale
2024-08-24 22:19:40 -05:00
if (
2024-09-18 21:15:52 -05:00
$force
2025-01-28 17:14:49 -06:00
|| ! $market_data->exists
2024-08-24 22:19:40 -05:00
|| is_null($market_data->updated_at)
2024-09-09 17:20:26 -05:00
|| $market_data->updated_at->diffInMinutes(now()) >= config('investbrain.refresh')
2024-08-24 22:19:40 -05:00
) {
2025-01-28 17:14:49 -06:00
2024-08-10 13:30:19 -05:00
// get quote
2024-08-24 22:19:40 -05:00
$quote = app(MarketDataInterface::class)->quote($symbol);
2024-08-10 13:30:19 -05:00
// fill data
2024-08-24 22:19:40 -05:00
$market_data->fill($quote->toArray());
2024-08-10 13:30:19 -05:00
2024-08-30 20:58:00 -05:00
// save with timestamps updated
$market_data->touch();
}
2024-08-10 13:30:19 -05:00
return $market_data;
}
2025-01-28 17:14:49 -06:00
}