<?php

namespace App\Repository;

use App\Entity\ShippingCompany;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

class ShippingCompanyRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, ShippingCompany::class);
    }

    public function save(ShippingCompany $company, bool $flush = false): void
    {
        $this->getEntityManager()->persist($company);

        if ($flush) {
            $this->getEntityManager()->flush();
        }
    }

    public function remove(ShippingCompany $company, bool $flush = false): void
    {
        $this->getEntityManager()->remove($company);

        if ($flush) {
            $this->getEntityManager()->flush();
        }
    }

    /**
     * @return ShippingCompany[]
     */
    public function findAllOrderedByName(): array
    {
        return $this->createQueryBuilder('sc')
            ->orderBy('sc.name', 'ASC')
            ->getQuery()
            ->getResult();
    }

    public function findOneByName(string $name): ?ShippingCompany
    {
        return $this->createQueryBuilder('sc')
            ->andWhere('sc.name = :name')
            ->setParameter('name', $name)
            ->getQuery()
            ->getOneOrNullResult();
    }
}