Source path: app/Console/Commands/RefreshTokens.php
Extracted from supplied specification. Review before copying into application.

<?php
namespace App\Console\Commands;

use App\Events\TokenExpiring;
use App\Models\SocialAccount;
use App\Services\Publishers\PublisherFactory;
use Illuminate\Console\Command;

class RefreshTokens extends Command
{
    protected $signature = 'tokens:refresh';
    protected $description = 'Refresh tokens expiring within 7 days';

    public function handle(PublisherFactory $factory): int
    {
        SocialAccount::where('is_active', 1)
            ->whereNotNull('token_expires_at')
            ->where('token_expires_at', '<=', now()->addDays(7))
            ->each(function (SocialAccount $acc) use ($factory) {
                try {
                    $factory->for($acc->platform)->refreshToken($acc);
                } catch (\Throwable $e) {
                    TokenExpiring::dispatch($acc);
                }
            });

        return self::SUCCESS;
    }
}
