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