GatewayFieldsFactory::getBillingFields()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 14
Ratio 100 %
Metric Value
dl 14
loc 14
rs 9.4285
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
3
/**
4
 * Helper for generating gateway fields, based on best practices.
5
 *
6
 * @package payment
7
 */
8
class GatewayFieldsFactory{
9
10
	protected $fieldgroups = array(
11
		'Card',
12
		'Billing',
13
		'Shipping',
14
		'Company',
15
		'Email'
16
	);
17
18
	protected $gateway;
19
	protected $groupdatefields = true;
20
21
	public function __construct($gateway = null, $fieldgroups = null) {
22
		$this->gateway = $gateway;
23
		$this->setFieldGroups($fieldgroups);
24
	}
25
26
	public function setFieldGroups($groups) {
27
		if (is_array($groups)) {
28
			$this->fieldgroups = $groups;
29
		}
30
31
		return $this;
32
	}
33
34
	public function setGateway($gateway) {
35
		$this->gateway = $gateway;
36
37
		return $this;
38
	}
39
40
	public function getFields() {
41
		$fields = new FieldList();
42
		foreach ($this->fieldgroups as $group) {
43
			if (method_exists($this, "get".$group."Fields")) {
44
				$fields->merge($this->{"get".$group."Fields"}());
45
			}
46
		}
47
48
		return $fields;
49
	}
50
51
	public function getCardFields() {
52
		$months = array();
53
		//generate list of months
54
		for ($x = 1; $x <= 12; $x++) {
55
			$months[$x] = date('m - F', mktime(0, 0, 0, $x, 1));
56
		}
57
		$year = date("Y");
58
		$range = 5;
59
		$startrange = range(date("Y", strtotime("-$range years")), $year);
60
		$expiryrange = range($year, date("Y", strtotime("+$range years")));
61
62
		$fields = array(
63
			"type" => DropdownField::create('type', _t("PaymentForm.TYPE", "Type"), $this->getCardTypes()),
64
			"name" => TextField::create('name', _t("PaymentForm.NAME", "Name on Card")),
65
			"number" => TextField::create('number', _t("PaymentForm.NUMBER", "Card Number"))
66
							->setDescription(_t("PaymentForm.NUMBERDESCRIPTION", "no dashes or spaces")),
67
			"startMonth" => DropdownField::create('startMonth', _t("PaymentForm.STARTMONTH", "Month"), $months),
68
			"startYear" => DropdownField::create('startYear', _t("PaymentForm.STARTYEAR", "Year"),
69
								array_combine($startrange, $startrange), $year
70
							),
71
			"expiryMonth" => DropdownField::create('expiryMonth', _t("PaymentForm.EXPIRYMONTH", "Month"), $months),
72
			"expiryYear" => DropdownField::create('expiryYear', _t("PaymentForm.EXPIRYYEAR", "Year"),
73
								array_combine($expiryrange, $expiryrange), $year
74
							),
75
			"cvv" => TextField::create('cvv', _t("PaymentForm.CVV", "Security Code"))
76
							->setMaxLength(5),
77
			"issueNumber" => TextField::create('issueNumber', _t("PaymentForm.ISSUENUMBER", "Issue Number"))
78
		);
79
		
80
		$this->cullForGateway($fields);
81
		//optionally group date fields
82
		if ($this->groupdatefields) {
83 View Code Duplication
			if (isset($fields['startMonth']) && isset($fields['startYear'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

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

Loading history...
84
				$fields['startMonth'] = new FieldGroup(_t("PaymentForm.START", "Start"),
85
					$fields['startMonth'], $fields['startYear']
86
				);
87
				$fields['startMonth']->addExtraClass('card_startyear');
88
				unset($fields['startYear']);
89
			}
90 View Code Duplication
			if (isset($fields['expiryMonth']) && isset($fields['expiryYear'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

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

Loading history...
91
				$fields['expiryMonth'] = new FieldGroup(_t("PaymentForm.EXPIRY", "Expiry"),
92
					$fields['expiryMonth'], $fields['expiryYear']
93
				);
94
				$fields['expiryMonth']->addExtraClass('card_expiry');
95
				unset($fields['expiryYear']);
96
			}
97
		}
98
99
		return FieldList::create($fields);
100
	}
101
102
	public function getCardTypes() {
103
		$card = new Omnipay\Common\CreditCard();
104
		$brands = $card->getSupportedBrands();
105
		foreach ($brands as $brand => $x) {
106
			$brands[$brand] = _t("PaymentForm.".strtoupper($brand), $brand);
107
		}
108
109
		return $brands;
110
	}
111
112 View Code Duplication
	public function getBillingFields() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

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

Loading history...
113
		$fields = array(
114
			"billingAddress1" => TextField::create('billingAddress1', _t("PaymentForm.BILLINGADDRESS1", "")),
115
			"billingAddress2" => TextField::create('billingAddress2', _t("PaymentForm.BILLINGADDRESS2", "")),
116
			"city" => TextField::create('billingCity', _t("PaymentForm.BILLINGCITY", "City")),
117
			"postcode" => TextField::create('billingPostcode', _t("PaymentForm.BILLINGPOSTCODE", "Postcode")),
118
			"state" => TextField::create('billingState', _t("PaymentForm.BILLINGSTATE", "State")),
119
			"country" => TextField::create('billingCountry', _t("PaymentForm.BILLINGCOUNTRY", "Country")),
120
			"phone" => PhoneNumberField::create('billingPhone', _t("PaymentForm.BILLINGPHONE", "Phone"))
121
		);
122
		$this->cullForGateway($fields);
123
124
		return FieldList::create($fields);
125
	}
126
127 View Code Duplication
	public function getShippingFields() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

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

Loading history...
128
		$fields = array(
129
			"shippingAddress1" => TextField::create(
130
				'shippingAddress1', _t("PaymentForm.SHIPPINGADDRESS1", "Shipping Address")
131
			),
132
			"shippingAddress2" => TextField::create(
133
				'shippingAddress2', _t("PaymentForm.SHIPPINGADDRESS2", "Shipping Address 2")
134
			),
135
			"city" => TextField::create('shippingCity', _t("PaymentForm.SHIPPINGCITY", "Shipping City")),
136
			"postcode" => TextField::create('shippingPostcode', _t("PaymentForm.SHIPPINGPOSTCODE", "Shipping Postcode")),
137
			"state" => TextField::create('shippingState', _t("PaymentForm.SHIPPINGSTATE", "Shipping State")),
138
			"country" => TextField::create('shippingCountry', _t("PaymentForm.SHIPPINGCOUNTRY", "Shipping Country")),
139
			"phone" => PhoneNumberField::create('shippingPhone', _t("PaymentForm.SHIPPINGPHONE", "Shipping Phone"))
140
		);
141
		$this->cullForGateway($fields);
142
143
		return FieldList::create($fields);
144
	}
145
146
	public function getEmailFields() {
147
		$fields = array(
148
			"email" => EmailField::create('email', _t("PaymentForm.EMAIL", "Email"))
149
		);
150
		$this->cullForGateway($fields);
151
152
		return FieldList::create($fields);
153
	}
154
155
	public function getCompanyFields() {
156
		$fields = array(
157
			"company" => TextField::create('company', _t("PaymentForm.COMPANY", "Company"))
158
		);
159
		$this->cullForGateway($fields);
160
161
		return FieldList::create($fields);
162
	}
163
164
	protected function cullForGateway(&$fields, $defaults = array()) {
165
		$selected = array_merge($defaults, GatewayInfo::required_fields($this->gateway));
166
		foreach ($fields as $name => $field) {
167
			if (!in_array($name, $selected)) {
168
				unset($fields[$name]);
169
			}
170
		}
171
	}
172
173
}
174