eonx-com / webhooks
Package to allow payloads to be sent based on event triggers
Installs: 5 564
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 5
Forks: 0
Open Issues: 2
Type:package
Requires
- php: >=7.3
- ext-json: *
- ext-mbstring: *
- doctrine/orm: ^2.6.3
- eonx-com/externals: ^1.0|^2.0
- eonx-com/utils: ^1.0|^2.0
- guzzlehttp/guzzle: ^6.0.0
Requires (Dev)
- eonx-com/standards: ^0.2
- laravel/lumen-framework: ^5.5
- mockery/mockery: ^1.0
- phpmd/phpmd: ^2.6
- phpstan/phpstan: ^0.11
- phpstan/phpstan-phpunit: ^0.11.0
- phpstan/phpstan-strict-rules: ^0.11.0
- phpunit/phpunit: ^8.0
- roave/security-advisories: dev-master
- sebastian/phpcpd: ^4.0
- squizlabs/php_codesniffer: 3.*
- symfony/security: ^4.3
Provides
Replaces
This package is auto-updated.
Last update: 2021-12-27 04:52:51 UTC
README
This library adds support for creating Activities which are then fired as webhooks to subscribers of those activities.
Installation
Use Composer to install the package in your project:
composer require eoneopay/webhooks
Usage
Inject the \EoneoPay\Webhooks\Activities\Interface\ActivityFactoryInterface
service
into your application where an activity needs to be created. The send method on this
interface accepts an ActivityDataInterface
implementation that represents a specific
activity to be created.
For each of the different activities you will fire inside your application you will need
to create a class that implements ActivityDataInterface
.
Theory of Operation
ActivityFactoryInterface
receives an instance ofActivityDataInterface
- The factory will then call the
PayloadManager
to build the payload for theActivityDataInterface
- The factory will take the payload and the
ActivityDataInterface
and save it as a newActivityInterface
entity. - Finally, the factory will dispatch an ActivityCreatedEvent
- The factory will then call the
- The listeners will receive the event inside an asynchronous queue worker and call
WebhookManager#processActivity
- The WebhookManager will resolve any subscriptions for the activity
- Then it will create a new WebhookRequest for each subscription
- And dispatch a new WebhookRequestCreatedEvent.
- Another listener will accept this event and call
RequestProcessor#process
- Which builds a PSR7 Request
- Sends the request
- Records the result as a WebhookResponse
Integration
Laravel
To integrate the package into your Laravel or Lumen you need to register the following service providers:
\EoneoPay\Webhooks\Bridge\Laravel\Providers\WebhookServiceProvider
\EoneoPay\Webhooks\Bridge\Laravel\Providers\WebhookEventServiceProvider
Any implementation of this library will need to:
- Implement and bind a service for the interface
EoneoPay\Webhooks\Subscription\Interfaces\SubscriptionResolverInterface
- Implement a service for the interface
EoneoPay\Webhooks\Payload\Interfaces\PayloadBuilderInterface
- example
interface WebHookPayloadBuilderInterface extends PayloadBuilderInterface { ... } final class PayloadBuilder implements WebHookPayloadBuilderInterface { ... }
- bind and tag the service for the interface
YourNamespace\WebHookPayloadBuilderInterface
$this->app->bind(WebHookPayloadBuilderInterface::class, PayloadBuilder::class); $this->app->tag([WebHookPayloadBuilderInterface::class], ['webhooks_payload_builders']);
- Add
EoneoPay\Externals\Bridge\Laravel\ORM\ResolveTargetEntityExtension
toconfig/doctrine.php
under theextensions
key - Modify
config/doctrine.php
to add the following changes to the configuration:
<?php declare(strict_types=1); use EoneoPay\Externals\Bridge\LaravelDoctrine\Extensions\ResolveTargetEntityExtension; use EoneoPay\Webhooks\Models\ActivityInterface; use EoneoPay\Webhooks\Models\WebhookRequestInterface; use EoneoPay\Webhooks\Models\WebhookResponseInterface; use EoneoPay\Webhooks\Bridge\Doctrine\Entities\Activity; use EoneoPay\Webhooks\Bridge\Doctrine\Entities\Lifecycle\Request; use EoneoPay\Webhooks\Bridge\Doctrine\Entities\Lifecycle\Response; return [ 'managers' => [ 'default' => [ // ... 'namespaces' => [ // ... // Add the Webhooks Entities to the namespace mappings 'Eoneopay\\Webhooks\\Bridge\\Doctrine\\Entities' ], 'paths' => [ // ... // Add the Webhooks filepath to the Entity Manager \base_path('vendor/eoneopay/webhooks/src/Bridge/Doctrine/Entities') ] // ... ] ], // ... 'extensions' => [ // ... // Add the ResolveTargetEntityExtension to Doctrine ResolveTargetEntityExtension::class ], // ... 'replacements' => [ // Add replacements so Doctrine can look up entities by interface ActivityInterface::class => Activity::class, WebhookRequestInterface::class => Request::class, WebhookResponseInterface::class => Response::class ] ];