Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/request logger #2

Merged
merged 4 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ You can install the package via composer:
```bash
composer require morning-train/laravel-e-conomic
```
### Configuring the package

You can publish the config file with:

Expand All @@ -24,19 +25,42 @@ php artisan vendor:publish --tag="laravel-e-conomic-config"
This is the contents of the published config file:

```php
<?php

return [
'app_secret_token' => env('ECONOMIC_APP_SECRET_TOKEN'),
'agreement_grant_token' => env('ECONOMIC_AGREEMENT_GRANT_TOKEN'),

/*
* This class handles actions on request and response to Economic.
*/
'request_logger' => \Morningtrain\LaravelEconomic\RequestLogger\VoidRequestLogger::class,
];

```

## Usage

### Creating your own request logger

A request logger is any class that implements `\Morningtrain\LaravelEconomic\RequestLogger\RequestLogger`. Here's what that interface looks like.

```php
$laravelEConomic = new Morningtrain\LaravelEConomic();
echo $laravelEConomic->echoPhrase('Hello, Morningtrain!');
use GuzzleHttp\Psr7\Response;
use Illuminate\Http\Client\Request;
use Psr\Http\Message\ResponseInterface;

interface RequestLogger
{
public function onRequest(Request $request);

public function onResponse(Response $response): ResponseInterface;
}

```

After creating your own `RequestLogger` you must register it in the `request_logger` in the `e-conomic` config file.

## Testing

```bash
Expand Down
5 changes: 5 additions & 0 deletions config/e-conomic.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@
return [
'app_secret_token' => env('ECONOMIC_APP_SECRET_TOKEN'),
'agreement_grant_token' => env('ECONOMIC_AGREEMENT_GRANT_TOKEN'),

/*
* This class handles actions on request and response to Economic.
*/
'request_logger' => \Morningtrain\LaravelEconomic\RequestLogger\VoidRequestLogger::class,
];
3 changes: 1 addition & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ parameters:
checkOctaneCompatibility: true
checkModelProperties: true
checkMissingIterableValueType: false
ignoreErrors:
- '#Call to an undefined static method Illuminate\\Support\\Facades\\Http::economic\(\)#'
treatPhpDocTypesAsCertain: false


24 changes: 24 additions & 0 deletions src/LaravelEconomicServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,49 @@

namespace Morningtrain\LaravelEconomic;

use GuzzleHttp\Psr7\Response;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use InvalidArgumentException;
use Morningtrain\Economic\Services\EconomicApiService;
use Morningtrain\LaravelEconomic\Drivers\HttpEconomicDriver;
use Morningtrain\LaravelEconomic\RequestLogger\RequestLogger;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

class LaravelEconomicServiceProvider extends PackageServiceProvider
{
public function boot()
{
$this->app->singleton(RequestLogger::class, config('e-conomic.request_logger'));

Http::macro('economic', function (): PendingRequest {
/** @var RequestLogger $logger */
$logger = app(RequestLogger::class);

if (! is_subclass_of($logger, RequestLogger::class)) {
throw new InvalidArgumentException(
sprintf(
'The request logger must implement the %s interface',
RequestLogger::class
)
);
}

/** @var \Illuminate\Http\Client\Factory $this */
return $this
->withHeaders([
'X-AppSecretToken' => config('e-conomic.app_secret_token'),
'X-AgreementGrantToken' => config('e-conomic.agreement_grant_token'),
'Content-Type' => 'application/json',
])
->beforeSending(
fn (Request $request) => $logger->onRequest($request)
)
->withResponseMiddleware(
fn (Response $response) => $logger->onResponse($response)
)
->throw();
});

Expand Down
14 changes: 14 additions & 0 deletions src/RequestLogger/RequestLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Morningtrain\LaravelEconomic\RequestLogger;

use GuzzleHttp\Psr7\Response;
use Illuminate\Http\Client\Request;
use Psr\Http\Message\ResponseInterface;

interface RequestLogger
{
public function onRequest(Request $request);

public function onResponse(Response $response): ResponseInterface;
}
20 changes: 20 additions & 0 deletions src/RequestLogger/VoidRequestLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Morningtrain\LaravelEconomic\RequestLogger;

use GuzzleHttp\Psr7\Response;
use Illuminate\Http\Client\Request;
use Psr\Http\Message\ResponseInterface;

class VoidRequestLogger implements RequestLogger
{
public function onRequest(Request $request)
{
//
}

public function onResponse(Response $response): ResponseInterface
{
return $response;
}
}
27 changes: 27 additions & 0 deletions tests/TestClasses/TestRequestLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Morningtrain\LaravelEconomic\Tests\TestClasses;

use GuzzleHttp\Psr7\Response;
use Illuminate\Http\Client\Request;
use Morningtrain\LaravelEconomic\RequestLogger\RequestLogger;
use Psr\Http\Message\ResponseInterface;

class TestRequestLogger implements RequestLogger
{
public int $onRequestCalledTimes = 0;

public int $onResponseCalledTimes = 0;

public function onRequest(Request $request)
{
$this->onRequestCalledTimes++;
}

public function onResponse(Response $response): ResponseInterface
{
$this->onResponseCalledTimes++;

return $response;
}
}
23 changes: 23 additions & 0 deletions tests/Unit/RequestLoggerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Illuminate\Support\Facades\Http;
use Morningtrain\Economic\Resources\Customer;
use Morningtrain\LaravelEconomic\RequestLogger\RequestLogger;
use Morningtrain\LaravelEconomic\Tests\TestClasses\TestRequestLogger;

beforeEach(function () {
$this->instance(RequestLogger::class, new TestRequestLogger());
});

it('calls request logger before and after request', function () {
$logger = app(RequestLogger::class);

Http::fake([
'https://restapi.e-conomic.com/customers*' => Http::response([]),
]);

Customer::where('name', 'Morningtrain')->first();

expect($logger->onRequestCalledTimes)->toBe(1);
expect($logger->onResponseCalledTimes)->toBe(1);
});
Loading