| Conditions | 10 |
| Paths | 12 |
| Total Lines | 98 |
| Code Lines | 54 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 71 | public function rest_api_adyen_payments( WP_REST_Request $request ) { |
||
| 72 | $payment_id = $request->get_param( 'payment_id' ); |
||
| 73 | |||
| 74 | // Payment ID. |
||
| 75 | if ( null === $payment_id ) { |
||
| 76 | return new \WP_Error( |
||
| 77 | 'pronamic-pay-adyen-no-payment-id', |
||
| 78 | __( 'No payment ID given in `payment_id` parameter.', 'pronamic_ideal' ) |
||
| 79 | ); |
||
| 80 | } |
||
| 81 | |||
| 82 | $payment = \get_pronamic_payment( $payment_id ); |
||
| 83 | |||
| 84 | if ( null === $payment ) { |
||
| 85 | return new \WP_Error( |
||
| 86 | 'pronamic-pay-adyen-payment-not-found', |
||
| 87 | sprintf( |
||
| 88 | /* translators: %s: payment ID */ |
||
| 89 | __( 'Could not find payment with ID `%s`.', 'pronamic_ideal' ), |
||
| 90 | $payment_id |
||
| 91 | ), |
||
| 92 | $payment_id |
||
| 93 | ); |
||
| 94 | } |
||
| 95 | |||
| 96 | // State data. |
||
| 97 | $data = \json_decode( \file_get_contents( 'php://input' ) ); |
||
| 98 | |||
| 99 | if ( null === $data ) { |
||
| 100 | return new \WP_Error( |
||
| 101 | 'pronamic-pay-adyen-no-data', |
||
| 102 | __( 'No state data given in request body.', 'pronamic_ideal' ) |
||
| 103 | ); |
||
| 104 | } |
||
| 105 | |||
| 106 | // Gateway. |
||
| 107 | $config_id = $payment->get_config_id(); |
||
| 108 | |||
| 109 | $gateway = Plugin::get_gateway( $config_id ); |
||
| 110 | |||
| 111 | if ( empty( $gateway ) ) { |
||
| 112 | return new \WP_Error( |
||
| 113 | 'pronamic-pay-adyen-gateway-not-found', |
||
| 114 | sprintf( |
||
| 115 | /* translators: %s: Gateway configuration ID */ |
||
| 116 | __( 'Could not find gateway with ID `%s`.', 'pronamic_ideal' ), |
||
| 117 | $config_id |
||
| 118 | ), |
||
| 119 | $config_id |
||
| 120 | ); |
||
| 121 | } |
||
| 122 | |||
| 123 | if ( ! isset( $gateway->client ) ) { |
||
| 124 | return new \WP_Error( |
||
| 125 | 'pronamic-pay-adyen-client-not-found', |
||
| 126 | sprintf( |
||
| 127 | /* translators: %s: Gateway configuration ID */ |
||
| 128 | __( 'Could not find client in gateway with ID `%s`.', 'pronamic_ideal' ), |
||
| 129 | $config_id |
||
| 130 | ), |
||
| 131 | $config_id |
||
| 132 | ); |
||
| 133 | } |
||
| 134 | |||
| 135 | // Create payment. |
||
| 136 | if ( ! isset( $data->paymentMethod->type ) ) { |
||
| 137 | return new \WP_Error( |
||
| 138 | 'pronamic-pay-adyen-no-payment-method', |
||
| 139 | __( 'No payment method given.', 'pronamic_ideal' ) |
||
| 140 | ); |
||
| 141 | } |
||
| 142 | |||
| 143 | switch ( $data->paymentMethod->type ) { |
||
| 144 | case PaymentMethodType::DIRECT_DEBIT: |
||
| 145 | $payment_method = new PaymentMethodSepaDirectDebit( $data->paymentMethod->type, $data->paymentMethod->{'sepa.ibanNumber'}, $data->paymentMethod->{'sepa.ownerName'} ); |
||
| 146 | |||
| 147 | break; |
||
| 148 | case PaymentMethodType::IDEAL: |
||
| 149 | $payment_method = new PaymentMethodIDeal( $data->paymentMethod->type, $data->paymentMethod->issuer ); |
||
| 150 | |||
| 151 | break; |
||
| 152 | default: |
||
| 153 | $payment_method = PaymentMethod::from_object( $data->paymentMethod ); |
||
| 154 | } |
||
| 155 | |||
| 156 | $response = $gateway->create_payment( $payment, $payment_method ); |
||
| 157 | |||
| 158 | // Return action if available. |
||
| 159 | $action = $response->get_action(); |
||
| 160 | |||
| 161 | if ( null !== $action ) { |
||
| 162 | return (object) array( |
||
| 163 | 'action' => $action->get_json(), |
||
| 164 | ); |
||
| 165 | } |
||
| 166 | |||
| 167 | return (object) array( |
||
| 168 | 'resultCode' => $response->get_result_code(), |
||
| 169 | ); |
||
| 172 |