<?php

declare(strict_types=1);

namespace Autogedal\FeedMonitor\Model\Adapter;

use Autogedal\FeedMonitor\Model\FeedRun;
use Autogedal\FeedMonitor\Model\JobRegistry;

/**
 * Custom stock / price cron jobs monitored as a whole (one run, one alert per job).
 */
class CustomCronMonitor extends AbstractJobMonitor
{
    public function begin(string $jobCode): FeedRun
    {
        return $this->monitor->start($jobCode);
    }

    public function check(bool $outsideCron): array
    {
        $found = [];
        foreach ($this->jobs->getJobCodes(JobRegistry::SOURCE_CUSTOM) as $jobCode) {
            $maxAge = $this->getMaxAgeMinutes($jobCode);
            if ($this->jobs->getFeedCodes($jobCode) || $maxAge === null) {
                continue; // sub-feeds: CustomSubfeedMonitor
            }
            $line = $this->monitor->checkNotRunning($jobCode, '', $maxAge);
            if ($line !== null) {
                $found[] = $line;
            }
        }

        return $found;
    }
}
