Completed
Pull Request — master (#293)
by Nic
07:28
created

DonationProduct::getCMSFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
crap 2
1
<?php
2
3
class DonationProduct extends ProductPage
4
{
5
    /**
6
     * @var string
7
     */
8
    private static $singular_name = 'Donation';
9
10
    /**
11
     * @var string
12
     */
13
    private static $plural_name = 'Donations';
14
15
    /**
16
     * @var array
17
     */
18
    private static $allowed_children = [];
0 ignored issues
show
Unused Code introduced by
The property $allowed_children is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
19
20
    /**
21
     * @return FieldList
22
     */
23
    public function getCMSFields()
24
    {
25
        $fields = parent::getCMSFields();
26
27
        $fields->removeByName([
28
            'Weight',
29
            'Price',
30
        ]);
31
32
        return $fields;
33
    }
34
}
35
36
class DonationProduct_Controller extends ProductPage_Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
37
{
38
39
    /**
40
     * @var array
41
     */
42
    private static $allowed_actions = [
43
        'PurchaseForm',
44
        'updatevalue',
45
    ];
46
47
    /**
48
     *
49
     */
50
    public function init()
51
    {
52
        parent::init();
53
        Requirements::javascript("framework/thirdparty/jquery/jquery.js");
54
    }
55
56
    /**
57
     * @return FoxyStripePurchaseForm
58
     */
59
    public function PurchaseForm()
60
    {
61
        $form = parent::PurchaseForm();
62
63
        $fields = $form->Fields();
64
65
        $fields->insertBefore($currencyField = CurrencyField::create('price', 'Amount'), 'quantity');
66
67
        $fields->removeByName(array(
68
            'quantity',
69
        ));
70
71
        if (SiteConfig::current_site_config()->CartValidation) {
72
            Requirements::javascript("framework/thirdparty/jquery-validate/jquery.validate.js");
73
            Requirements::javascriptTemplate("foxystripe/javascript/donationProduct.js", [
0 ignored issues
show
Documentation introduced by
array('Trigger' => (stri...->Link('updatevalue'))) is of type array<string,string|fals...teURL":"false|string"}>, but the function expects a array<integer,string|integer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
                'Trigger' => (string)$currencyField->getAttribute('id'),
75
                'UpdateURL' => Director::absoluteURL($this->Link('updatevalue')),
76
            ]);
77
        }
78
79
        return $form;
80
    }
81
82
    /**
83
     * create new encrypted price value based on user input
84
     *
85
     * @param $request
86
     * @return string|SS_HTTPResponse
87
     */
88
    public function updatevalue($request)
89
    {
90
91
        if ($price = $request->getVar('Price') && SiteConfig::current_site_config()->CartValidation) {
0 ignored issues
show
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: $price = ($request->getV...nfig()->CartValidation), Probably Intended Meaning: ($price = $request->getV...onfig()->CartValidation
Loading history...
92
            $signedPrice = FoxyCart_Helper::fc_hash_value($this->Code, 'price', $price, 'name', false);
0 ignored issues
show
Documentation introduced by
$price is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
93
            $json = json_encode(['Price' => $signedPrice]);
94
            $response = new SS_HTTPResponse($json);
95
            $response->removeHeader('Content-Type');
96
            $response->addHeader('Content-Type', 'application/json');
97
            return $response;
98
        }
99
100
        return 'false';
101
    }
102
}