Completed
Push — master ( 0bd97d...00f789 )
by Klochok
11:44
created

src/actions/AddToCartAction.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * Cart module for Yii2
5
 *
6
 * @link      https://github.com/hiqdev/yii2-cart
7
 * @package   yii2-cart
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\yii2\cart\actions;
13
14
use hipanel\modules\finance\cart\AbstractCartPosition;
15
use hiqdev\hiart\Collection;
16
use hiqdev\yii2\cart\CartPositionInterface;
17
use hiqdev\yii2\cart\Module as CartModule;
18
use Yii;
19
20
class AddToCartAction extends \yii\base\Action
21
{
22
    /**
23
     * @var CartPositionInterface The class for new product
24
     */
25
    public $productClass;
26
27
    /**
28
     * @var boolean whether the action expects bulk models load using `selection`
29
     */
30
    public $bulkLoad = false;
31
32
    /**
33
     * @var bool whether client should be redirected to the cart in case of success item adding
34
     */
35
    public $redirectToCart = false;
36
37
    /**
38
     * @var bool whether any errors occurred during save
39
     */
40
    protected $hasErrors = false;
41
42
    /**
43
     * Returns the cart module.
44
     * @return CartModule
45
     */
46
    public function getCartModule()
47
    {
48
        return CartModule::getInstance();
49
    }
50
51
    public function run()
52
    {
53
        $data = null;
54
        $request = Yii::$app->request;
55
        /** @var CartPositionInterface $model */
56
        $model = Yii::createObject($this->productClass);
57
        $collection = new Collection(); // TODO: drop dependency
58
        $collection->setModel($model);
59
60
        if (!$this->bulkLoad) {
61
            $data = [$request->post() ?: $request->get()];
62
            $collection->load($data);
63
        } else {
64
            $collection->load();
65
        }
66
67
        foreach ($collection->models as $position) {
68
            /** @var CartPositionInterface|AbstractCartPosition $position */
69
            if (!$position->validate()) {
70
                $this->hasErrors = true;
71
                $error = $collection->getFirstError();
72
                if (empty($error)) {
73
                    $error = Yii::t('cart', 'Failed to add item to the cart');
74
                }
75
                Yii::$app->session->addFlash('warning', $error);
76
                Yii::warning('Failed to add item to cart', 'cart');
77
78
                continue;
79
            }
80
        }
81
82
        $this->putPositionsToCart($collection->models);
83
    }
84
85
    protected function putPositionsToCart($positions)
86
    {
87
        $cart = $this->getCartModule()->getCart();
88
        $cart->putPositions($positions);
89
    }
90
91
    protected function afterRun()
92
    {
93
        $this->ensureBehaviors();
94
        if ($this->hasEventHandlers('afterAction')) {
95
            return true;
96
        }
97
98
        $request = Yii::$app->request;
99
100
        if ($request->isAjax) {
101
            Yii::$app->end();
102
        }
103
104
        if ($this->redirectToCart && !$this->hasErrors) {
105
            return $this->controller->redirect('@cart');
0 ignored issues
show
The method redirect does only exist in yii\web\Controller, but not in yii\base\Controller.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
106
        } elseif (isset($request->referrer)) {
107
            return $this->controller->redirect($request->referrer);
108
        } else {
109
            return $this->controller->goHome();
0 ignored issues
show
The method goHome does only exist in yii\web\Controller, but not in yii\base\Controller.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
110
        }
111
    }
112
}
113