<?php

namespace Core\App\Cron;

class CronRunner
{
    protected array $tasks = [];

    public function register(CronTaskInterface $task): void
    {
        $this->tasks[] = $task;
    }

    public function runAll(): array
    {
        $results = [];

        foreach ($this->tasks as $task) {
            try {
                $results[$task->name()] = $task->run();
            } catch (\Throwable $e) {
                $results[$task->name()] = [
                    'success' => false,
                    'error'   => $e->getMessage()
                ];
            }
        }

        return $results;
    }
}
