ShippingCheckoutComponent   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 8
dl 0
loc 69
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getFormFields() 0 17 2
A getRequiredFields() 0 4 1
A validateData() 0 19 3
A getData() 0 9 2
A setData() 0 13 3
1
<?php
2
3
/**
4
 * @package silvershop-shipping
5
 */
6
class ShippingCheckoutComponent extends CheckoutComponent
7
{
8
    public function getFormFields(Order $order)
9
    {
10
        $fields = new FieldList();
11
        $estimates = $order->getShippingEstimates();
12
        if($estimates->exists()){
13
            $fields->push(
14
                OptionsetField::create(
15
                    "ShippingMethodID",
16
                    _t('ShippingCheckoutComponent.ShippingOptions', 'Shipping Options'),
17
                    $estimates->map(),
18
                    $estimates->First()->ID
19
                )
20
            );
21
        }
22
23
        return $fields;
24
    }
25
26
    public function getRequiredFields(Order $order)
27
    {
28
        return array();
29
    }
30
31
    public function validateData(Order $order, array $data)
32
    {
33
        $result = new ValidationResult();
34
        if (!isset($data['ShippingMethodID'])) {
35
            $result->error(
36
                _t('ShippingCheckoutComponent.ShippingMethodNotProvidedMessage', "Shipping method not provided"),
37
                _t('ShippingCheckoutComponent.ShippingMethodErrorCode', "ShippingMethod")
38
            );
39
            throw new ValidationException($result);
40
        }
41
42
        if (!ShippingMethod::get()->byID($data['ShippingMethodID'])) {
43
            $result->error(
44
                _t('ShippingCheckoutComponent.ShippingMethodDoesNotExistMessage', "Shipping Method does not exist"),
45
                _t('ShippingCheckoutComponent.ShippingMethodErrorCode', "ShippingMethod")
46
            );
47
            throw new ValidationException($result);
48
        }
49
    }
50
51
    public function getData(Order $order)
52
    {
53
        $estimates = $order->getShippingEstimates();
54
        $method = count($estimates) === 1 ? $estimates->First() : Session::get("Checkout.ShippingMethod");
55
56
        return array(
57
            'ShippingMethod' => $method
58
        );
59
    }
60
61
    public function setData(Order $order, array $data)
62
    {
63
        $option = null;
64
        if (isset($data['ShippingMethodID'])) {
65
            $option = ShippingMethod::get()
66
                ->byID((int)$data['ShippingMethodID']);
67
        }
68
        //assign option to order / modifier
69
        if ($option) {
70
            $order->setShippingMethod($option);
71
            Session::set("Checkout.ShippingMethod", $option);
72
        }
73
    }
74
}
75