<?php

declare(strict_types=1);

namespace External\LeaflyBundle\View;

use KupShop\KupShopBundle\Views\View;

class QuickBuyVariationsView extends View
{
    protected $template = 'category.quickbuy-variations.tpl';
    private $productId;

    public function getBodyVariables()
    {
        $vars = [];

        if (!$this->productId) {
            $this->productId = $this->request->get('id_product');
        }
        $product = $this->getProduct();
        if (!$product) {
            return $vars;
        }

        $product->fetchVariations();
        $product->fetchImages(1);
        $vars['product'] = $product;

        return $vars;
    }

    public function setProductId(int $id): void
    {
        $this->productId = $id;
    }

    private function getProduct(): ?\Product
    {
        if ($this->productId) {
            $product = new \Product();

            if ($product->createFromDB($this->productId)) {
                return $product;
            }
        }

        return null;
    }
}
