<?php

namespace App\Services\Registries;

use App\Models\Asset;
use App\Models\Registries\Registry;
use App\Services\FileDownloadService;
use Illuminate\Support\Str;
use PHPHtmlParser\Dom;

class RegistryImportService {
    protected Registry $registry;
    protected string $url;
    protected Dom $dom;

    public function __construct(Registry $registry, string $url) {
        $this->registry = $registry;
        $this->url = $url;
        $this->dom = new Dom;
        $this->dom->loadFromUrl($url);
    }


    public function download($url, $dir = null, $filename = null): ?Asset {
        $info = parse_url($url);
        if (empty($info['host'])) {
            $url = 'https://' . Str::replace('//', '/', "www.faufcc.ru/{$url}");
        }
        return (new FileDownloadService())->download($url, $dir, $filename);
    }

}