<?php

namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\DAO;

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\DAO\Exception\NotFoundException;
use WP_Post;
class ProductAttachmentDAO
{
    public const ATTACHMENT_META_URL = 'url';
    public function get_attachment_id_by_url(string $url): WP_Post
    {
        $attachments = get_posts(['posts_per_page' => 1, 'post_type' => 'attachment', 'meta_query' => [
            //phpcs:ignore
            ['key' => self::ATTACHMENT_META_URL, 'value' => $url, 'compare' => '='],
        ]]);
        if (empty($attachments)) {
            throw new NotFoundException('Error, attachment not found');
        }
        $attachment = reset($attachments);
        return $attachment;
    }
}
