CartGrid::init()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace yii2mod\cart\widgets;
4
5
use Yii;
6
use yii\base\Widget;
7
use yii\data\ArrayDataProvider;
8
use yii\helpers\ArrayHelper;
9
use yii2mod\cart\Cart;
10
11
/**
12
 * Class Cart
13
 *
14
 * @package yii2mod\cart\widgets
15
 */
16
class CartGrid extends Widget
17
{
18
    /**
19
     * @var \yii\data\BaseDataProvider
20
     */
21
    public $cartDataProvider;
22
23
    /**
24
     * @var array GridView columns
25
     */
26
    public $cartColumns = ['id', 'label'];
27
28
    /**
29
     * @var array GridView options
30
     */
31
    public $gridOptions = [];
32
33
    /**
34
     * @var string Only items of that type will be rendered. Defaults to Cart::ITEM_PRODUCT
35
     */
36
    public $itemType = Cart::ITEM_PRODUCT;
37
38
    /**
39
     * @inheritdoc
40
     */
41
    public function init()
42
    {
43
        $cart = Yii::$app->get('cart');
44
45
        if (!isset($this->cartDataProvider)) {
46
            $this->cartDataProvider = new ArrayDataProvider([
47
                'allModels' => $cart->getItems($this->itemType),
48
                'pagination' => false,
49
            ]);
50
        }
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56
    public function run()
57
    {
58
        return $this->render('cart', [
59
            'gridOptions' => $this->getGridOptions(),
60
        ]);
61
    }
62
63
    /**
64
     * Return grid options
65
     *
66
     * @return array
67
     */
68
    public function getGridOptions(): array
69
    {
70
        return ArrayHelper::merge($this->gridOptions, [
71
            'dataProvider' => $this->cartDataProvider,
72
            'columns' => $this->cartColumns,
73
        ]);
74
    }
75
}
76