ShippingEstimateForm::submit()   B
last analyzed

Complexity

Conditions 5
Paths 10

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
cc 5
eloc 16
nc 10
nop 2
1
<?php
2
3
/**
4
 * @package silvershop-shipping
5
 */
6
class ShippingEstimateForm extends Form
7
{
8
    public function __construct($controller, $name = "ShippingEstimateForm")
9
    {
10
        $address = new Address();  // get address to access it's getCountryField method
11
        $fields = new FieldList(
12
            $address->getCountryField(),
13
            TextField::create('State', _t('Address.db_State', 'State')),
14
            TextField::create('City', _t('Address.db_City', 'City')),
15
            TextField::create('PostalCode', _t('Address.db_PostalCode', 'Postal Code'))
16
        );
17
        $actions =  new FieldList(
18
            FormAction::create(
19
                "submit",
20
                _t('ShippingEstimateForm.FormActionTitle', 'Estimate')
21
            )
22
        );
23
        $validator = new RequiredFields(array(
24
            'Country'
25
        ));
26
        parent::__construct($controller, $name, $fields, $actions, $validator);
27
        $this->extend('updateForm');
28
    }
29
30
    public function submit($data, $form)
31
    {
32
        if ($country = SiteConfig::current_site_config()->getSingleCountry()) {  // Add Country if missing due to ReadonlyField in form
33
            $data['Country'] = $country;
34
        }
35
        if ($order = ShoppingCart::singleton()->current()) {
36
            $estimator = new ShippingEstimator(
37
                $order,
38
                new Address(Convert::raw2sql($data))
0 ignored issues
show
Bug introduced by
It seems like \Convert::raw2sql($data) targeting Convert::raw2sql() can also be of type string; however, DataObject::__construct() does only seem to accept array|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
39
            );
40
            $estimates = $estimator->getEstimates();
41
            if (!$estimates->exists()) {
42
                $form->sessionMessage(
43
                    _t('ShippingEstimateForm.FormActionWarningMessage', 'No estimates could be found for that location.'),
44
                    _t('ShippingEstimateForm.FormActionWarningCode', "warning")
45
                );
46
            }
47
            Session::set("ShippingEstimates", $estimates);
48
            if (Director::is_ajax()) {
49
                //TODO: replace with an AJAXResponse class that can output to different formats
50
                return json_encode($estimates->toNestedArray());
51
            }
52
        }
53
        $this->controller->redirectBack();
54
    }
55
}
56