QuantityCell   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 44
ccs 0
cts 27
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 5 1
A run() 0 8 1
A isSelectMode() 0 4 2
A registerClientScript() 0 10 1
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\widgets;
13
14
class QuantityCell extends \yii\base\Widget
15
{
16
    const MODE_SELECT = 'select';
17
18
    const MODE_NUMBER = 'number';
19
20
    public $model;
21
22
    public $type;
23
24
    public function init()
25
    {
26
        parent::init();
27
        $this->registerClientScript();
28
    }
29
30
    public function run()
31
    {
32
        return $this->render('QuantityCell_view', [
33
            'view'   => $this->getView(),
34
            'model'  => $this->model,
35
            'widget' => $this,
36
        ]);
37
    }
38
39
    /**
40
     * Select is default mode.
41
     */
42
    public function isSelectMode()
43
    {
44
        return $this->type !== self::MODE_NUMBER && method_exists($this->model, 'getQuantityOptions');
45
    }
46
47
    public function registerClientScript()
48
    {
49
        $this->getView()->registerJs(<<<JS
50
            jQuery(document).on('change', '.quantity-field', function(evnet) {
51
                var form = jQuery(this).parents('form').get(0);
52
                $(form).submit();
53
            });
54
JS
55
        );
56
    }
57
}
58