Skip to content

Commit

Permalink
✨ Introduce Webhook Handling
Browse files Browse the repository at this point in the history
  • Loading branch information
marcreichel committed Jun 3, 2021
1 parent a426457 commit 6b0c2b1
Show file tree
Hide file tree
Showing 119 changed files with 1,931 additions and 3 deletions.
82 changes: 81 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,89 @@ if ($game) {
If you used the Query Builder itself you must check if a property exists
yourself.

## ✨ Webhooks (since v2.3.0)

Since version 2.3.0 of this package you can create webhooks and handle their requests with ease. 🎉

### Initial setup

#### Configuration

Inside your `config/igdb.php` file you need to have a `webhook_secret` of your choice like so (you only need to create this if you upgraded from a prior version of this package. New installations have this configured automatically):

```php
return [
// ...
// Other configs
// ...

'webhook_secret' => env('IGDB_WEBHOOK_SECRET', null)
];
```

And then set a secret inside your `.env` file:

```dotenv
IGDB_WEBHOOK_SECRET=yoursecret
```

#### Routing

Create a POST route where you want to handle the incoming webhook requests and simply call `Webhook::handle($request)`:

```php
use Illuminate\Http\Request;
use MarcReichel\IGDBLaravel\Models\Webhook;

Route::post('webhook/handle', function (Request $request) {
return Webhook::handle($request);
})->name('handle-webhook');
```

That's it!

### Creating a webhook

Let's say we want to be informed whenever a new game is created on https://igdb.com.

First of all we need to inform IGDB that we want to be informed.

For this we create a webhook like so:

```php
use MarcReichel\IGDBLaravel\Models\Game;

Game::createWebhook(route('handle-webhook'), 'create');
```

The first parameter describes the route where we want to handle the webhook.
The second parameter needs to be one of `create`, `update` or `delete` according to
which event we want to listen for.

### Listen for events

Now that we have created our webhook we can listen for a specific event - in our case
when a game is created.

For this we create a Laravel EventListener or for sake of simplicity we just listen for an event
inside the `boot()` method of our `app/providers/EventServiceProvider.php`:

```php
use MarcReichel\IGDBLaravel\Events\GameCreated;
use Illuminate\Support\Facades\Event;

public function boot()
{
Event::listen(function (GameCreated $event) {
// $event->game holds the game data
});
}
```

Further information on how to set up event listeners can be found on the [official docs](https://laravel.com/docs/events).

## TODO List

- Refactor code (beautify code)
- Write unit tests

## Contribution
Expand Down
8 changes: 8 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@
* To turn cache off set this value to 0
*/
'cache_lifetime' => env('IGDB_CACHE_LIFETIME', 3600),

/*
* Default webhook secret.
*
* This needs to be a string of your choice in order to use the webhook
* functionality.
*/
'webhook_secret' => env('IGDB_WEBHOOK_SECRET', null),
];
2 changes: 2 additions & 0 deletions src/ApiHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

class ApiHelper
{
public const IGDB_BASE_URI = 'https://api.igdb.com/v4/';

/**
* Retrieves an Access Token from Twitch.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ protected function init(): void
private function initClient(): void
{
$this->client = new Client([
'base_uri' => 'https://api.igdb.com/v4/',
'base_uri' => ApiHelper::IGDB_BASE_URI,
'headers' => [
'Accept' => 'application/json',
'Client-ID' => config('igdb.credentials.client_id'),
Expand Down
10 changes: 10 additions & 0 deletions src/Enums/Webhook/Method.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace MarcReichel\IGDBLaravel\Enums\Webhook;

class Method
{
public const CREATE = 'create';
public const DELETE = 'delete';
public const UPDATE = 'update';
}
27 changes: 27 additions & 0 deletions src/Events/AgeRatingContentDescriptionCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace MarcReichel\IGDBLaravel\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use MarcReichel\IGDBLaravel\Models\AgeRatingContentDescription;

class AgeRatingContentDescriptionCreated
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* @var AgeRatingContentDescription
*/
public $ageRatingContentDescription;

/**
* @param AgeRatingContentDescription $ageRatingContentDescription
* @return void
*/
public function __construct(AgeRatingContentDescription $ageRatingContentDescription)
{
$this->ageRatingContentDescription = $ageRatingContentDescription;
}
}
8 changes: 8 additions & 0 deletions src/Events/AgeRatingContentDescriptionDeleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace MarcReichel\IGDBLaravel\Events;

class AgeRatingContentDescriptionDeleted extends AgeRatingContentDescriptionCreated
{
//
}
8 changes: 8 additions & 0 deletions src/Events/AgeRatingContentDescriptionUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace MarcReichel\IGDBLaravel\Events;

class AgeRatingContentDescriptionUpdated extends AgeRatingContentDescriptionCreated
{
//
}
27 changes: 27 additions & 0 deletions src/Events/AgeRatingCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace MarcReichel\IGDBLaravel\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use MarcReichel\IGDBLaravel\Models\AgeRating;

class AgeRatingCreated
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* @var AgeRating
*/
public $ageRating;

/**
* @param AgeRating $ageRating
* @return void
*/
public function __construct(AgeRating $ageRating)
{
$this->ageRating = $ageRating;
}
}
8 changes: 8 additions & 0 deletions src/Events/AgeRatingDeleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace MarcReichel\IGDBLaravel\Events;

class AgeRatingDeleted extends AgeRatingCreated
{
//
}
8 changes: 8 additions & 0 deletions src/Events/AgeRatingUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace MarcReichel\IGDBLaravel\Events;

class AgeRatingUpdated extends AgeRatingCreated
{
//
}
27 changes: 27 additions & 0 deletions src/Events/AlternativeNameCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace MarcReichel\IGDBLaravel\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use MarcReichel\IGDBLaravel\Models\AlternativeName;

class AlternativeNameCreated
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* @var AlternativeName
*/
public $alternativeName;

/**
* @param AlternativeName $alternativeName
* @return void
*/
public function __construct(AlternativeName $alternativeName)
{
$this->alternativeName = $alternativeName;
}
}
8 changes: 8 additions & 0 deletions src/Events/AlternativeNameDeleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace MarcReichel\IGDBLaravel\Events;

class AlternativeNameDeleted extends AlternativeNameCreated
{
//
}
8 changes: 8 additions & 0 deletions src/Events/AlternativeNameUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace MarcReichel\IGDBLaravel\Events;

class AlternativeNameUpdated extends AlternativeNameCreated
{
//
}
27 changes: 27 additions & 0 deletions src/Events/ArtworkCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace MarcReichel\IGDBLaravel\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use MarcReichel\IGDBLaravel\Models\Artwork;

class ArtworkCreated
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* @var Artwork
*/
public $artwork;

/**
* @param Artwork $artwork
* @return void
*/
public function __construct(Artwork $artwork)
{
$this->artwork = $artwork;
}
}
8 changes: 8 additions & 0 deletions src/Events/ArtworkDeleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace MarcReichel\IGDBLaravel\Events;

class ArtworkDeleted extends ArtworkCreated
{
//
}
8 changes: 8 additions & 0 deletions src/Events/ArtworkUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace MarcReichel\IGDBLaravel\Events;

class ArtworkUpdated extends ArtworkCreated
{
//
}
27 changes: 27 additions & 0 deletions src/Events/CharacterCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace MarcReichel\IGDBLaravel\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use MarcReichel\IGDBLaravel\Models\Character;

class CharacterCreated
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* @var Character
*/
public $character;

/**
* @param Character $character
* @return void
*/
public function __construct(Character $character)
{
$this->character = $character;
}
}
8 changes: 8 additions & 0 deletions src/Events/CharacterDeleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace MarcReichel\IGDBLaravel\Events;

class CharacterDeleted extends CharacterCreated
{
//
}
27 changes: 27 additions & 0 deletions src/Events/CharacterMugShotCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace MarcReichel\IGDBLaravel\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use MarcReichel\IGDBLaravel\Models\CharacterMugShot;

class CharacterMugShotCreated
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* @var CharacterMugShot
*/
public $characterMugShot;

/**
* @param CharacterMugShot $characterMugShot
* @return void
*/
public function __construct(CharacterMugShot $characterMugShot)
{
$this->characterMugShot = $characterMugShot;
}
}
8 changes: 8 additions & 0 deletions src/Events/CharacterMugShotUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace MarcReichel\IGDBLaravel\Events;

class CharacterMugShotUpdated extends CharacterMugShotCreated
{
//
}
8 changes: 8 additions & 0 deletions src/Events/CharacterUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace MarcReichel\IGDBLaravel\Events;

class CharacterUpdated extends CharacterCreated
{
//
}
Loading

0 comments on commit 6b0c2b1

Please sign in to comment.