Skip to content
yanickrochon edited this page Jun 6, 2012 · 2 revisions

Cart Service

<?php

namespace SpeckCart\Service;

use SpeckCart\Model\Cart,
    SpeckCart\Model\CartItem;

interface CartServiceInterface
{
    public function addItemToCart(Cart $cart, CartItem $item);
}

Events

  • cart.addItem
    • Parameters: cart, item
  • cart.removeItem
    • Parameters: cart, item

Questions

  • How do we discourage interacting directly with the model classes? $cartModel->addItem($item); vs $cartService->addItemToCart($cart, $item);.

    • Answer : Assuming that there is only one cart instance per session, the cart service should not take responsibility of receiving an instance of the cart, and would act as an instance holder.

      interface CartServiceInterface
      {
          /**
           * @return Cart
           */
          public function getCart();
      }

      Then the cart would be responsible to send events (items added, removed, modified, quantities, etc.) --yanick

Cart Model

<?php

namespace SpeckCart\Model;

interface Cart
{
    /**
     * Add an item to the cart
     * 
     * @param CartItem $item
     * @return Cart
     */
    public function addItem(CartItem $item);

    /**
     * Remove an item from the cart
     *
     * @param int $itemId
     * @return Cart
     */
    public function removeItem($itemId);
}

CartItem Model

<?php

namespace SpeckCart\Model;

use \DateTime;

interface CartItem
{
    /**
     * Get the ID for this cart item. 
     * 
     * @return int
     */
    public function getCartItemId();

    /**
     * Set the ID for this cart item. 
     * 
     * @param int $cartItemId 
     * @return CartItem
     */
    public function setCartItemId($cartItemId);

    /**
     * Get the cart ID that this item belongs to.
     * 
     * @return int
     */
    public function getCartId();

    /**
     * Set the cart ID that this item belongs to. 
     * 
     * @param int $cartId 
     * @return CartItem
     */
    public function setCartId($cartId);

    /**
     * Returns performance indicators for querying this item.
     * 
     * @return array
     */
    public function getPerformanceIndicators();

    /**
     * Set the performance indicators for querying this item. 
     * 
     * @param array $indicators 
     * @return CartItem
     */
    public function setPerformanceIndicators(array $indicators);

    /**
     * Add a performance indicator for querying this item. 
     * 
     * @param string $indicator 
     * @return CartItem
     */
    public function addPerformanceIndicator($indicator);

    /**
     * Get the price of this item 
     * 
     * @return float
     */
    public function getPrice();

    /**
     * Set the price of this item 
     * 
     * @return CartItem
     */
    public function setPrice($price);

    /**
     * Get the quantity of this item 
     * 
     * @return int
     */
    public function getQuantity();

    /**
     * Set the quantity of this item 
     * 
     * @param int $quantity 
     * @return CartItem
     */
    public function setQuantity($quantity);

    /**
     * Get the DateTime that this item was added to the cart 
     * 
     * @return DateTime
     */
    public function getAddedTime();

    /**
     * Set the DateTime that this item was added to the cart 
     * 
     * @param DateTime $addedTime 
     * @return CartItem
     */
    public function setAddedTime(DateTime $addedTime);

    /**
     * Get the extended price for this item.
     * 
     * @return float
     */
    public function getExtPrice();
}