<commit_before><?php

namespace Heystack\Subsystem\Ecommerce\Locale;

use Heystack\Subsystem\Core\State\State;
use Heystack\Subsystem\Core\State\StateableInterface;
use Heystack\Subsystem\Ecommerce\Locale\Interfaces\CountryInterface;
use Heystack\Subsystem\Ecommerce\Locale\Interfaces\LocaleServiceInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
 * Class LocaleService
 * @author  <NAME> <<EMAIL>>
 * @package Heystack\Subsystem\Ecommerce\Locale
 */
class LocaleService implements LocaleServiceInterface, StateableInterface
{
    /**
     * The active country key
     */
    const ACTIVE_COUNTRY_KEY = 'localservice.activecountry';
    /**
     * @var
     */
    protected $countries;
    /**
     * @var
     */
    protected $defaultCountry;
    /**
     * @var
     */
    protected $activeCountry;
    /**
     * @var \Heystack\Subsystem\Core\State\State
     */
    protected $sessionState;
    /**
     * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
     */
    protected $eventService;
    /**
     * @param array                    $countries
     * @param CountryInterface         $defaultCountry
     * @param State                    $sessionState
     * @param EventDispatcherInterface $eventService
     */
    public function __construct(
        array $countries,
        CountryInterface $defaultCountry,
        State $sessionState,
        EventDispatcherInterface $eventService
    ) {
        $this->setCountries($countries);
        $this->defaultCountry = $this->activeCountry = $defaultCountry;
        $this->sessionState = $sessionState;
        $this->eventService = $eventService;
    }
    /**
     * @param array $countries
     */
    public function setCountries(array $countries)
    {
        foreach ($countries as $country) {
            $this->addCountry($country);
        }
    }
    /**
     * @param CountryInterface $country
     */
    public function addCountry(CountryInterface $country)
    {
        $this->countries[$country->getIdentifier()->getFull()] = $country;
    }

    /**
     * Uses the State service to restore the data array. It also sets all the
     * currencies/default currency on the data array if this is the first time
     * this method has been called.
     */
    public function restoreState()
    {
        if ($identifier = $this->sessionState->getByKey(self::ACTIVE_COUNTRY_KEY)) {
            $this->setActiveCountry($identifier, false);
        }
    }
    /**
     * Saves the data array on the State service
     */
    public function saveState()
    {
        $this->sessionState->setByKey(
            self::ACTIVE_CURRENCY_KEY,
            $this->activeCountry->getIdentifier()->getFull()
        );
    }
    /**
     * @param      $identifier
     * @param bool $saveState  Determines whether the state is saved and the update event is dispatched
     */
    public function setActiveCountry($identifier, $saveState = true)
    {
        if ($country = $this->getCountry($identifier)) {
            $this->activeCountry = $country;

            if ($saveState) {
                $this->saveState();<commit_msg>BUGFIX: Country key not currency key<commit_after><?php

namespace Heystack\Subsystem\Ecommerce\Locale;

use Heystack\Subsystem\Core\State\State;
use Heystack\Subsystem\Core\State\StateableInterface;
use Heystack\Subsystem\Ecommerce\Locale\Interfaces\CountryInterface;
use Heystack\Subsystem\Ecommerce\Locale\Interfaces\LocaleServiceInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
 * Class LocaleService
 * @author  <NAME> <<EMAIL>>
 * @package Heystack\Subsystem\Ecommerce\Locale
 */
class LocaleService implements LocaleServiceInterface, StateableInterface
{
    /**
     * The active country key
     */
    const ACTIVE_COUNTRY_KEY = 'localservice.activecountry';
    /**
     * @var
     */
    protected $countries;
    /**
     * @var
     */
    protected $defaultCountry;
    /**
     * @var
     */
    protected $activeCountry;
    /**
     * @var \Heystack\Subsystem\Core\State\State
     */
    protected $sessionState;
    /**
     * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
     */
    protected $eventService;
    /**
     * @param array                    $countries
     * @param CountryInterface         $defaultCountry
     * @param State                    $sessionState
     * @param EventDispatcherInterface $eventService
     */
    public function __construct(
        array $countries,
        CountryInterface $defaultCountry,
        State $sessionState,
        EventDispatcherInterface $eventService
    ) {
        $this->setCountries($countries);
        $this->defaultCountry = $this->activeCountry = $defaultCountry;
        $this->sessionState = $sessionState;
        $this->eventService = $eventService;
    }
    /**
     * @param array $countries
     */
    public function setCountries(array $countries)
    {
        foreach ($countries as $country) {
            $this->addCountry($country);
        }
    }
    /**
     * @param CountryInterface $country
     */
    public function addCountry(CountryInterface $country)
    {
        $this->countries[$country->getIdentifier()->getFull()] = $country;
    }

    /**
     * Uses the State service to restore the data array. It also sets all the
     * currencies/default currency on the data array if this is the first time
     * this method has been called.
     */
    public function restoreState()
    {
        if ($identifier = $this->sessionState->getByKey(self::ACTIVE_COUNTRY_KEY)) {
            $this->setActiveCountry($identifier, false);
        }
    }
    /**
     * Saves the data array on the State service
     */
    public function saveState()
    {
        $this->sessionState->setByKey(
            self::ACTIVE_COUNTRY_KEY,
            $this->activeCountry->getIdentifier()->getFull()
        );
    }
    /**
     * @param      $identifier
     * @param bool $saveState  Determines whether the state is saved and the update event is dispatched
     */
    public function setActiveCountry($identifier, $saveState = true)
    {
        if ($country = $this->getCountry($identifier)) {
            $this->activeCountry = $country;

            if ($saveState) {
                $this->saveState();
{'fraction_non_alphanumeric': 0.11067131095630146, 'fraction_numerical': 0.0, 'mean_word_length': 2.4443838604143946, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 49, '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': '4041280', 'n_tokens_mistral': 1735, 'n_tokens_neox': 1711, 'n_words': 372}