<?php
/**
 * @author Colinet Julien
 */

namespace CpCreation\VitiCore\Shop\Repository;

use CpCreation\VitiCore\Repository\BaseRepository;
use CpCreation\VitiCore\Shop\Model\ShippingConfig;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\Persistence\ManagerRegistry;

class ShippingConfigRepository extends BaseRepository
{
    private bool $shippingConfigLoaded = false;

    private ?ShippingConfig $shippingConfig = null;

    /**
     * @throws \ReflectionException
     */
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, ShippingConfig::class);
    }

    /**
     * @throws NonUniqueResultException
     */
    public function findOne(): ?ShippingConfig
    {
        if ($this->shippingConfigLoaded) {
            return $this->shippingConfig;
        }

        $this->shippingConfigLoaded = true;

        return $this->shippingConfig = $this
            ->createQueryBuilder('shipping_config')
            ->setMaxResults(1)
            ->getQuery()
            ->getOneOrNullResult();
    }
}
