<?php

namespace App\Tag\Repository;

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

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

    /** @return Tag[] */
    public function findActive(): array
    {
        return $this->createQueryBuilder('t')
            ->where('t.status = 1')
            ->orderBy('t.name', 'ASC')
            ->getQuery()
            ->getResult();
    }

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

    public function remove(Tag $tag, bool $flush = true): void
    {
        $this->getEntityManager()->remove($tag);
        if ($flush) $this->getEntityManager()->flush();
    }
}
