<commit_before><?php

namespace OpenOrchestra\Backoffice\Context;

use FOS\UserBundle\Model\GroupableInterface;
use OpenOrchestra\BaseBundle\Context\CurrentSiteIdInterface;
use OpenOrchestra\ModelInterface\Repository\SiteRepositoryInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

/**
 * Centralize app contextual datas
 */
class ContextManager implements CurrentSiteIdInterface
{
    const KEY_LOCALE = '_locale';
    const KEY_SITE = '_site';

    protected $session;
    protected $siteRepository;
    protected $tokenStorage;

    /**
     * Constructor
     *
     * @param Session                 $session
     * @param SiteRepositoryInterface $siteRepository
     * @param TokenStorageInterface   $tokenStorage
     * @param string                  $defaultLocale
     */
    public function __construct(Session $session, SiteRepositoryInterface $siteRepository, TokenStorageInterface $tokenStorage, $defaultLocale = 'en')
    {
        $this->session = $session;
        $this->tokenStorage = $tokenStorage;

        if ($this->getCurrentLocale() == '') {
            $this->setCurrentLocale($defaultLocale);
        }

        $this->siteRepository = $siteRepository;
    }

    /**
     * Get current locale value
     *
     * @return string
     */
    public function getCurrentLocale()
    {
        return $this->session->get(self::KEY_LOCALE);
    }

    /**
     * Set current locale
     *
     * @param string $locale
     */
    public function setCurrentLocale($locale)
    {
        $this->session->set(self::KEY_LOCALE, $locale);
    }

    /**
     * Get availables sites on platform
     *
     * @return array<SiteInterface>
     */
    public function getAvailableSites()
    {
        $token = $this->tokenStorage->getToken();
        $sites = array();

        if (($user = $token->getUser()) instanceof GroupableInterface) {
            foreach ($user->getGroups() as $group) {
                $sites[] = $group->getSite();
            }<commit_msg>put protection on token presence<commit_after><?php

namespace OpenOrchestra\Backoffice\Context;

use FOS\UserBundle\Model\GroupableInterface;
use OpenOrchestra\BaseBundle\Context\CurrentSiteIdInterface;
use OpenOrchestra\ModelInterface\Repository\SiteRepositoryInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

/**
 * Centralize app contextual datas
 */
class ContextManager implements CurrentSiteIdInterface
{
    const KEY_LOCALE = '_locale';
    const KEY_SITE = '_site';

    protected $session;
    protected $siteRepository;
    protected $tokenStorage;

    /**
     * Constructor
     *
     * @param Session                 $session
     * @param SiteRepositoryInterface $siteRepository
     * @param TokenStorageInterface   $tokenStorage
     * @param string                  $defaultLocale
     */
    public function __construct(Session $session, SiteRepositoryInterface $siteRepository, TokenStorageInterface $tokenStorage, $defaultLocale = 'en')
    {
        $this->session = $session;
        $this->tokenStorage = $tokenStorage;

        if ($this->getCurrentLocale() == '') {
            $this->setCurrentLocale($defaultLocale);
        }

        $this->siteRepository = $siteRepository;
    }

    /**
     * Get current locale value
     *
     * @return string
     */
    public function getCurrentLocale()
    {
        return $this->session->get(self::KEY_LOCALE);
    }

    /**
     * Set current locale
     *
     * @param string $locale
     */
    public function setCurrentLocale($locale)
    {
        $this->session->set(self::KEY_LOCALE, $locale);
    }

    /**
     * Get availables sites on platform
     *
     * @return array<SiteInterface>
     */
    public function getAvailableSites()
    {
        $token = $this->tokenStorage->getToken();
        $sites = array();

        if ($token && ($user = $token->getUser()) instanceof GroupableInterface) {
            foreach ($user->getGroups() as $group) {
                $sites[] = $group->getSite();
            }
{'fraction_non_alphanumeric': 0.11323003575685339, 'fraction_numerical': 0.0, 'mean_word_length': 2.703442188879082, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 33, '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': '1196466', 'n_tokens_mistral': 1123, 'n_tokens_neox': 1099, 'n_words': 229}