<commit_before><?php

namespace Victoire\Bundle\ViewReferenceBundle\Cache;


use Predis\ClientInterface;
use Victoire\Bundle\ViewReferenceBundle\Cache\Redis\ViewReferenceRedisRepository;
use Victoire\Bundle\ViewReferenceBundle\Cache\Redis\ViewReferenceRedisTool;
use Victoire\Bundle\ViewReferenceBundle\ViewReference\ViewReference;

/**
 * Class UrlManager
 * @package Victoire\Bundle\ViewReferenceBundle\Cache
 *
 * This class manage url vor ViewReferences
 * ref : victoire_view_reference.url.manager
 */
class UrlManager
{
    private $viewReferenceRepository;
    private $redis;
    private $tool;

    /**
     * UrlManager constructor.
     * @param ViewReferenceRedisRepository $viewReferenceRepository
     * @param ClientInterface $redis
     */
    public function __construct(ViewReferenceRedisRepository $viewReferenceRepository, ClientInterface $redis)
    {
        $this->viewReferenceRepository = $viewReferenceRepository;
        $this->redis = $redis;
        $this->tool = new ViewReferenceRedisTool();
    }

    /**
     * This method build an url for a viewReference with parent in redis
     * @param ViewReference $viewReference
     */
    public function buildUrl(ViewReference $viewReference)
    {
        $reference = $this->viewReferenceRepository->findById($viewReference->getId());
        $url = "";
        // while the reference has a slug
        while(isset($reference['slug']) && $reference['slug'] != "" )
        {
            // Build url
            if($url != "")
            {
                $url = $reference['slug'].'/'. $url;
            }else{
                $url = $reference['slug'];
            }
            // Set reference with the parent
            if($parentId = $this->tool->unredislize($reference['parent']))
            {
                $reference = $this->viewReferenceRepository->findById($parentId);
            }else{
                $reference = array();

            }
        }
        // set the new url
        $this->setUrl($viewReference->getId(), $url, $viewReference->getLocale());
    }

    /**
     * This method set an url for a redis reference
     * @param $refId
     * @param $url
     * @param string $locale
     */
    public function setUrl($refId, $url, $locale = "fr")
    {
        //if an url exist for the current reference
        if($this->redis->hexists("reference:".$refId, "url"))
        {
            // Remove the old url
            $refUrl = $this->tool->unredislize($this->redis->hget("reference:".$refId, "url"));
            $this->removeUrl($refUrl, $locale);
            $this->redis->hdel("reference:".$refId, "url");
        }
        // Set the new url
        $this->redis->set($locale.":/".$url, $refId);
        $this->redis->hset("reference:".$refId, "url", $url);
    }

    /**
     * Find a ref id for an url
     * @param string $url
     * @param string $locale
     * @return mixed|string
     */
    public function findRefIdByUrl($url ="", $locale = "fr")
    {
        $refId = $this->tool->unredislize($this->redis->get($locale.":".$url));
        return $refId;
    }

    /**
     * Remove an url
     * @param $url<commit_msg>don't remove url if it's an empty string<commit_after><?php

namespace Victoire\Bundle\ViewReferenceBundle\Cache;


use Predis\ClientInterface;
use Victoire\Bundle\ViewReferenceBundle\Cache\Redis\ViewReferenceRedisRepository;
use Victoire\Bundle\ViewReferenceBundle\Cache\Redis\ViewReferenceRedisTool;
use Victoire\Bundle\ViewReferenceBundle\ViewReference\ViewReference;

/**
 * Class UrlManager
 * @package Victoire\Bundle\ViewReferenceBundle\Cache
 *
 * This class manage url vor ViewReferences
 * ref : victoire_view_reference.url.manager
 */
class UrlManager
{
    private $viewReferenceRepository;
    private $redis;
    private $tool;

    /**
     * UrlManager constructor.
     * @param ViewReferenceRedisRepository $viewReferenceRepository
     * @param ClientInterface $redis
     */
    public function __construct(ViewReferenceRedisRepository $viewReferenceRepository, ClientInterface $redis)
    {
        $this->viewReferenceRepository = $viewReferenceRepository;
        $this->redis = $redis;
        $this->tool = new ViewReferenceRedisTool();
    }

    /**
     * This method build an url for a viewReference with parent in redis
     * @param ViewReference $viewReference
     */
    public function buildUrl(ViewReference $viewReference)
    {
        $reference = $this->viewReferenceRepository->findById($viewReference->getId());
        $url = "";
        // while the reference has a slug
        while(isset($reference['slug']) && $reference['slug'] != "" )
        {
            // Build url
            if($url != "")
            {
                $url = $reference['slug'].'/'. $url;
            }else{
                $url = $reference['slug'];
            }
            // Set reference with the parent
            if($parentId = $this->tool->unredislize($reference['parent']))
            {
                $reference = $this->viewReferenceRepository->findById($parentId);
            }else{
                $reference = array();

            }
        }
        // set the new url
        $this->setUrl($viewReference->getId(), $url, $viewReference->getLocale());
    }

    /**
     * This method set an url for a redis reference
     * @param $refId
     * @param $url
     * @param string $locale
     */
    public function setUrl($refId, $url, $locale = "fr")
    {
        //if an url exist for the current reference
        if($this->redis->hexists("reference:".$refId, "url"))
        {
            // Remove the old url
            $refUrl = $this->tool->unredislize($this->redis->hget("reference:".$refId, "url"));
            if($refUrl != "")
            {
                $this->removeUrl($refUrl, $locale);
                $this->redis->hdel("reference:".$refId, "url");
            }
        }
        // Set the new url
        $this->redis->set($locale.":/".$url, $refId);
        $this->redis->hset("reference:".$refId, "url", $url);
    }

    /**
     * Find a ref id for an url
     * @param string $url
     * @param string $locale
     * @return mixed|string
     */
    public function findRefIdByUrl($url ="", $locale = "fr")
    {
        $refId = $this->tool->unredislize($this->redis->get($locale.":".$url));
        return $refId;
    }

    /**
     * Remove an url
     * @param $url
{'fraction_non_alphanumeric': 0.13977822895517725, 'fraction_numerical': 0.0, 'mean_word_length': 2.3027333677153172, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 63, 'https://': 0, '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': '2227589', 'n_tokens_mistral': 1830, 'n_tokens_neox': 1809, 'n_words': 389}