Add service for current webhook

This commit is contained in:
Alex P 2021-09-21 17:52:44 +03:00
parent efb5ab3d14
commit 2c294861a1
2 changed files with 42 additions and 16 deletions

View file

@ -10,8 +10,10 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Webhooks;
use Exception;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\WebhookEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Webhook;
use WooCommerce\PayPalCommerce\ApiClient\Factory\WebhookFactory;
use WooCommerce\PayPalCommerce\WcGateway\Assets\WebhooksStatusPageAssets;
use WooCommerce\PayPalCommerce\Webhooks\Endpoint\ResubscribeEndpoint;
use WooCommerce\PayPalCommerce\Webhooks\Handler\CheckoutOrderApproved;
@ -37,14 +39,14 @@ return array(
},
'webhook.endpoint.controller' => function( $container ) : IncomingWebhookEndpoint {
$webhook_endpoint = $container->get( 'api.endpoint.webhook' );
$webhook_factory = $container->get( 'api.factory.webhook' );
$webhook = $container->get( 'webhook.current' );
$handler = $container->get( 'webhook.endpoint.handler' );
$logger = $container->get( 'woocommerce.logger.woocommerce' );
$verify_request = ! defined( 'PAYPAL_WEBHOOK_REQUEST_VERIFICATION' ) || PAYPAL_WEBHOOK_REQUEST_VERIFICATION;
return new IncomingWebhookEndpoint(
$webhook_endpoint,
$webhook_factory,
$webhook,
$logger,
$verify_request,
... $handler
@ -63,6 +65,29 @@ return array(
);
},
'webhook.current' => function( $container ) : ?Webhook {
$data = (array) get_option( WebhookRegistrar::KEY, array() );
if ( empty( $data ) ) {
return null;
}
$factory = $container->get( 'api.factory.webhook' );
assert( $factory instanceof WebhookFactory );
try {
return $factory->from_array( $data );
} catch ( Exception $exception ) {
$logger = $container->get( 'woocommerce.logger.woocommerce' );
assert( $logger instanceof LoggerInterface );
$logger->error( 'Failed to parse the stored webhook data: ' . $exception->getMessage() );
return null;
}
},
'webhook.is-registered' => function( $container ) : bool {
return $container->get( 'webhook.current' ) !== null;
},
'webhook.status.registered-webhooks' => function( $container ) : array {
$endpoint = $container->get( 'api.endpoint.webhook' );
assert( $endpoint instanceof WebhookEndpoint );