<?php

namespace App\Services;

use App\ImportsMedia;
use App\Models\Album;
use Illuminate\Http\UploadedFile;
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;

class MediaImporter {
    private array $importers = [];

    public function register(string $importer) : void {
        if(in_array(ImportsMedia::class, class_implements($importer))) {
            array_push($this->importers, $importer);
        }
    }

    public function import(UploadedFile $file, Album $location): void {
        foreach ($this->importers as $importer) {
            if($importer::supports($file)) {
                (new $importer($file, $location))->import();
                return;
            }
        }

        throw new UnsupportedMediaTypeHttpException("The provided MediaType is not supported");
    }
}