<?php

declare(strict_types=1);

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Commerce\Catalog\Repository;

use CpCreation\VitiCore\Commerce\Catalog\Model\CatalogConfiguration;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
 * @extends ServiceEntityRepository<CatalogConfiguration>
 */
final class CatalogConfigurationRepository extends ServiceEntityRepository implements CatalogConfigurationStore
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, CatalogConfiguration::class);
    }

    public function get(): ?CatalogConfiguration
    {
        return $this->find(CatalogConfiguration::SINGLETON_ID);
    }

    public function getOrCreate(): CatalogConfiguration
    {
        return $this->get() ?? new CatalogConfiguration();
    }

    public function save(CatalogConfiguration $configuration, bool $flush = true): void
    {
        $this->getEntityManager()->persist($configuration);
        if ($flush) {
            $this->getEntityManager()->flush();
        }
    }
}
