<commit_before><?php
/**
 * This file is part of the Ecommerce-Shipping package
 *
 * @package Ecommerce-Shipping
 */

/**
 * Interfaces namespace
 */
namespace Heystack\Subsystem\Shipping\CountryBased;

use Heystack\Subsystem\Shipping\Events;
use Heystack\Subsystem\Shipping\Interfaces\ShippingHandlerInterface;
use Heystack\Subsystem\Shipping\Traits\ShippingHandlerTrait;

use Heystack\Subsystem\Ecommerce\Transaction\TransactionModifierTypes;
use Heystack\Subsystem\Ecommerce\Transaction\Traits\TransactionModifierStateTrait;
use Heystack\Subsystem\Ecommerce\Transaction\Traits\TransactionModifierSerializeTrait;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Monolog\Logger;

use Heystack\Subsystem\Core\State\StateableInterface;
use Heystack\Subsystem\Core\State\State;

class ShippingHandler implements ShippingHandlerInterface, StateableInterface, \Serializable
{
    use ShippingHandlerTrait;
    use TransactionModifierStateTrait;
    use TransactionModifierSerializeTrait;
    
    /**
     * Holds the key used for storing state
     */
    const STATE_KEY = 'shipping';
    const ALL_COUNTRIES_KEY = 'allcountries';
    
    protected $data = array();    
    
    protected $countryClass;
    protected $eventService;
    protected $stateService;
    protected $monologService;
    
    
    public function __construct($countryClass, EventDispatcherInterface $eventService, State $stateService, Logger $monologService = null)
    {
        $this->countryClass = $countryClass;
        $this->eventService = $eventService;
        $this->stateService = $stateService;
        $this->monologService = $monologService;
    }
    
    public function getShippingFields(){
        return array(
            'AddressLine1',
            'AddressLine2',
            'City',
            'Postcode',
            'Country',
            'Title',
            'FirstName',
            'Surname',
            'Email',
            'Phone'
        );
    }
    
    public function getShippingFieldsData()
    {
        $data = array();
        
        foreach($this->getShippingFields() as $shippingField){
            $data[$shippingField] = $this->$shippingField;
        }
        
        return $data;
    }
    
    /**
     * If after restoring state no countries are loaded onto the data array get 
     * them from the database and load them to the data array, and save the state.
     * @throws \Exception
     */
    public function ensureDataExists()
    {
        if (!$this->data || !isset($this->data[self::ALL_COUNTRIES_KEY])) {
            $countries = \DataObject::get($this->countryClass);

            if ($countries instanceof \DataObjectSet && $countries->exists()) {

                foreach ($countries as $country) {
                    $this->data[self::ALL_COUNTRIES_KEY][$country->getIdentifier()] = $country;
                }
                
                $this->saveState();

            } else {
                
                if(isset($this->monologService)){
                    $this->monologService->err('Please create some countries');
                }
                
                throw new \Exception('Please create some countries');<commit_msg>MINOR: changed visibility of ensureDataExists()<commit_after><?php
/**
 * This file is part of the Ecommerce-Shipping package
 *
 * @package Ecommerce-Shipping
 */

/**
 * Interfaces namespace
 */
namespace Heystack\Subsystem\Shipping\CountryBased;

use Heystack\Subsystem\Shipping\Events;
use Heystack\Subsystem\Shipping\Interfaces\ShippingHandlerInterface;
use Heystack\Subsystem\Shipping\Traits\ShippingHandlerTrait;

use Heystack\Subsystem\Ecommerce\Transaction\TransactionModifierTypes;
use Heystack\Subsystem\Ecommerce\Transaction\Traits\TransactionModifierStateTrait;
use Heystack\Subsystem\Ecommerce\Transaction\Traits\TransactionModifierSerializeTrait;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Monolog\Logger;

use Heystack\Subsystem\Core\State\StateableInterface;
use Heystack\Subsystem\Core\State\State;

class ShippingHandler implements ShippingHandlerInterface, StateableInterface, \Serializable
{
    use ShippingHandlerTrait;
    use TransactionModifierStateTrait;
    use TransactionModifierSerializeTrait;
    
    /**
     * Holds the key used for storing state
     */
    const STATE_KEY = 'shipping';
    const ALL_COUNTRIES_KEY = 'allcountries';
    
    protected $data = array();    
    
    protected $countryClass;
    protected $eventService;
    protected $stateService;
    protected $monologService;
    
    
    public function __construct($countryClass, EventDispatcherInterface $eventService, State $stateService, Logger $monologService = null)
    {
        $this->countryClass = $countryClass;
        $this->eventService = $eventService;
        $this->stateService = $stateService;
        $this->monologService = $monologService;
    }
    
    public function getShippingFields(){
        return array(
            'AddressLine1',
            'AddressLine2',
            'City',
            'Postcode',
            'Country',
            'Title',
            'FirstName',
            'Surname',
            'Email',
            'Phone'
        );
    }
    
    public function getShippingFieldsData()
    {
        $data = array();
        
        foreach($this->getShippingFields() as $shippingField){
            $data[$shippingField] = $this->$shippingField;
        }
        
        return $data;
    }
    
    /**
     * If after restoring state no countries are loaded onto the data array get 
     * them from the database and load them to the data array, and save the state.
     * @throws \Exception
     */
    protected function ensureDataExists()
    {
        if (!$this->data || !isset($this->data[self::ALL_COUNTRIES_KEY])) {
            $countries = \DataObject::get($this->countryClass);

            if ($countries instanceof \DataObjectSet && $countries->exists()) {

                foreach ($countries as $country) {
                    $this->data[self::ALL_COUNTRIES_KEY][$country->getIdentifier()] = $country;
                }
                
                $this->saveState();

            } else {
                
                if(isset($this->monologService)){
                    $this->monologService->err('Please create some countries');
                }
                
                throw new \Exception('Please create some countries');
{'fraction_non_alphanumeric': 0.1003556517705273, 'fraction_numerical': 0.0006185248183083346, 'mean_word_length': 2.4532835024025625, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, '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': '937441', 'n_tokens_mistral': 1742, 'n_tokens_neox': 1717, 'n_words': 359}