<?php

namespace AutoAdmin\Bundle\Seo\Repository;

use AutoAdmin\Bundle\Content\Repository\AbstractContentRepository;

class RedirectRepository extends AbstractContentRepository
{
    public function findOneByOriginLike(string $url)
    {
        $qb = $this->createQueryBuilder();

        $qb->andWhere(':url LIKE REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(_.origin, \'**\', \'%\'), \'*1*\', \'%\'), \'*2*\', \'%\'), \'*3*\', \'%\'), \'*4*\', \'%\')');
        $qb->setParameter('url', str_replace('%', '\%', $url));

        return (($res = $qb->getQuery()->execute()) && !empty($res[0])) ? $res[0] : null;
    }
}
