Conditions | 4 |
Paths | 3 |
Total Lines | 122 |
Code Lines | 98 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
22 | function createOrder() |
||
23 | { |
||
24 | // There are 3 objects which are mandatory: User object, ShoppingCart object and Configuration object. |
||
25 | //1. User Object |
||
26 | writeLog('Creating User object'); |
||
27 | writeLog('Adding the address of the user'); |
||
28 | $userAddress = new \PagaMasTarde\OrdersApiClient\Model\Order\User\Address(); |
||
29 | $userAddress |
||
30 | ->setZipCode('28031') |
||
31 | ->setFullName('María Sanchez Escudero') |
||
32 | ->setCountryCode('ES') |
||
33 | ->setCity('Madrid') |
||
34 | ->setAddress('Paseo de la Castellana, 95') |
||
35 | ->setDni('59661738Z') |
||
36 | ->setFixPhone('911231234') |
||
37 | ->setMobilePhone('600123123'); |
||
38 | |||
39 | $orderBillingAddress = $userAddress; |
||
40 | |||
41 | $orderShippingAddress = new \PagaMasTarde\OrdersApiClient\Model\Order\User\Address(); |
||
42 | $orderShippingAddress |
||
43 | ->setZipCode('08029') |
||
44 | ->setFullName('Alberto Escudero Sanchez') |
||
45 | ->setCountryCode('ES') |
||
46 | ->setCity('Barcelona') |
||
47 | ->setAddress('Avenida de la diagonal 525') |
||
48 | ->setDni('77695544A') |
||
49 | ->setFixPhone('931232345') |
||
50 | ->setMobilePhone('600123124'); |
||
51 | |||
52 | writeLog('Adding the information of the user'); |
||
53 | $orderUser = new \PagaMasTarde\OrdersApiClient\Model\Order\User(); |
||
54 | $orderUser |
||
55 | ->setFullName('María Sanchez Escudero') |
||
56 | ->setAddress($userAddress) |
||
57 | ->setBillingAddress($orderBillingAddress) |
||
58 | ->setShippingAddress($orderShippingAddress) |
||
59 | ->setDateOfBirth('1985-12-30') |
||
60 | ->setEmail('[email protected]') |
||
61 | ->setFixPhone('911231234') |
||
62 | ->setMobilePhone('600123123') |
||
63 | ->setDni('59661738Z'); |
||
64 | writeLog('Created User object'); |
||
65 | |||
66 | //2. ShoppingCart Object |
||
67 | writeLog('Creating ShoppingCart object'); |
||
68 | writeLog('Adding the purchases of the customer, if there are.'); |
||
69 | $orderHistory = new \PagaMasTarde\OrdersApiClient\Model\Order\User\OrderHistory(); |
||
70 | $orderHistory |
||
71 | ->setAmount('2499') |
||
72 | ->setDate('2010-01-31'); |
||
73 | $orderUser->addOrderHistory($orderHistory); |
||
74 | |||
75 | writeLog('Adding cart products'); |
||
76 | $product = new \PagaMasTarde\OrdersApiClient\Model\Order\ShoppingCart\Details\Product(); |
||
77 | $product |
||
78 | ->setAmount('59999') |
||
79 | ->setQuantity('1') |
||
80 | ->setDescription('TV LG UltraPlana'); |
||
81 | |||
82 | $details = new \PagaMasTarde\OrdersApiClient\Model\Order\ShoppingCart\Details(); |
||
83 | $details->setShippingCost('0'); |
||
84 | $details->addProduct($product); |
||
85 | |||
86 | $orderShoppingCart = new \PagaMasTarde\OrdersApiClient\Model\Order\ShoppingCart(); |
||
87 | $orderShoppingCart |
||
88 | ->setDetails($details) |
||
89 | ->setOrderReference(ORDERIDENTIFICATION) |
||
90 | ->setPromotedAmount(0) // This amount means that the merchant will asume the interests. |
||
91 | ->setTotalAmount('59999'); |
||
92 | writeLog('Created OrderShoppingCart object'); |
||
93 | |||
94 | //3. Configuration Object |
||
95 | writeLog('Creating Configuration object'); |
||
96 | writeLog('Adding urls to redirect the user according each case'); |
||
97 | $confirmUrl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]?action=confirmOrder"; |
||
98 | $errorUrl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]?action=cancelOrder"; |
||
99 | $orderConfigurationUrls = new \PagaMasTarde\OrdersApiClient\Model\Order\Configuration\Urls(); |
||
100 | $orderConfigurationUrls |
||
101 | ->setCancel($errorUrl) |
||
102 | ->setKo($errorUrl) |
||
103 | ->setNotificationCallback($confirmUrl) |
||
104 | ->setOk($confirmUrl); |
||
105 | |||
106 | writeLog('Adding channel info'); |
||
107 | $orderChannel = new \PagaMasTarde\OrdersApiClient\Model\Order\Configuration\Channel(); |
||
108 | $orderChannel |
||
109 | ->setAssistedSale(false) |
||
110 | ->setType(\PagaMasTarde\OrdersApiClient\Model\Order\Configuration\Channel::ONLINE); |
||
111 | |||
112 | $orderConfiguration = new \PagaMasTarde\OrdersApiClient\Model\Order\Configuration(); |
||
113 | $orderConfiguration |
||
114 | ->setChannel($orderChannel) |
||
115 | ->setUrls($orderConfigurationUrls); |
||
116 | writeLog('Created Configuration object'); |
||
117 | |||
118 | $order = new \PagaMasTarde\OrdersApiClient\Model\Order(); |
||
119 | $order |
||
120 | ->setConfiguration($orderConfiguration) |
||
121 | ->setShoppingCart($orderShoppingCart) |
||
122 | ->setUser($orderUser); |
||
123 | |||
124 | writeLog('Creating client variable'); |
||
125 | if (PUBLICKEY=='' || PRIVATEKEY == '') { |
||
|
|||
126 | throw new \Exception('You need set the public and private key'); |
||
127 | } |
||
128 | $orderClient = new \PagaMasTarde\OrdersApiClient\Client(PUBLICKEY, PRIVATEKEY); |
||
129 | |||
130 | writeLog('Creating Paga+Tarde order'); |
||
131 | $order = $orderClient->createOrder($order); |
||
132 | if ($order instanceof \PagaMasTarde\OrdersApiClient\Model\Order) { |
||
133 | //If the order is correct and created then we have the redirection URL here: |
||
134 | $url = $order->getActionUrls()->getForm(); |
||
135 | $_SESSION['order_id'] = $order->getId(); |
||
136 | writeLog(json_encode($order->export(), JSON_PRETTY_PRINT)); |
||
137 | } else { |
||
138 | throw new \Exception('Order not created'); |
||
139 | } |
||
140 | |||
141 | // You can use our test credit cards to fill the Paga+Tarde form |
||
142 | writeLog("Redirecting to Paga+Tarde form => $url"); |
||
143 | header('Location:'. $url); |
||
144 | } |
||
198 | } |