<?php

namespace App\Tests\Domain\Scraping\Adapter;

use App\Domain\Scraping\Domain\Contract\Repository\ScrapingJobRepositoryInterface;
use App\Domain\Scraping\Domain\Model\ScrapingJob;

class InMemoryScrapingJobRepository implements ScrapingJobRepositoryInterface
{
    /** @var ScrapingJob[] */
    private static array $jobs = [];

    public function save(ScrapingJob $job): void
    {
        self::$jobs[$job->getId()] = $job;
    }

    public function getJobs(): array
    {
        return array_values(self::$jobs);
    }

    public function findById(string $id): ?ScrapingJob
    {
        return self::$jobs[$id] ?? null;
    }

    public function findByChapterId(string $chapterId): ?ScrapingJob
    {
        foreach (self::$jobs as $job) {
            if ($job->getId() === $chapterId) {
                return $job;
            }
        }

        return null;
    }

    public function clear(): void
    {
        self::$jobs = [];
    }
}
