<commit_before><?php

namespace jamesdb\Cart;

use jamesdb\Cart\CartItem;
use jamesdb\Cart\Event as CartEvent;
use jamesdb\Cart\Exception as CartException;
use jamesdb\Cart\Storage\StorageInterface;
use League\Event\Emitter;
use SebastianBergmann\Money\Currency;
use SebastianBergmann\Money\Money;

class Cart
{
    /**
     * @var string
     */
    protected $identifier;

    /**
     * @var \jamesdb\Cart\Storage\StorageInterface
     */
    protected $storage;

    /**
     * Cart Contents.
     *
     * @var array
     */
    protected $contents = [];

    /**
     * Currency.
     *
     * @var \SebastianBergmann\Money\Currency
     */
    protected $currency;

    /**
     * Event Emitter.
     *
     * @var \League\Event\Emitter
     */
    protected $eventEmitter;

    /**
     * Constructor.
     *
     * @param string                                 $identifier
     * @param \jamesdb\Cart\Storage\StorageInterface $storage
     */
    public function __construct($identifier, StorageInterface $storage)
    {
        $this->identifier   = $identifier;
        $this->storage      = $storage;
        $this->currency     = $this->currency ?: new Currency('GBP');
        $this->eventEmitter = $this->eventEmitter ?: new Emitter();

        $this->restore();
    }

    /**
     * Set the Currency.
     *
     * @param \SebastianBergmann\Money\Currency $currency
     */
    public function setCurrency(Currency $currency)
    {
        $this->currency = $currency;
    }

    /**
     * Return the Currency.
     *
     * @return \SebastianBergmann\Money\Currency $currency
     */
    public function getCurrency()
    {
        return $this->currency;
    }

    /**
     * Add an Event Listener to the Emitter.
     *
     * @param  string          $eventName
     * @param  callable|object $listener
     *
     * @return void
     */
    public function addEventListener($eventName, $listener)
    {
        $this->eventEmitter->addListener($eventName, $listener);
    }

    /**
     * Returns the Event Emitter.
     *
     * @return \League\Event\Emitter
     */
    public function getEventEmitter()
    {
        return $this->eventEmitter;
    }

    /**
     * Add an item.
     *
     * @param  \jamesdb\Cart\CartItem $item
     *
     * @return string
     */
    public function add(CartItem $item)
    {
        $rowId = $item->getRowId();

        if ($row = $this->getItem($rowId)) {
            $row->quantity += $item->quantity;
        } else {
            $this->contents[$rowId] = $item;
        }

        $this->storage->store($this->identifier, serialize($this->toArray()));

        $this->getEventEmitter()->emit(new CartEvent\CartItemAddEvent($this, $item));

        return $rowId;
    }

    /**
     * Remove an item.
     *
     * @param  string $rowId
     *
     * @throws \jamesdb\Cart\Exception\CartRemoveItemException
     *
     * @return boolean
     */
    public function remove($rowId)
    {
        $item = $this->getItem($rowId);

        if ($item === null) {
            throw new CartException\CartItemRemoveException(
                sprintf('No such item with rowid (%s).', $rowId)
            );
        }

        unset($this->contents[$rowId]);

        $this->storage->store($this->identifier, serialize($this->contents));

        $this->getEventEmitter()->emit(new CartEvent\CartItemRemoveEvent($this, $item));

        return true;
    }

    /**
     * Update an item stored in the Cart.
     *
     * @param  string $rowId
     * @param  array  $data
     *
     * @throws \jamesdb\Cart\Exception\CartUpdateException
     *
     * @return boolean
     */
    public function update($rowId, array $data = [])
    {
        $row = $this->getItem($rowId);

        if ($row === null) {
            throw new CartException\CartItemUpdateException(
                sprintf('Could not update item (%s).', $rowId)
            );
        }

        foreach ($data as $key => $value) {
            $row->{$key} = $value;
        }

        $this->getEventEmitter()->emit(new CartEvent\CartItemUpdateEvent($this, $row));

        return true;
    }

    /**
     * Empty the cart.
     *
     * @return void
     */
    public function clear()
    {
        $this->storage->clear($this->identifier);

        $this->contents = [];
    }

    /**
     * Get a specific item from the cart.
     *
     * @param  string $rowId
     *
     * @return array|null
     */
    public function getItem($rowId)
    {
        if (array_key_exists($rowId, $this->contents)) {<commit_msg>Update Cart.php, use CurrencyAwareInterface and CurrencyAwareTrait<commit_after><?php

namespace jamesdb\Cart;

use jamesdb\Cart\CartItem;
use jamesdb\Cart\Event as CartEvent;
use jamesdb\Cart\Exception as CartException;
use jamesdb\Cart\Storage\StorageInterface;
use League\Event\Emitter;
use SebastianBergmann\Money\Currency;
use SebastianBergmann\Money\Money;

class Cart implements CurrencyAwareInterface
{
    use CurrencyAwareTrait;

    /**
     * @var string
     */
    protected $identifier;

    /**
     * @var \jamesdb\Cart\Storage\StorageInterface
     */
    protected $storage;

    /**
     * Cart Contents.
     *
     * @var array
     */
    protected $contents = [];

    /**
     * Event Emitter.
     *
     * @var \League\Event\Emitter
     */
    protected $eventEmitter;

    /**
     * Constructor.
     *
     * @param string                                 $identifier
     * @param \jamesdb\Cart\Storage\StorageInterface $storage
     */
    public function __construct($identifier, StorageInterface $storage)
    {
        $this->identifier   = $identifier;
        $this->storage      = $storage;
        $this->currency     = $this->currency ?: new Currency('GBP');
        $this->eventEmitter = $this->eventEmitter ?: new Emitter();

        $this->restore();
    }

    /**
     * Add an Event Listener to the Emitter.
     *
     * @param  string          $eventName
     * @param  callable|object $listener
     *
     * @return void
     */
    public function addEventListener($eventName, $listener)
    {
        $this->eventEmitter->addListener($eventName, $listener);
    }

    /**
     * Returns the Event Emitter.
     *
     * @return \League\Event\Emitter
     */
    public function getEventEmitter()
    {
        return $this->eventEmitter;
    }

    /**
     * Add an item.
     *
     * @param  \jamesdb\Cart\CartItem $item
     *
     * @return string
     */
    public function add(CartItem $item)
    {
        $rowId = $item->getRowId();

        if ($row = $this->getItem($rowId)) {
            $row->quantity += $item->quantity;
        } else {
            $this->contents[$rowId] = $item;
        }

        $this->storage->store($this->identifier, serialize($this->toArray()));

        $this->getEventEmitter()->emit(new CartEvent\CartItemAddEvent($this, $item));

        return $rowId;
    }

    /**
     * Remove an item.
     *
     * @param  string $rowId
     *
     * @throws \jamesdb\Cart\Exception\CartRemoveItemException
     *
     * @return boolean
     */
    public function remove($rowId)
    {
        $item = $this->getItem($rowId);

        if ($item === null) {
            throw new CartException\CartItemRemoveException(
                sprintf('No such item with rowid (%s).', $rowId)
            );
        }

        unset($this->contents[$rowId]);

        $this->storage->store($this->identifier, serialize($this->contents));

        $this->getEventEmitter()->emit(new CartEvent\CartItemRemoveEvent($this, $item));

        return true;
    }

    /**
     * Update an item stored in the Cart.
     *
     * @param  string $rowId
     * @param  array  $data
     *
     * @throws \jamesdb\Cart\Exception\CartUpdateException
     *
     * @return boolean
     */
    public function update($rowId, array $data = [])
    {
        $row = $this->getItem($rowId);

        if ($row === null) {
            throw new CartException\CartItemUpdateException(
                sprintf('Could not update item (%s).', $rowId)
            );
        }

        foreach ($data as $key => $value) {
            $row->{$key} = $value;
        }

        $this->getEventEmitter()->emit(new CartEvent\CartItemUpdateEvent($this, $row));

        return true;
    }

    /**
     * Clear the cart.
     *
     * @return void
     */
    public function clear()
    {
        $this->storage->clear($this->identifier);

        $this->contents = [];
    }

    /**
     * Get a specific item from the cart.
     *
     * @param  string $rowId
     *
     * @return array|null
     */
    public function getItem($rowId)
    {
        if (array_key_exists($rowId, $this->contents)) {
{'fraction_non_alphanumeric': 0.13809359549269865, 'fraction_numerical': 0.0, 'mean_word_length': 1.9003001000333444, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6906488', 'n_tokens_mistral': 2646, 'n_tokens_neox': 2603, 'n_words': 519}