Completed
Pull Request — 2.0 (#506)
by Roman
18:39
created

AddressCheckoutComponent   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 81.82%

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 23
c 3
b 2
f 0
lcom 1
cbo 8
dl 0
loc 140
ccs 63
cts 77
cp 0.8182
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getFormFields() 0 8 1
A getRequiredFields() 0 4 1
A validateData() 0 3 1
B getData() 0 36 4
F setData() 0 48 13
A setShowFormFieldDescriptions() 0 4 1
A setAddToAddressBook() 0 4 1
A getAddress() 0 4 1
1
<?php
2
3
abstract class AddressCheckoutComponent extends CheckoutComponent
4
{
5
    protected $formfielddescriptions = true;
6
7
    protected $addresstype;
8
9
    protected $addtoaddressbook      = false;
10
11 2
    public function getFormFields(Order $order)
12
    {
13 2
        return $this->getAddress($order)->getFrontEndFields(
14
            array(
15 2
                'addfielddescriptions' => $this->formfielddescriptions,
16
            )
17 2
        );
18
    }
19
20 4
    public function getRequiredFields(Order $order)
21
    {
22 4
        return $this->getAddress($order)->getRequiredFields();
23
    }
24
25 2
    public function validateData(Order $order, array $data)
26
    {
27 2
    }
28
29 2
    public function getData(Order $order)
30
    {
31 2
        $data = $this->getAddress($order)->toMap();
32
33
        //merge data from multiple sources
34 2
        $data = array_merge(
35 2
            ShopUserInfo::singleton()->getLocation(),
36 2
            $data,
37
            array(
38 2
                $this->addresstype . "AddressID" => $order->{$this->addresstype . "AddressID"},
39
            )
40 2
        );
41
42
        //merge in default address if an address isn't available
43 2
        $member = Member::currentUser();
44 2
        if(!$order->{$this->addresstype . "AddressID"}) {
45
            $data = array_merge(
46
                ShopUserInfo::singleton()->getLocation(),
47
                $member ? $member->{"Default" . $this->addresstype . "Address"}()->toMap() : array(),
48
                array(
49
                    $this->addresstype . "AddressID" => $order->{$this->addresstype . "AddressID"},
50
                )
51
            );
52
        }
53
54 2
        unset($data['ID']);
55 2
        unset($data['ClassName']);
56 2
        unset($data['RecordClassName']);
57
58
        //ensure country is restricted if there is only one allowed country
59 2
        if ($country = SiteConfig::current_site_config()->getSingleCountry()) {
60 2
            $data['Country'] = $country;
61 2
        }
62
63 2
        return $data;
64
    }
65
66
    /**
67
     * Create a new address if the existing address has changed, or is not yet
68
     * created.
69
     *
70
     * @param Order $order order to get addresses from
71
     * @param array $data  data to set
72
     */
73 2
    public function setData(Order $order, array $data)
0 ignored issues
show
Complexity introduced by
This operation has 600 execution paths which exceeds the configured maximum of 200.

A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.

You can also find more information in the “Code” section of your repository.

Loading history...
74
    {
75 2
        $address = $this->getAddress($order);
76
        //if the value matches the current address then unset
77
        //this is to fix issues with blank fields & the readonly Country field
78 2
        $addressfields = Address::database_fields(get_class($address));
79 2
        foreach($data as $key => $value) {
80 2
            if(!isset($addressfields[$key]) || (!$value && !$address->{$key})) {
81 2
                unset($data[$key]);
82 2
            }
83 2
        }
84 2
        $address->update($data);
85
        //if only one country is available, then set it
86 2
        if ($country = SiteConfig::current_site_config()->getSingleCountry()) {
87 2
            $address->Country = $country;
88 2
        }
89
        //write new address, or duplicate if changed
90 2
        if (!$address->isInDB()) {
91 2
            $address->write();
92 2
        } elseif ($address->isChanged()) {
93 1
            $address = $address->duplicate();
94 1
        }
95
        //set billing address, if not already set
96 2
        $order->{$this->addresstype . "AddressID"} = $address->ID;
97 2
        if (!$order->BillingAddressID) {
98 1
            $order->BillingAddressID = $address->ID;
99 1
        }
100 2
        $order->write();
101
        //update user info based on shipping address
102 2
        if ($this->addresstype === "Shipping") {
103 2
            ShopUserInfo::singleton()->setAddress($address);
104 2
            Zone::cache_zone_ids($address);
105 2
        }
106
        //associate member to address
107 2
        if ($member = Member::currentUser()) {
108 2
            $default = $member->{"Default" . $this->addresstype . "Address"}();
109
            //set default address
110 2
            if (!$default->exists()) {
111 2
                $member->{"Default" . $this->addresstype . "AddressID"} = $address->ID;
112 2
                $member->write();
113 2
            }
114 2
            if ($this->addtoaddressbook) {
115
                $member->AddressBook()->add($address);
116
            }
117 2
        }
118
        //extension hooks
119 2
        $order->extend('onSet' . $this->addresstype . 'Address', $address);
120 2
    }
121
122
    /**
123
     * Enable adding form field descriptions
124
     */
125
    public function setShowFormFieldDescriptions($show = true)
126
    {
127
        $this->formfielddescriptions = $show;
128
    }
129
130
    /**
131
     * Add new addresses to the address book.
132
     */
133
    public function setAddToAddressBook($add = true)
134
    {
135
        $this->addtoaddressbook = $add;
136
    }
137
138 4
    public function getAddress(Order $order)
139 4
    {
140 4
        return $order->{$this->addresstype . "Address"}();
141
    }
142
}
143
144
class ShippingAddressCheckoutComponent extends AddressCheckoutComponent
145
{
146
    protected $addresstype = "Shipping";
147
}
148
149
class BillingAddressCheckoutComponent extends AddressCheckoutComponent
150
{
151
    protected $addresstype = "Billing";
152
}
153