diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 44e74ffe9..b0164368e 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -29,4 +29,4 @@ jobs: - name: Run test suite run: ./vendor/bin/phpunit - name: Run cs - run: ./vendor/bin/phpcs src modules.local/ppcp-wc-gateway/src/ modules.local/ppcp-button/src/ modules.local/ppcp-onboarding/src/ --standard=Inpsyde + run: ./vendor/bin/phpcs src modules.local/ppcp-wc-gateway/src/ modules.local/ppcp-webhooks/src/ modules.local/ppcp-button/src/ modules.local/ppcp-onboarding/src/ --standard=Inpsyde diff --git a/modules.local/ppcp-webhooks/src/Handler/CheckoutOrderCompleted.php b/modules.local/ppcp-webhooks/src/Handler/CheckoutOrderCompleted.php index 505f6249b..ed98b0f53 100644 --- a/modules.local/ppcp-webhooks/src/Handler/CheckoutOrderCompleted.php +++ b/modules.local/ppcp-webhooks/src/Handler/CheckoutOrderCompleted.php @@ -1,4 +1,5 @@ eventTypes(), true); } + // phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong public function handleRequest(\WP_REST_Request $request): \WP_REST_Response { $response = ['success' => false]; $orderIds = array_filter( array_map( - function(array $purchaseUnit) : string { + static function (array $purchaseUnit): string { return isset($purchaseUnit['custom_id']) ? (string) $purchaseUnit['custom_id'] : ''; }, isset($request['resource']['purchase_units']) ? (array) $request['resource']['purchase_units'] : [] ), - function(string $orderId) : bool { + static function (string $orderId): bool { return ! empty($orderId); } ); if (empty($orderIds)) { - $message = sprintf( // translators: %s is the PayPal webhook Id. __('No order for webhook event %s was found.', 'woocommerce-paypal-commerce-gateway'), @@ -92,7 +93,11 @@ class CheckoutOrderCompleted implements RequestHandler ); $this->logger->log( 'info', - __('Order ' . $wcOrder->get_id() . ' has been updated through PayPal' , 'woocommerce-paypal-commerce-gateway'), + sprintf( + // translators: %s is the order ID. + __('Order %s has been updated through PayPal', 'woocommerce-paypal-commerce-gateway'), + (string) $wcOrder->get_id() + ), [ 'request' => $request, 'order' => $wcOrder, @@ -102,4 +107,5 @@ class CheckoutOrderCompleted implements RequestHandler $response['success'] = true; return rest_ensure_response($response); } -} \ No newline at end of file + // phpcs:enable Inpsyde.CodeQuality.FunctionLength.TooLong +} diff --git a/modules.local/ppcp-webhooks/src/Handler/PaymentCaptureRefunded.php b/modules.local/ppcp-webhooks/src/Handler/PaymentCaptureRefunded.php index a8fdc80d8..41f5de140 100644 --- a/modules.local/ppcp-webhooks/src/Handler/PaymentCaptureRefunded.php +++ b/modules.local/ppcp-webhooks/src/Handler/PaymentCaptureRefunded.php @@ -1,4 +1,5 @@ eventTypes(), true); } + // phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong public function handleRequest(\WP_REST_Request $request): \WP_REST_Response { $response = ['success' => false]; @@ -68,13 +70,16 @@ class PaymentCaptureRefunded implements RequestHandler */ $refund = wc_create_refund([ 'order_id' => $wcOrder->get_id(), - 'amount' => $request['resource']['amount']['value'] + 'amount' => $request['resource']['amount']['value'], ]); if (is_wp_error($refund)) { - $this->logger->log( 'warning', - __('Order ' . $wcOrder->get_id() . ' could not be refunded' , 'woocommerce-paypal-commerce-gateway'), + sprintf( + // translators: %s is the order id. + __('Order %s could not be refunded', 'woocommerce-paypal-commerce-gateway'), + (string) $wcOrder->get_id() + ), [ 'request' => $request, 'error' => $refund, @@ -88,8 +93,8 @@ class PaymentCaptureRefunded implements RequestHandler $this->logger->log( 'info', sprintf( - //translators: %1$s is the order id %2$s is the amount which has been refunded. - __('Order %1$s has been refunded with %2$s through PayPal' , 'woocommerce-paypal-commerce-gateway'), + // translators: %1$s is the order id %2$s is the amount which has been refunded. + __('Order %1$s has been refunded with %2$s through PayPal', 'woocommerce-paypal-commerce-gateway'), (string) $wcOrder->get_id(), (string) $refund->get_amount() ), @@ -101,4 +106,5 @@ class PaymentCaptureRefunded implements RequestHandler $response['success'] = true; return rest_ensure_response($response); } -} \ No newline at end of file + // phpcs:enable Inpsyde.CodeQuality.FunctionLength.TooLong +} diff --git a/modules.local/ppcp-webhooks/src/Handler/PaymentCaptureReversed.php b/modules.local/ppcp-webhooks/src/Handler/PaymentCaptureReversed.php index 53231b966..70c056339 100644 --- a/modules.local/ppcp-webhooks/src/Handler/PaymentCaptureReversed.php +++ b/modules.local/ppcp-webhooks/src/Handler/PaymentCaptureReversed.php @@ -1,9 +1,9 @@ eventTypes(), true); } + // phpcs:disable Inpsyde.CodeQuality.FunctionLength.TooLong public function handleRequest(\WP_REST_Request $request): \WP_REST_Response { $response = ['success' => false]; @@ -74,12 +75,12 @@ class PaymentCaptureReversed implements RequestHandler $response['success'] = (bool) $wcOrder->update_status('cancelled'); $message = $response['success'] ? sprintf( - //translators: %1$s is the order id. - __('Order %1$s has been cancelled through PayPal' , 'woocommerce-paypal-commerce-gateway'), + // translators: %1$s is the order id. + __('Order %1$s has been cancelled through PayPal', 'woocommerce-paypal-commerce-gateway'), (string) $wcOrder->get_id() ) : sprintf( - //translators: %1$s is the order id. - __('Failed to cancel order %1$s through PayPal' , 'woocommerce-paypal-commerce-gateway'), + // translators: %1$s is the order id. + __('Failed to cancel order %1$s through PayPal', 'woocommerce-paypal-commerce-gateway'), (string) $wcOrder->get_id() ); $this->logger->log( @@ -92,4 +93,5 @@ class PaymentCaptureReversed implements RequestHandler ); return rest_ensure_response($response); } -} \ No newline at end of file + // phpcs:enable Inpsyde.CodeQuality.FunctionLength.TooLong +} diff --git a/modules.local/ppcp-webhooks/src/Handler/RequestHandler.php b/modules.local/ppcp-webhooks/src/Handler/RequestHandler.php index 7743db5e2..fc6a369eb 100644 --- a/modules.local/ppcp-webhooks/src/Handler/RequestHandler.php +++ b/modules.local/ppcp-webhooks/src/Handler/RequestHandler.php @@ -1,15 +1,15 @@ logger = $logger; } - public function register() : bool + public function register(): bool { return (bool) register_rest_route( self::NAMESPACE, @@ -54,7 +54,8 @@ class IncomingWebhookEndpoint ); } - public function verifyRequest() : bool { + public function verifyRequest(): bool + { try { $data = (array) get_option(WebhookRegistrar::KEY, []); $webhook = $this->webhookFactory->fromArray($data); @@ -64,7 +65,8 @@ class IncomingWebhookEndpoint } } - public function handleRequest(\WP_REST_Request $request) : \WP_REST_Response { + public function handleRequest(\WP_REST_Request $request): \WP_REST_Response + { foreach ($this->handlers as $handler) { if ($handler->responsibleForRequest($request)) { @@ -72,8 +74,9 @@ class IncomingWebhookEndpoint $this->logger->log( 'info', sprintf( + // translators: %s is the event type __('Webhook has been handled by %s', 'woocommerce-paypal-commerce-gateway'), - ($handler->eventTypes())?current($handler->eventTypes()):'' + ($handler->eventTypes()) ? current($handler->eventTypes()) : '' ), [ 'request' => $request, @@ -88,11 +91,13 @@ class IncomingWebhookEndpoint return rest_ensure_response($response); } - public function url() : string { - return str_replace('http', 'https', rest_url(self::NAMESPACE . '/' . self::ROUTE)); + public function url(): string + { + return rest_url(self::NAMESPACE . '/' . self::ROUTE); } - public function handledEventTypes() : array { + public function handledEventTypes(): array + { $eventTypes = []; foreach ($this->handlers as $handler) { $eventTypes = array_merge($eventTypes, $handler->eventTypes()); diff --git a/modules.local/ppcp-webhooks/src/WebhookModule.php b/modules.local/ppcp-webhooks/src/WebhookModule.php index d1eae414c..0072d11a5 100644 --- a/modules.local/ppcp-webhooks/src/WebhookModule.php +++ b/modules.local/ppcp-webhooks/src/WebhookModule.php @@ -27,7 +27,7 @@ class WebhookModule implements ModuleInterface { add_action( 'rest_api_init', - function() use ($container) { + static function () use ($container) { $endpoint = $container->get('webhook.endpoint.controller'); /** * @var IncomingWebhookEndpoint $endpoint @@ -38,7 +38,7 @@ class WebhookModule implements ModuleInterface add_action( WebhookRegistrar::EVENT_HOOK, - function() use ($container) { + static function () use ($container) { $registrar = $container->get('webhook.registrar'); $registrar->register(); } @@ -46,7 +46,7 @@ class WebhookModule implements ModuleInterface add_action( 'woocommerce-paypal-commerce-gateway.deactivate', - function() use ($container) { + static function () use ($container) { $registrar = $container->get('webhook.registrar'); $registrar->unregister(); } diff --git a/modules.local/ppcp-webhooks/src/WebhookRegistrar.php b/modules.local/ppcp-webhooks/src/WebhookRegistrar.php index 5807739d8..23696e930 100644 --- a/modules.local/ppcp-webhooks/src/WebhookRegistrar.php +++ b/modules.local/ppcp-webhooks/src/WebhookRegistrar.php @@ -1,9 +1,9 @@ webhookFactory = $webhookFactory; $this->endpoint = $endpoint; $this->restEndpoint = $restEndpoint; } - public function register() : bool + public function register(): bool { $webhook = $this->webhookFactory->forUrlAndEvents( $this->restEndpoint->url(), @@ -36,7 +37,7 @@ class WebhookRegistrar try { $created = $this->endpoint->create($webhook); - if(empty($created->id())) { + if (empty($created->id())) { return false; } update_option( @@ -53,7 +54,8 @@ class WebhookRegistrar } } - public function unregister() : bool { + public function unregister(): bool + { $data = (array) get_option(self::KEY, []); if (! $data) { return false; @@ -70,4 +72,4 @@ class WebhookRegistrar } return $success; } -} \ No newline at end of file +}