<commit_before><?php
/**
 * Domain Manager
 *
 * This file contains the class used to manage domain names for the CMS.
 *
 * 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\Service;

use Doctrine\ORM\EntityRepository;
use Rcm\Exception\InvalidArgumentException;
use Zend\Cache\Storage\StorageInterface;

/**
 * Redirect Manager.
 *
 * The Redirect Manager is used to manage Redirects in the CMS.  Each site object is
 * related to a redirect for the site.  This allows the CMS to manage multiple sites
 * with one install of 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 RedirectManager
{
    /** @var \Rcm\Repository\Redirect */
    protected $repository;

    /** @var \Zend\Cache\Storage\StorageInterface */
    protected $cache;

    protected $siteManager;

    /**
     * Constructor
     *
     * @param EntityRepository $repository  Entity Repository for Redirects
     * @param StorageInterface $cache       Zend Cache Manager
     * @param SiteManager      $siteManager Rcm Site Manager
     */
    public function __construct(
        EntityRepository $repository,
        StorageInterface $cache,
        SiteManager $siteManager
    ) {
        $this->repository = $repository;
        $this->cache = $cache;
        $this->siteManager = $siteManager;
    }

    /**
     * Get a list of redirects for the CMS.
     *
     * @param integer $siteId Site Id
     *
     * @return array|null
     * @throws \Rcm\Exception\InvalidArgumentException
     */
    public function getRedirectList()
    {
        $siteId = $this->siteManager->getCurrentSiteId();

        if (!$this->siteManager->isValidSiteId($siteId)) {
            throw new InvalidArgumentException('Invalid Site ID');
        }

        $cacheKey = 'rcm_redirect_list_' . $siteId;

        //Check Cache for list of domains
        if ($this->cache->hasItem($cacheKey)) {
            return $this->cache->getItem($cacheKey);
        }

        $redirectList = $this->repository->getRedirectList($siteId);

        $this->cache->setItem($cacheKey, $redirectList);

        return $redirectList;
    }

    /**
     * getRedirectUrl
     *
     * @return string|null
     */

    public function getRedirectUrl($httpHost, $requestUri)
    {
        $active = $this->isCurrentSiteActive();

        if(!$active){
            $siteOne = $this->siteManager->getSiteById(1);
            $redirectUrl = $siteOne->getDomain()->getDomainName();

            return $redirectUrl;
        }

        $redirectList = $this->getRedirectList();

        if(isset($redirectList[$requestUri])){
            $redirectUrl = $httpHost.$redirectList[$requestUri]['redirectUrl'];

            return $redirectUrl;
        }

        return null;
    }

    /**
     * checkActive
     *
     * @return boolean
     */
    public function isCurrentSiteActive()
    {
        /**
         * @var $siteManager \Rcm\Service\SiteManager
         */
        $siteId = $this->siteManager->getCurrentSiteId();
        $currentSite = $this->siteManager->getSiteById($siteId);
        $status = $currentSite->getStatus();

        if ($status != 'A' &&  $siteId != 1) {
            return false;
        }<commit_msg>URL redirect - redirect to site with siteId '1' if disabled - redirect to specific URI if found in redirect list<commit_after><?php
/**
 * Domain Manager
 *
 * This file contains the class used to manage domain names for the CMS.
 *
 * PHP version 5.3
 *
 * LICENSE: BSD
 *
 * @category  Reliv
 * @package   Rcm
 * @author    <NAME>fer <<EMAIL>>
 * @copyright 2014 Reliv International
 * @license   License.txt New BSD License
 * @version   GIT: <git_id>
 * @link      https://github.com/reliv
 */

namespace Rcm\Service;

use Doctrine\ORM\EntityRepository;
use Rcm\Exception\InvalidArgumentException;
use Zend\Cache\Storage\StorageInterface;

/**
 * Redirect Manager.
 *
 * The Redirect Manager is used to manage Redirects in the CMS.  Each site object is
 * related to a redirect for the site.  This allows the CMS to manage multiple sites
 * with one install of 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 RedirectManager
{
    /** @var \Rcm\Repository\Redirect */
    protected $repository;

    /** @var \Zend\Cache\Storage\StorageInterface */
    protected $cache;

    protected $siteManager;

    /**
     * Constructor
     *
     * @param EntityRepository $repository  Entity Repository for Redirects
     * @param StorageInterface $cache       Zend Cache Manager
     * @param SiteManager      $siteManager Rcm Site Manager
     */
    public function __construct(
        EntityRepository $repository,
        StorageInterface $cache,
        SiteManager $siteManager
    ) {
        $this->repository = $repository;
        $this->cache = $cache;
        $this->siteManager = $siteManager;
    }

    /**
     * Get a list of redirects for the CMS.
     *
     * @param integer $siteId Site Id
     *
     * @return array|null
     * @throws \Rcm\Exception\InvalidArgumentException
     */
    public function getRedirectList()
    {
        $siteId = $this->siteManager->getCurrentSiteId();

        if (!$this->siteManager->isValidSiteId($siteId)) {
            throw new InvalidArgumentException('Invalid Site ID');
        }

        $cacheKey = 'rcm_redirect_list_' . $siteId;

        //Check Cache for list of domains
        if ($this->cache->hasItem($cacheKey)) {
            return $this->cache->getItem($cacheKey);
        }

        $redirectList = $this->repository->getRedirectList($siteId);

        $this->cache->setItem($cacheKey, $redirectList);

        return $redirectList;
    }

    /**
     * getRedirectUrl
     *
     * @return string|null
     */

    public function getRedirectUrl($httpHost, $requestUri)
    {
        $active = $this->isCurrentSiteActive();

        if(!$active){
            $siteOne = $this->siteManager->getSiteById(1);
            $redirectUrl = $siteOne->getDomain()->getDomainName();

            return $redirectUrl;
        }

        $redirectList = $this->getRedirectList();

        if(isset($redirectList[$requestUri])){
            $redirectUrl = $httpHost.$redirectList[$requestUri]['redirectUrl'];

            return $redirectUrl;
        }

        return null;
    }

    /**
     * isCurrentSiteActive
     *
     * @return boolean
     */
    public function isCurrentSiteActive()
    {
        /**
         * @var $siteManager \Rcm\Service\SiteManager
         */
        $siteId = $this->siteManager->getCurrentSiteId();
        $currentSite = $this->siteManager->getSiteById($siteId);
        $status = $currentSite->getStatus();

        if ($status != 'A' &&  $siteId != 1) {
            return false;
        }
{'fraction_non_alphanumeric': 0.12078346028291621, 'fraction_numerical': 0.004488574537540806, 'mean_word_length': 2.3713892709766164, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 69, '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': '2346621', 'n_tokens_mistral': 2133, 'n_tokens_neox': 2059, 'n_words': 530}