Total Complexity | 43 |
Total Lines | 354 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like EDU_KlarnaCheckout often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EDU_KlarnaCheckout, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class EDU_KlarnaCheckout extends EDU_Integration { |
||
6 | public function __construct() { |
||
20 | } |
||
21 | |||
22 | /** |
||
23 | * @param $attributes |
||
24 | */ |
||
25 | public function test_page( $attributes ) { |
||
26 | $attributes = shortcode_atts( |
||
27 | array( |
||
28 | 'bookingid' => 0, |
||
29 | 'programmebookingid' => 0, |
||
30 | ), |
||
31 | normalize_empty_atts( $attributes ), |
||
32 | 'test_page' |
||
33 | ); |
||
34 | |||
35 | if ( $attributes['bookingid'] > 0 ) { |
||
36 | $event_booking = EDUAPI()->OData->Bookings->GetItem( |
||
37 | $attributes['bookingid'], |
||
38 | null, |
||
39 | 'Customer($select=CustomerId;),ContactPerson($select=PersonId;),OrderRows', |
||
40 | false |
||
41 | ); |
||
42 | } elseif ( $attributes['programmebookingid'] > 0 ) { |
||
43 | $event_booking = EDUAPI()->OData->ProgrammeBookings->GetItem( |
||
44 | $attributes['programmebookingid'], |
||
45 | null, |
||
46 | 'Customer($select=CustomerId;),ContactPerson($select=PersonId;),OrderRows', |
||
47 | false |
||
48 | ); |
||
49 | } |
||
50 | |||
51 | $_customer = EDUAPI()->OData->Customers->GetItem( |
||
52 | $event_booking['Customer']['CustomerId'], |
||
53 | null, |
||
54 | null, |
||
55 | false |
||
56 | ); |
||
57 | |||
58 | $_contact = EDUAPI()->OData->Persons->GetItem( |
||
59 | $event_booking['ContactPerson']['PersonId'], |
||
60 | null, |
||
61 | null, |
||
62 | false |
||
63 | ); |
||
64 | |||
65 | $ebi = new EduAdmin_BookingInfo( $event_booking, $_customer, $_contact ); |
||
66 | |||
67 | if ( ! empty( EDU()->session['klarna-order-id'] ) && ! empty( $_GET['klarna_order_id'] ) && EDU()->session['klarna-order-id'] === $_GET['klarna_order_id'] ) { |
||
68 | do_action( 'eduadmin-bookingcompleted', $ebi ); |
||
69 | } else { |
||
70 | do_action( 'eduadmin-processbooking', $ebi ); |
||
71 | } |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param EduAdmin_BookingInfo|null $ebi |
||
76 | */ |
||
77 | public function intercept_booking( $ebi = null ) { |
||
78 | if ( 'no' === $this->get_option( 'enabled', 'no' ) ) { |
||
79 | return; |
||
80 | } |
||
81 | |||
82 | if ( ! empty( $_POST['act'] ) && ( 'bookCourse' === $_POST['act'] || 'bookProgramme' === $_POST['act'] ) ) { |
||
83 | $ebi->NoRedirect = true; |
||
84 | } |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param EduAdmin_BookingInfo|null $ebi |
||
89 | */ |
||
90 | public function process_booking( $ebi = null ) { |
||
91 | if ( 'no' === $this->get_option( 'enabled', 'no' ) ) { |
||
92 | return; |
||
93 | } |
||
94 | |||
95 | $ebi->NoRedirect = true; |
||
96 | |||
97 | if ( empty( $_GET['klarna_order_id'] ) || empty( EDU()->session['klarna-order-id'] ) ) { |
||
98 | $checkout = $this->create_checkout( $ebi ); |
||
99 | |||
100 | $snippet = $checkout['gui']['snippet']; |
||
101 | echo "<div>{$snippet}</div>"; |
||
102 | } |
||
103 | } |
||
104 | |||
105 | public function process_klarnaresponse() { |
||
106 | if ( 'no' === $this->get_option( 'enabled', 'no' ) ) { |
||
107 | return; |
||
108 | } |
||
109 | $checkout_url = ! checked( $this->get_option( 'test_mode', 'no' ), '1', false ) ? Klarna_Checkout_Connector::BASE_URL : Klarna_Checkout_Connector::BASE_TEST_URL; |
||
110 | $shared_secret = $this->get_option( 'shared_secret', '' ); |
||
111 | |||
112 | if ( ! empty( $_GET['klarna_order_id'] ) && ! empty( EDU()->session['klarna-order-id'] ) && EDU()->session['klarna-order-id'] === $_GET['klarna_order_id'] ) { |
||
113 | try { |
||
114 | $connector = Klarna_Checkout_Connector::create( |
||
115 | $shared_secret, |
||
116 | $checkout_url |
||
117 | ); |
||
118 | |||
119 | $order_id = EDU()->session['klarna-order-id']; |
||
120 | |||
121 | $order = new Klarna_Checkout_Order( $connector, $order_id ); |
||
122 | |||
123 | $order->fetch(); |
||
124 | |||
125 | $snippet = $order['gui']['snippet']; |
||
126 | echo "<div>{$snippet}</div>"; |
||
127 | EDU()->session['klarna-order-id'] = null; |
||
128 | |||
129 | } catch ( Klarna_Checkout_ApiErrorException $ex ) { |
||
130 | EDU()->write_debug( $ex->getMessage() ); |
||
131 | EDU()->write_debug( $ex->getPayload() ); |
||
132 | } |
||
133 | } |
||
134 | } |
||
135 | |||
136 | public function init_form_fields() { |
||
137 | $this->setting_fields = array( |
||
138 | 'enabled' => array( |
||
139 | 'title' => __( 'Enabled', 'edauadmin-wp-klarna-checkout' ), |
||
140 | 'type' => 'checkbox', |
||
141 | 'description' => __( 'Enables/Disabled the integration with Klarna Checkout', 'eduadmin-wp-klarna-checkout' ), |
||
142 | 'default' => 'no', |
||
143 | ), |
||
144 | 'eid' => array( |
||
145 | 'title' => __( 'EID', 'eduadmin-wp-klarna-checkout' ), |
||
146 | 'type' => 'text', |
||
147 | 'description' => __( 'The EID to connect to Klarna Checkout v2', 'eduadmin-wp-klarna-checkout' ), |
||
148 | 'default' => '', |
||
149 | ), |
||
150 | 'shared_secret' => array( |
||
151 | 'title' => __( 'Shared secret', 'eduadmin-wp-klarna-checkout' ), |
||
152 | 'type' => 'password', |
||
153 | 'description' => __( 'The shared secret to connect to Klarna Checkout v2', 'eduadmin-wp-klarna-checkout' ), |
||
154 | 'default' => '', |
||
155 | ), |
||
156 | 'termsurl' => array( |
||
157 | 'title' => __( 'Terms and Conditions URL', 'eduadmin-wp-klarna-checkout' ), |
||
158 | 'type' => 'text', |
||
159 | 'description' => __( 'This URL is required for Klarna Checkout', 'eduadmin-wp-klarna-checkout' ), |
||
160 | 'default' => '', |
||
161 | ), |
||
162 | 'test_mode' => array( |
||
163 | 'title' => __( 'Test mode', 'eduadmin-wp-klarna-checkout' ), |
||
164 | 'type' => 'checkbox', |
||
165 | 'description' => __( 'Enables test mode, so you can test the integration', 'eduadmin-wp-klarna-checkout' ), |
||
166 | 'default' => 'no', |
||
167 | ), |
||
168 | ); |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * @param EduAdmin_BookingInfo|null $ebi |
||
173 | * |
||
174 | * @return Klarna_Checkout_Order|null |
||
175 | */ |
||
176 | public function create_checkout( $ebi = null ) { |
||
177 | |||
178 | $checkout_url = ! checked( $this->get_option( 'test_mode', 'no' ), '1', false ) ? Klarna_Checkout_Connector::BASE_URL : Klarna_Checkout_Connector::BASE_TEST_URL; |
||
179 | $shared_secret = $this->get_option( 'shared_secret', '' ); |
||
180 | |||
181 | $create = array(); |
||
182 | |||
183 | $create['locale'] = strtolower( str_replace( '_', '-', get_locale() ) ); |
||
184 | $create['purchase_country'] = 'SE'; |
||
185 | $create['purchase_currency'] = get_option( 'eduadmin-currency', 'SEK' ); |
||
186 | |||
187 | $merchant = array(); |
||
188 | $merchant['id'] = $this->get_option( 'eid', '' ); |
||
189 | $merchant['terms_uri'] = $this->get_option( 'termsurl', '' ); |
||
190 | |||
191 | $current_url = esc_url( "{$_SERVER['REQUEST_SCHEME']}://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}" ); |
||
192 | |||
193 | $booking_id = 0; |
||
194 | $programme_booking_id = 0; |
||
195 | |||
196 | $reference_id = 0; |
||
197 | |||
198 | $_event = null; |
||
199 | |||
200 | if ( ! empty( $ebi->EventBooking['BookingId'] ) ) { |
||
201 | $booking_id = intval( $ebi->EventBooking['BookingId'] ); |
||
202 | $reference_id = $booking_id; |
||
203 | |||
204 | $_event = EDUAPI()->OData->Events->GetItem( $ebi->EventBooking['EventId'] ); |
||
205 | } |
||
206 | |||
207 | if ( ! empty( $ebi->EventBooking['ProgrammeBookingId'] ) ) { |
||
208 | $programme_booking_id = intval( $ebi->EventBooking['ProgrammeBookingId'] ); |
||
209 | $reference_id = $programme_booking_id; |
||
210 | |||
211 | $_event = EDUAPI()->OData->ProgrammeStarts->GetItem( $ebi->EventBooking['ProgrammeStartId'] ); |
||
212 | } |
||
213 | |||
214 | $rowExtraInfo = ""; |
||
215 | |||
216 | if ( null != $_event ) { |
||
217 | if ( ! empty( $_event['City'] ) ) { |
||
218 | $rowExtraInfo .= ';' . $_event['City']; |
||
219 | } |
||
220 | |||
221 | if ( ! empty( $_event['StartDate'] ) ) { |
||
222 | $rowExtraInfo .= ';' . date( "Y-m-d", strtotime( $_event['StartDate'] ) ); |
||
223 | } |
||
224 | |||
225 | if ( ! empty( $_event['EndDate'] ) ) { |
||
226 | $rowExtraInfo .= ';' . date( "Y-m-d", strtotime( $_event['EndDate'] ) ); |
||
227 | } |
||
228 | } |
||
229 | |||
230 | $confirmation_url = add_query_arg( |
||
231 | array( |
||
232 | 'klarna_order_id' => '{checkout.order.id}', |
||
233 | 'booking_id' => $booking_id, |
||
234 | 'programme_booking_id' => $programme_booking_id, |
||
235 | 'edu-valid-form' => wp_create_nonce( 'edu-booking-confirm' ), |
||
236 | 'act' => 'paymentCompleted', |
||
237 | ), |
||
238 | $current_url |
||
239 | ); |
||
240 | |||
241 | $push_url = add_query_arg( |
||
242 | array( |
||
243 | 'klarna_order_id' => '{checkout.order.id}', |
||
244 | 'booking_id' => $booking_id, |
||
245 | 'programme_booking_id' => $programme_booking_id, |
||
246 | 'status' => 'push', |
||
247 | ), |
||
248 | $current_url |
||
249 | ); |
||
250 | |||
251 | $merchant['checkout_uri'] = $current_url; |
||
252 | $merchant['confirmation_uri'] = $confirmation_url; |
||
253 | $merchant['push_uri'] = $push_url; |
||
254 | |||
255 | $create['merchant'] = $merchant; |
||
256 | |||
257 | $create['merchant_reference'] = array(); |
||
258 | $create['merchant_reference']['orderid1'] = $reference_id; |
||
259 | $create['merchant_reference']['orderid2'] = $reference_id; |
||
260 | |||
261 | $create['cart'] = array(); |
||
262 | $create['cart']['items'] = array(); |
||
263 | |||
264 | foreach ( $ebi->EventBooking['OrderRows'] as $order_row ) { |
||
265 | $cart_item = array(); |
||
266 | |||
267 | $cart_item['reference'] = $order_row['ItemNumber']; |
||
268 | $cart_item['name'] = $order_row['Description'] . $rowExtraInfo; |
||
269 | $cart_item['quantity'] = intval( $order_row['Quantity'] ); |
||
270 | |||
271 | if ( ! $order_row['PriceIncVat'] ) { |
||
272 | $price_per_unit = $order_row['PricePerUnit'] * ( 1 + ( $order_row['VatPercent'] / 100 ) ) * 100; |
||
273 | } else { |
||
274 | $price_per_unit = $order_row['PricePerUnit'] * 100; |
||
275 | } |
||
276 | |||
277 | $cart_item['unit_price'] = $price_per_unit; |
||
278 | $cart_item['tax_rate'] = intval( $order_row['VatPercent'] * 100 ); |
||
279 | $cart_item['discount_rate'] = intval( $order_row['DiscountPercent'] * 100 ); |
||
280 | |||
281 | $create['cart']['items'][] = $cart_item; |
||
282 | } |
||
283 | |||
284 | try { |
||
285 | $connector = Klarna_Checkout_Connector::create( |
||
286 | $shared_secret, |
||
287 | $checkout_url |
||
288 | ); |
||
289 | |||
290 | $order = new Klarna_Checkout_Order( $connector ); |
||
291 | $order->create( $create ); |
||
292 | |||
293 | $order->fetch(); |
||
294 | |||
295 | $order_id = $order['id']; |
||
296 | EDU()->session['klarna-order-id'] = $order_id; |
||
297 | |||
298 | return $order; |
||
299 | } catch ( Klarna_Checkout_ApiErrorException $ex ) { |
||
300 | EDU()->write_debug( $ex->getMessage() ); |
||
301 | EDU()->write_debug( $ex->getPayload() ); |
||
302 | |||
303 | return null; |
||
304 | } |
||
305 | } |
||
306 | |||
307 | public function process_paymentstatus() { |
||
359 | } |
||
360 | } |
||
361 | } |
||
362 | } |
||
363 | } |
||
364 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.