dovuofficial / guardian-php-sdk

Configuration based Guardian policy consumption and management for dovu platform
MIT License
0 stars 0 forks source link

Added Notifictions functionality + code refactor #5

Closed jonwood2 closed 1 year ago

jonwood2 commented 1 year ago

I have updated the SDK to work better and have a basic notification system.

Improvements

1. Notification system

Try / Catch is now in the HttpClient module, and therefore in only one place. This removes the impact of having try/catch all over the laravel code.

If an error is thrown, it returns the error but also sends the error to any channel that has been added. Currently there is only a Slack notifier, but adding others should be fairly straight forward. It should be noted this is not using Laravel notifications so its independant and can be used in other frameworks etc..

To get notified by slack, first we need to get a webhook url from api.slack.com and create a webhook to the specific channels we want the error message to go to, then the webhook needs adding in to the .env file like below:

LOG_SLACK_WEBHOOK_URL=https://hooks.slack.com/services/xxxxx

The GuardianProvider.php file needs updating in laravel to add the slack notification as follows:

$api->addNotification( ['slack' => config('logging.channels.slack.url')] );

Notifications will be logged as below:

Screenshot 2022-11-08 at 16 56 19

2. HttpClient

I have removed all the fluent Http calls from the BaseAPIClient.php file - as much as i like the Spatie stuff, i'm not always in love with their coding style (I do love the packages and training they provide !!).

Given PHP 8.x has better coding exposure such as named arguments - its easy to see in the code what is being called, even if the parameters are boolean, so have used dependancy injection and polymorphism to pass objects along in a more OOP style.

All the HttpClient method calls are now in the HttpClient and the services (eg AccountService, MRVService) now call the httpclient directly. This has made the code a lot cleaner.

3. Hmac

Hmac now works as a singleton to remove some of the hard dependancy on httpclient.

What else this means

Having decided to not use try / catches all over the place in the laravel code (I just hated that in the end), it keeps the laravel code the same for now. It could do with some organisational change but i think that can be added to the technical debt and be done at another time.