QuantityField::getProduct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Dynamic\Foxy\Form;
4
5
use Dynamic\Foxy\Model\Setting;
6
use Dynamic\Products\Page\Product;
0 ignored issues
show
Bug introduced by
The type Dynamic\Products\Page\Product was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use SilverStripe\Control\Controller;
8
use SilverStripe\Control\HTTPRequest;
9
use SilverStripe\Forms\NumericField;
10
use SilverStripe\View\Requirements;
11
12
/**
13
 * Class QuantityField
14
 * @package Dynamic\FoxyStripe\Form
15
 */
16
class QuantityField extends NumericField
17
{
18
    /**
19
     * @var array
20
     */
21
    private static $allowed_actions = [
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
22
        'newvalue',
23
    ];
24
25
    /**
26
     * @param array $properties
27
     * @return string
28
     */
29
    public function Field($properties = [])
30
    {
31
        Requirements::javascript('silverstripe/admin: thirdparty/jquery/jquery.min.js');
32
        Requirements::javascript('dynamic/silverstripe-foxy: client/dist/javascript/quantity.js');
33
        Requirements::css('dynamic/silverstripe-foxy: client/dist/css/quantityfield.css');
34
35
        $this->setAttribute('data-link', $this->Link('newvalue'));
36
        $this->setAttribute('data-code', $this->getProduct()->Code);
37
        $this->setAttribute('data-id', $this->getProduct()->ID);
38
        $this->setAttribute('id', 'quantity-toggle-field');
39
        if ($this->getProduct()->QuantityMax > 0) {
40
            $this->setAttribute('data-limit', $this->getProduct()->QuantityMax);
41
        }
42
43
        return parent::Field($properties);
44
    }
45
46
    /**
47
     * @return Product
48
     */
49
    public function getProduct()
50
    {
51
        return $this->getForm()->getProduct();
52
    }
53
54
    /**
55
     * @param HTTPRequest $request
56
     * @return bool|string
57
     */
58
    public function newvalue(HTTPRequest $request)
59
    {
60
        $value = $request->getVar('value');
61
        if (!$value && $value != 0) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $value of type mixed|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
62
            return '';
63
        }
64
65
        if (!$code = $request->getVar('code')) {
66
            return '';
67
        }
68
69
        $this->extend('updateQuantity', $value);
70
71
        $data = [
72
            'quantity' => $value,
73
            'quantityGenerated' => AddToCartForm::getGeneratedValue($code, 'quantity', $value, 'value'),
74
        ];
75
76
        if ($this->getProduct()->QuantityMax > 0) {
77
            $data['limit'] = $this->getProduct()->QuantityMax;
78
        }
79
80
        $this->extend('updateData', $data);
81
82
        return json_encode($data);
83
    }
84
}
85