/** * Receives a notification from a publisher object * * @param MW_Observer_Publisher_Interface $order Shop basket instance implementing publisher interface * @param string $action Name of the action to listen for * @param mixed $value Object or value changed in publisher */ public function update(MW_Observer_Publisher_Interface $order, $action, $value = null) { $context = $this->_getContext(); $context->getLogger()->log(__METHOD__ . ': event=' . $action, MW_Logger_Abstract::DEBUG); $class = 'MShop_Order_Item_Base_Interface'; if (!$order instanceof $class) { $msg = 'Received notification from "%1$s" which doesn\'t implement "%2$s"'; throw new MShop_Plugin_Exception(sprintf($msg, get_class($order), $class)); } if (self::$_lock === false) { self::$_lock = true; $couponManager = MShop_Factory::createManager($context, 'coupon'); $search = $couponManager->createSearch(); foreach ($order->getCoupons() as $code => $products) { $search->setConditions($search->compare('==', 'coupon.code.code', $code)); $results = $couponManager->searchItems($search); if (($couponItem = reset($results)) !== false) { $couponProvider = $couponManager->getProvider($couponItem, $code); $couponProvider->updateCoupon($order); } } self::$_lock = false; } return true; }
/** * Receives a notification from a publisher object * * @param MW_Observer_Publisher_Interface $order Shop basket instance implementing publisher interface * @param string $action Name of the action to listen for * @param mixed $value Object or value changed in publisher */ public function update(MW_Observer_Publisher_Interface $order, $action, $value = null) { $class = 'MShop_Order_Item_Base_Interface'; if (!$order instanceof $class) { throw new MShop_Plugin_Exception(sprintf('Object is not of required type "%1$s"', $class)); } $notAvailable = array(); if (self::$_lock === false) { self::$_lock = true; $couponManager = MShop_Factory::createManager($this->_getContext(), 'coupon'); foreach ($order->getCoupons() as $code => $products) { $search = $couponManager->createSearch(true); $expr = array($search->compare('==', 'coupon.code.code', $code), $search->getConditions()); $search->setConditions($search->combine('&&', $expr)); $search->setSlice(0, 1); $results = $couponManager->searchItems($search); if (($couponItem = reset($results)) !== false) { $couponProvider = $couponManager->getProvider($couponItem, $code); $couponProvider->updateCoupon($order); } else { $notAvailable[$code] = 'coupon.gone'; } } self::$_lock = false; } if (count($notAvailable) > 0) { $codes = array('coupon' => $notAvailable); $msg = sprintf('Coupon in basket is not available any more'); throw new MShop_Plugin_Provider_Exception($msg, -1, null, $codes); } return true; }
public function testUpdateInvalidObject() { $object = new MShop_Plugin_Provider_Order_Coupon(TestHelper::getContext(), $this->_plugin); $this->setExpectedException('MShop_Plugin_Exception'); $object->update(new MShop_Publisher_Test(), 'test'); }