Skip to content

Commit

Permalink
AddToCartAction - code style fixes, PHPDoc updates
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverFire committed Jan 18, 2016
1 parent 9e490cd commit 053f3e8
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions src/actions/AddToCartAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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'));
Expand All @@ -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());
}
}

0 comments on commit 053f3e8

Please sign in to comment.