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