ShippingEstimateForm   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 12
dl 0
loc 50
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 1
B submit() 0 25 5
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