<commit_before><?php

namespace Heystack\Subsystem\Deals\Result;

use Heystack\Subsystem\Core\DataObjectHandler\DataObjectHandlerInterface;
use Heystack\Subsystem\Core\Identifier\Identifier;
use Heystack\Subsystem\Core\State\State;
use Heystack\Subsystem\Deals\Events;
use Heystack\Subsystem\Deals\Interfaces\AdaptableConfigurationInterface;
use Heystack\Subsystem\Deals\Interfaces\DealHandlerInterface;
use Heystack\Subsystem\Deals\Interfaces\PurchasableConditionInterface;
use Heystack\Subsystem\Deals\Interfaces\ResultInterface;
use Heystack\Subsystem\Ecommerce\Purchasable\Interfaces\PurchasableHolderInterface;
use Heystack\Subsystem\Ecommerce\Purchasable\Interfaces\PurchasableInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
 *
 * @copyright  Heyday
 * @author     <NAME> <<EMAIL>>
 * @package    Ecommerce-Deals
 */
class FreePurchasable implements ResultInterface
{
    /**
     *
     */
    const IDENTIFIER = 'free_purchasables';

    /**
     * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
     */
    protected $eventService;
    /**
     * @var \Heystack\Subsystem\Ecommerce\Purchasable\Interfaces\PurchasableHolderInterface
     */
    protected $purchasableHolder;
    /**
     * @var \Heystack\Subsystem\Core\State\State
     */
    protected $stateService;
    /**
     * @var \Heystack\Subsystem\Core\DataObjectHandler\DataObjectHandlerInterface
     */
    protected $dataObjectHandler;
    /**
     * @var \Heystack\Subsystem\Deals\Interfaces\AdaptableConfigurationInterface
     */
    protected $configuration;
    /**
     * @var int
     */
    protected $total = 0;

    /**
     * @param EventDispatcherInterface $eventService
     * @param PurchasableHolderInterface $purchasableHolder
     * @param State $stateService
     * @param AdaptableConfigurationInterface $configuration
     * @throws \Exception when there is no purchasable_identifier in the configuration
     */
    public function __construct(
        EventDispatcherInterface $eventService,
        PurchasableHolderInterface $purchasableHolder,
        State $stateService,
        DataObjectHandlerInterface $dataObjectHandler,
        AdaptableConfigurationInterface $configuration
    ) {
        $this->eventService = $eventService;
        $this->purchasableHolder = $purchasableHolder;
        $this->stateService = $stateService;
        $this->dataObjectHandler = $dataObjectHandler;
        $this->configuration = $configuration;

        if (!$configuration->hasConfig('purchasable_identifier')) {
            throw new \Exception('Free Purchasables Result requires a purchasable_identifier configuration value');
        }
    }

    /**
     * @param Heystack\Subsystem\Core\Identifier\Identifier the deal handlers identifier
     */
    protected function saveState($identifier)
    {
        $this->stateService->setByKey(
            self::IDENTIFIER . $identifier,
            array(
                $this->total
            )
        );
    }

    /**
     * @param Heystack\Subsystem\Core\Identifier\Identifier the deal handler's identifier
     */
    protected function restoreState($identifier)
    {
        $data = $this->stateService->getByKey(self::IDENTIFIER . $identifier);
        if (is_array($data)) {
            list($this->total) = $data;
        }
    }

    /**
     * @return string
     */
    public function getDescription()
    {
        return 'Free Purchasable:' . $this->configuration['purchasable_identifier'];
    }

    /**
     * @param DealHandlerInterface $dealHandler
     * @return mixed
     */
    public function process(DealHandlerInterface $dealHandler)
    {
        $dealIdentifier = $dealHandler->getIdentifier()->getFull();
        $this->restoreState($dealIdentifier);

        $purchasable = $this->getPurchasable();

        if (!$this->processed($purchasable, $dealHandler)) {

            //Add the selected purchasable to the purchasableHolder
            $this->purchasableHolder->addPurchasable($purchasable);

            //Get the processed purchasable back from the purchasableHolder (could be a different object than the one passed in)
            $productHolderPurchasable = $this->purchasableHolder->getPurchasable($purchasable->getIdentifier());

            $this->total = $productHolderPurchasable->getUnitPrice();
            $this->saveState($dealIdentifier);

            $this->eventService->dispatch(Events::RESULT_PROCESSED);
        }

        return $this->total;
    }

    protected function processed(PurchasableInterface $purchasable, DealHandlerInterface $dealHandler)
    {
        $productHolderPurchasable = $this->purchasableHolder->getPurchasable($purchasable->getIdentifier());

        $conditions = $dealHandler->getConditions();

        $purchasableIdentifierFromCondition = null;
<commit_msg>BUGFIX: use the adaptable configuration object getConfig instead of assuming the configuration is an array<commit_after><?php

namespace Heystack\Subsystem\Deals\Result;

use Heystack\Subsystem\Core\DataObjectHandler\DataObjectHandlerInterface;
use Heystack\Subsystem\Core\Identifier\Identifier;
use Heystack\Subsystem\Core\State\State;
use Heystack\Subsystem\Deals\Events;
use Heystack\Subsystem\Deals\Interfaces\AdaptableConfigurationInterface;
use Heystack\Subsystem\Deals\Interfaces\DealHandlerInterface;
use Heystack\Subsystem\Deals\Interfaces\PurchasableConditionInterface;
use Heystack\Subsystem\Deals\Interfaces\ResultInterface;
use Heystack\Subsystem\Ecommerce\Purchasable\Interfaces\PurchasableHolderInterface;
use Heystack\Subsystem\Ecommerce\Purchasable\Interfaces\PurchasableInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
 *
 * @copyright  Heyday
 * @author     <NAME> <<EMAIL>>
 * @package    Ecommerce-Deals
 */
class FreePurchasable implements ResultInterface
{
    /**
     *
     */
    const IDENTIFIER = 'free_purchasables';

    /**
     * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
     */
    protected $eventService;
    /**
     * @var \Heystack\Subsystem\Ecommerce\Purchasable\Interfaces\PurchasableHolderInterface
     */
    protected $purchasableHolder;
    /**
     * @var \Heystack\Subsystem\Core\State\State
     */
    protected $stateService;
    /**
     * @var \Heystack\Subsystem\Core\DataObjectHandler\DataObjectHandlerInterface
     */
    protected $dataObjectHandler;
    /**
     * @var \Heystack\Subsystem\Deals\Interfaces\AdaptableConfigurationInterface
     */
    protected $configuration;
    /**
     * @var int
     */
    protected $total = 0;

    /**
     * @param EventDispatcherInterface $eventService
     * @param PurchasableHolderInterface $purchasableHolder
     * @param State $stateService
     * @param AdaptableConfigurationInterface $configuration
     * @throws \Exception when there is no purchasable_identifier in the configuration
     */
    public function __construct(
        EventDispatcherInterface $eventService,
        PurchasableHolderInterface $purchasableHolder,
        State $stateService,
        DataObjectHandlerInterface $dataObjectHandler,
        AdaptableConfigurationInterface $configuration
    ) {
        $this->eventService = $eventService;
        $this->purchasableHolder = $purchasableHolder;
        $this->stateService = $stateService;
        $this->dataObjectHandler = $dataObjectHandler;
        $this->configuration = $configuration;

        if (!$configuration->hasConfig('purchasable_identifier')) {
            throw new \Exception('Free Purchasables Result requires a purchasable_identifier configuration value');
        }
    }

    /**
     * @param Heystack\Subsystem\Core\Identifier\Identifier the deal handlers identifier
     */
    protected function saveState($identifier)
    {
        $this->stateService->setByKey(
            self::IDENTIFIER . $identifier,
            array(
                $this->total
            )
        );
    }

    /**
     * @param Heystack\Subsystem\Core\Identifier\Identifier the deal handler's identifier
     */
    protected function restoreState($identifier)
    {
        $data = $this->stateService->getByKey(self::IDENTIFIER . $identifier);
        if (is_array($data)) {
            list($this->total) = $data;
        }
    }

    /**
     * @return string
     */
    public function getDescription()
    {
        return 'Free Purchasable:' . $this->configuration->getConfig('purchasable_identifier');
    }

    /**
     * @param DealHandlerInterface $dealHandler
     * @return mixed
     */
    public function process(DealHandlerInterface $dealHandler)
    {
        $dealIdentifier = $dealHandler->getIdentifier()->getFull();
        $this->restoreState($dealIdentifier);

        $purchasable = $this->getPurchasable();

        if (!$this->processed($purchasable, $dealHandler)) {

            //Add the selected purchasable to the purchasableHolder
            $this->purchasableHolder->addPurchasable($purchasable);

            //Get the processed purchasable back from the purchasableHolder (could be a different object than the one passed in)
            $productHolderPurchasable = $this->purchasableHolder->getPurchasable($purchasable->getIdentifier());

            $this->total = $productHolderPurchasable->getUnitPrice();

            $this->saveState($dealIdentifier);

            $this->eventService->dispatch(Events::RESULT_PROCESSED);
        }

        return $this->total;
    }

    protected function processed(PurchasableInterface $purchasable, DealHandlerInterface $dealHandler)
    {
        $productHolderPurchasable = $this->purchasableHolder->getPurchasable($purchasable->getIdentifier());

        $conditions = $dealHandler->getConditions();

        $purchasableIdentifierFromCondition = null;

{'fraction_non_alphanumeric': 0.10373019928461931, 'fraction_numerical': 0.00020439448134900357, 'mean_word_length': 3.1981981981981984, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 76, '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': '133652', 'n_tokens_mistral': 2755, 'n_tokens_neox': 2676, 'n_words': 460}