From 053f3e8481bd82db81b0969243047e18b843c23d Mon Sep 17 00:00:00 2001 From: SilverFire - Dmitry Naumenko Date: Mon, 18 Jan 2016 19:08:59 +0200 Subject: [PATCH] AddToCartAction - code style fixes, PHPDoc updates --- src/actions/AddToCartAction.php | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/actions/AddToCartAction.php b/src/actions/AddToCartAction.php index 8b963f0..6c6606f 100644 --- a/src/actions/AddToCartAction.php +++ b/src/actions/AddToCartAction.php @@ -12,27 +12,39 @@ namespace hiqdev\yii2\cart\actions; use hiqdev\hiart\Collection; -use hiqdev\yii2\cart\Module; +use hiqdev\yii2\cart\CartPositionInterface; +use hiqdev\yii2\cart\Module as CartModule; use Yii; class AddToCartAction extends \yii\base\Action { + /** + * @var CartPositionInterface The class for new product + */ public $productClass; + /** + * @var boolean whether the action expects bulk models load using `selection` + */ public $bulkLoad = false; - public function getModule() + /** + * Returns the cart module + * @return CartModule + */ + public function getCartModule() { - return Module::getInstance(); + return CartModule::getInstance(); } public function run() { $data = null; - $cart = $this->getModule()->getCart(); + $cart = $this->getCartModule()->getCart(); $request = Yii::$app->request; - $model = new $this->productClass(); - $collection = new Collection(); + /** @var CartPositionInterface $model */ + $model = Yii::createObject($this->productClass); + $collection = new Collection(); // TODO: drop dependency $collection->setModel($model); if ($this->bulkLoad) { @@ -47,6 +59,7 @@ public function run() if ($collection->load($data) && $collection->validate()) { foreach ($collection->models as $position) { + /** @var CartPositionInterface $position */ if (!$cart->hasPosition($position->getId())) { $cart->put($position); Yii::$app->session->addFlash('success', Yii::t('cart', 'Item has been added to cart')); @@ -55,13 +68,14 @@ public function run() } } } else { - Yii::$app->session->addFlash('warning', Yii::t('cart', 'Item does not exists')); + Yii::$app->session->addFlash('warning', Yii::t('cart', 'Failed to add item to the cart')); + Yii::warning('Failed to add item to the cart', 'cart'); } if ($request->isAjax) { Yii::$app->end(); - } else { - return $this->controller->redirect($request->referrer); } + + return $this->controller->redirect($request->referrer ?: $this->controller->goHome()); } }