<commit_before><?php

/**
 * Domain Repository
 *
 * This file contains the domain repository
 *
 * PHP version 5.3
 *
 * LICENSE: BSD
 *
 * @category  Reliv
 * @package   Rcm
 * @author    <NAME> <<EMAIL>>
 * @copyright 2014 Reliv International
 * @license   License.txt New BSD License
 * @version   GIT: <git_id>
 * @link      https://github.com/reliv
 */

namespace Rcm\Repository;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NoResultException;
use Doctrine\ORM\Query;
use Doctrine\ORM\Query\Expr\Join;
use Rcm\Entity\Language as LanguageEntity;
use Rcm\Exception\DomainNotFoundException;
use Rcm\Exception\DuplicateDomainException;

/**
 * Domain Repository
 *
 * Domain Repository.  Used to get domains for the CMS
 *
 * PHP version 5.3
 *
 * LICENSE: BSD
 *
 * @category  Reliv
 * @package   Rcm
 * @author    <NAME> <<EMAIL>>
 * @copyright 2012 Reliv International
 * @license   License.txt New BSD License
 * @version   Release: 1.0
 * @link      https://github.com/reliv
 */
class Domain extends EntityRepository
{
    /**
     * Get the current list of domains.
     *
     * @return array
     */
    public function getActiveDomainList()
    {
        return $this->getDomainLookupQuery()->getArrayResult();
    }

    /**
     * Get the info for a single domain
     *
     * @param string $domain Domain name to search by
     *
     * @return array
     */
    public function getDomainInfo($domain)
    {
        try {
            $result = $this->getDomainLookupQuery($domain)->getSingleResult();
        } catch (NoResultException $e) {
            $result = null;
        }

        return $result;
    }

    /**
     * Get Doctrine Query Object for Domain Lookups
     *
     * @param null $domain
     *
     * @return Query
     */
    private function getDomainLookupQuery($domain = null)
    {
        /** @var \Doctrine\ORM\QueryBuilder $queryBuilder */
        $queryBuilder = $this->_em->createQueryBuilder();

        $queryBuilder->select(
            'domain.domain,
            primary.domain primaryDomain,
            language.iso639_2b languageId,
            site.siteId,
            country.iso3 countryId'
        )
            ->from('\Rcm\Entity\Domain', 'domain', 'domain.domain')
            ->leftJoin('domain.primaryDomain', 'primary')
            ->leftJoin('domain.defaultLanguage', 'language')
            ->leftJoin(
                '\Rcm\Entity\Site',
                'site',
                Join::WITH,
                'site.domain = domain.domainId'
            )
            ->leftJoin('site.country', 'country');

        if (!empty($domain)) {
            $queryBuilder->andWhere('domain.domain = :domain')
                ->setParameter('domain', $domain);
        }

        return $queryBuilder->getQuery();
    }

    /**
     * getDomainByName
     *
     * @param      $domainName
     * @param null $default
     *
     * @return null|object
     */
    public function getDomainByName($domainName, $default = null)
    {
        if (empty($domainName)) {
            return $default;
        }

        try {
            $result = $this->findOneBy(['domain' => $domainName]);
        } catch (NoResultException $e) {
            $result = $default;
        }

        return $result;
    }

    /**
     * Create Domain
     *
     * @param string $domainName
     * @param mixed $primaryDomain
     *
     * @return \Rcm\Entity\Domain
     */
    public function createDomain(
        $domainName,
        $primaryDomain = null
    ) {
        if (empty($domainName)) {
            throw new DomainNotFoundException('Domain name is required.');
        }

        // Check if exists first
        $existingDomain = $this->getDomainByName($domainName);

        if (!empty($existingDomain)) {
            throw new DuplicateDomainException(
                'Duplicate domains may not be created.'
            );
        }

        $domain = new \Rcm\Entity\Domain();
        $domain->setDomainName($domainName);

        if ($primaryDomain instanceof \Rcm\Entity\Domain) {
            $domain->setPrimary($primaryDomain);
        }

        $this->getEntityManager()->persist($domain);

        return $domain;
    }
}<commit_msg>Added search for domain by query<commit_after><?php

/**
 * Domain Repository
 *
 * This file contains the domain repository
 *
 * PHP version 5.3
 *
 * LICENSE: BSD
 *
 * @category  Reliv
 * @package   Rcm
 * @author    <NAME> <<EMAIL>>
 * @copyright 2014 Reliv International
 * @license   License.txt New BSD License
 * @version   GIT: <git_id>
 * @link      https://github.com/reliv
 */

namespace Rcm\Repository;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NoResultException;
use Doctrine\ORM\Query;
use Doctrine\ORM\Query\Expr\Join;
use Rcm\Entity\Language as LanguageEntity;
use Rcm\Exception\DomainNotFoundException;
use Rcm\Exception\DuplicateDomainException;

/**
 * Domain Repository
 *
 * Domain Repository.  Used to get domains for the CMS
 *
 * PHP version 5.3
 *
 * LICENSE: BSD
 *
 * @category  Reliv
 * @package   Rcm
 * @author    <NAME> <<EMAIL>>
 * @copyright 2012 Reliv International
 * @license   License.txt New BSD License
 * @version   Release: 1.0
 * @link      https://github.com/reliv
 */
class Domain extends EntityRepository
{
    /**
     * Get the current list of domains.
     *
     * @return array
     */
    public function getActiveDomainList()
    {
        return $this->getDomainLookupQuery()->getArrayResult();
    }

    /**
     * Get the info for a single domain
     *
     * @param string $domain Domain name to search by
     *
     * @return array
     */
    public function getDomainInfo($domain)
    {
        try {
            $result = $this->getDomainLookupQuery($domain)->getSingleResult();
        } catch (NoResultException $e) {
            $result = null;
        }

        return $result;
    }

    /**
     * Get Doctrine Query Object for Domain Lookups
     *
     * @param null $domain
     *
     * @return Query
     */
    private function getDomainLookupQuery($domain = null)
    {
        /** @var \Doctrine\ORM\QueryBuilder $queryBuilder */
        $queryBuilder = $this->_em->createQueryBuilder();

        $queryBuilder->select(
            'domain.domain,
            primary.domain primaryDomain,
            language.iso639_2b languageId,
            site.siteId,
            country.iso3 countryId'
        )
            ->from('\Rcm\Entity\Domain', 'domain', 'domain.domain')
            ->leftJoin('domain.primaryDomain', 'primary')
            ->leftJoin('domain.defaultLanguage', 'language')
            ->leftJoin(
                '\Rcm\Entity\Site',
                'site',
                Join::WITH,
                'site.domain = domain.domainId'
            )
            ->leftJoin('site.country', 'country');

        if (!empty($domain)) {
            $queryBuilder->andWhere('domain.domain = :domain')
                ->setParameter('domain', $domain);
        }

        return $queryBuilder->getQuery();
    }

    /**
     * getDomainByName
     *
     * @param      $domainName
     * @param null $default
     *
     * @return null|object
     */
    public function getDomainByName($domainName, $default = null)
    {
        if (empty($domainName)) {
            return $default;
        }

        try {
            $result = $this->findOneBy(['domain' => $domainName]);
        } catch (NoResultException $e) {
            $result = $default;
        }

        return $result;
    }

    /**
     * Create Domain
     *
     * @param string $domainName
     * @param mixed $primaryDomain
     *
     * @return \Rcm\Entity\Domain
     */
    public function createDomain(
        $domainName,
        $primaryDomain = null
    ) {
        if (empty($domainName)) {
            throw new DomainNotFoundException('Domain name is required.');
        }

        // Check if exists first
        $existingDomain = $this->getDomainByName($domainName);

        if (!empty($existingDomain)) {
            throw new DuplicateDomainException(
                'Duplicate domains may not be created.'
            );
        }

        $domain = new \Rcm\Entity\Domain();
        $domain->setDomainName($domainName);

        if ($primaryDomain instanceof \Rcm\Entity\Domain) {
            $domain->setPrimary($primaryDomain);
        }

        $this->getEntityManager()->persist($domain);

        return $domain;
    }

    /**
     * Search for a domain name by query string.
     *
     * @param string $domainSearchParam Query String... ie. "domain LIKE '[queryString]"
     *
     * @return array
     */
    public function searchForDomain($domainSearchParam)
    {
        $domainsQueryBuilder = $pwsSites = $this->createQueryBuilder('domain');
        $domainsQueryBuilder->where('domain.domain LIKE :domainSearchParam');

        $query = $domainsQueryBuilder->getQuery();
        $query->setParameter('domainSearchParam', $domainSearchParam);

        return $query->getResult();
    }
}
{'fraction_non_alphanumeric': 0.1222358039782198, 'fraction_numerical': 0.004222691410156684, 'mean_word_length': 2.0181086519114686, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 66, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5686912', 'n_tokens_mistral': 2564, 'n_tokens_neox': 2479, 'n_words': 581}