Skip to content

Commit fd80fce

Browse files
committed
✨ Finalize v3
1 parent b2df4ab commit fd80fce

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+701
-336
lines changed

README.md

+107-87
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
[![GitHub](https://img.shields.io/github/license/marcreichel/igdb-laravel)](https://packagist.org/packages/marcreichel/igdb-laravel)
88
[![Gitmoji](https://img.shields.io/badge/gitmoji-%20😜%20😍-FFDD67.svg)](https://gitmoji.dev)
99

10-
This is a Laravel wrapper for version 4 of the [IGDB API](https://api-docs.igdb.com/) (Apicalypse) including [webhook handling](#-webhooks-since-v230) since version 2.3.0.
10+
This is a Laravel wrapper for version 4 of the [IGDB API](https://api-docs.igdb.com/) (Apicalypse)
11+
including [webhook handling](#-webhooks-since-v230) since version 2.3.0.
1112

1213
## Basic installation
1314

@@ -45,6 +46,11 @@ return [
4546
*/
4647
'cache_lifetime' => env('IGDB_CACHE_LIFETIME', 3600),
4748

49+
/*
50+
* Path where the webhooks should be handled.
51+
*/
52+
'webhook_path' => 'igdb-webhook/handle',
53+
4854
/*
4955
* The webhook secret.
5056
*
@@ -58,8 +64,8 @@ return [
5864
## Usage
5965

6066
If you're familiar with the [Eloquent System](https://laravel.com/docs/eloquent)
61-
and the [Query Builder](https://laravel.com/docs/queries) of Laravel you
62-
will love this package as it uses a similar approach.
67+
and the [Query Builder](https://laravel.com/docs/queries) of Laravel you will love this package as it uses a similar
68+
approach.
6369

6470
### Models
6571

@@ -77,9 +83,8 @@ $games = Game::where('first_release_date', '>=', 1546297200)->get();
7783

7884
### Query Builder
7985

80-
You can use one of the defined models listed above. The search results will be
81-
mapped into the used model automatically then. This method is used in the
82-
examples below.
86+
You can use one of the defined models listed above. The search results will be mapped into the used model automatically
87+
then. This method is used in the examples below.
8388

8489
Otherwise you can use the Query Builder itself like this:
8590

@@ -93,10 +98,9 @@ $games = $igdb->get();
9398

9499
#### Select
95100

96-
Select which fields should be in the response. If you want to have all available
97-
fields in the response you can also skip this method as the query builder will
98-
select `*` by default. (**Attention**: This is the opposite behaviour from the
99-
Apicalypse API)
101+
Select which fields should be in the response. If you want to have all available fields in the response you can also
102+
skip this method as the query builder will select `*` by default. (**Attention**: This is the opposite behaviour from
103+
the Apicalypse API)
100104

101105
```php
102106
use MarcReichel\IGDBLaravel\Models\Game;
@@ -126,8 +130,8 @@ use MarcReichel\IGDBLaravel\Models\Game;
126130
$games = Game::where('first_release_date', '>=', 1546297200)->get();
127131
```
128132

129-
For convenience, if you want to verify that a column is equal to a given value,
130-
you may pass the value directly as the second argument to the `where` method:
133+
For convenience, if you want to verify that a column is equal to a given value, you may pass the value directly as the
134+
second argument to the `where` method:
131135

132136
```php
133137
use MarcReichel\IGDBLaravel\Models\Game;
@@ -137,8 +141,8 @@ $games = Game::where('name', 'Fortnite')->get();
137141

138142
##### Or Statements
139143

140-
You may chain where constraints together as well as add `or` clauses to the query.
141-
The `orWhere` method accepts the same arguments as the `where` method:
144+
You may chain where constraints together as well as add `or` clauses to the query. The `orWhere` method accepts the same
145+
arguments as the `where` method:
142146

143147
```php
144148
use MarcReichel\IGDBLaravel\Models\Game;
@@ -160,8 +164,7 @@ $games = Game::whereBetween('first_release_date', 1546297200, 1577833199)->get()
160164

161165
###### whereNotBetween
162166

163-
The `whereNotBetween` method verifies that a field's value lies outside of two
164-
values:
167+
The `whereNotBetween` method verifies that a field's value lies outside of two values:
165168

166169
```php
167170
use MarcReichel\IGDBLaravel\Models\Game;
@@ -171,8 +174,7 @@ $games = Game::whereNotBetween('first_release_date', 1546297200, 1577833199)->ge
171174

172175
###### whereIn
173176

174-
The `whereIn` method verifies that a given field's value is contained within the
175-
given array:
177+
The `whereIn` method verifies that a given field's value is contained within the given array:
176178

177179
```php
178180
use MarcReichel\IGDBLaravel\Models\Game;
@@ -182,7 +184,7 @@ $games = Game::whereIn('category', [0,4])->get();
182184

183185
###### whereNotIn
184186

185-
The `whereNotIn` method verifies that the given field's value is **not**
187+
The `whereNotIn` method verifies that the given field's value is **not**
186188
contained in the given array:
187189

188190
```php
@@ -193,7 +195,7 @@ $games = Game::whereNotIn('category', [0,4])->get();
193195

194196
###### whereInAll / whereNotInAll / whereInExact / whereNotInExact
195197

196-
Alternatively you could use one of these methods to match against **all** or **exactly** the given array.
198+
Alternatively you could use one of these methods to match against **all** or **exactly** the given array.
197199

198200
###### whereNull
199201

@@ -227,8 +229,7 @@ $games = Game::whereDate('first_release_date', '2019-01-01')->get();
227229

228230
###### whereYear
229231

230-
The `whereYear` method may be used to compare a fields's value against a specific
231-
year:
232+
The `whereYear` method may be used to compare a fields's value against a specific year:
232233

233234
```php
234235
use MarcReichel\IGDBLaravel\Models\Game;
@@ -238,8 +239,7 @@ $games = Game::whereYear('first_release_date', 2019)->get();
238239

239240
###### whereHas / whereHasNot
240241

241-
These methods have the same syntax as `whereNull` and `whereNotNull` and literally
242-
do the exact same thing.
242+
These methods have the same syntax as `whereNull` and `whereNotNull` and literally do the exact same thing.
243243

244244
##### Parameter Grouping
245245

@@ -253,13 +253,13 @@ $games = Game::where('name', 'Fortnite')
253253
})->get();
254254
```
255255

256-
#### Ordering, Limit, & Offset
256+
#### Ordering, Limit & Offset
257257

258258
##### orderBy
259259

260-
The `orderBy` method allows you to sort the result of the query by a given field.
261-
The first argument to the `orderBy` method should be the field you wish to sort
262-
by, while the second argument controls the direction of the sort and may be either
260+
The `orderBy` method allows you to sort the result of the query by a given field. The first argument to the `orderBy`
261+
method should be the field you wish to sort by, while the second argument controls the direction of the sort and may be
262+
either
263263
`asc` or `desc`:
264264

265265
```php
@@ -270,8 +270,8 @@ $games = Game::orderBy('first_release_date', 'asc')->get();
270270

271271
##### skip / take
272272

273-
To limit the number of results returned from the query, or to skip a given
274-
number of results in the query, you may use the `skip` and `take` methods (`take` is limited to a maximum of 500):
273+
To limit the number of results returned from the query, or to skip a given number of results in the query, you may use
274+
the `skip` and `take` methods (`take` is limited to a maximum of 500):
275275

276276
```php
277277
use MarcReichel\IGDBLaravel\Models\Game;
@@ -289,8 +289,7 @@ $games = Game::offset(10)->limit(5)->get();
289289

290290
#### Cache
291291

292-
You can overwrite the default cache time for one specific query. So you can for
293-
example turn off caching for a query:
292+
You can overwrite the default cache time for one specific query. So you can for example turn off caching for a query:
294293

295294
```php
296295
use MarcReichel\IGDBLaravel\Models\Game;
@@ -331,8 +330,7 @@ $game = Game::first();
331330

332331
#### Find
333332

334-
If you know the Identifier of the model you can simply call the `find`-method
335-
with the identifier as a parameter:
333+
If you know the Identifier of the model you can simply call the `find`-method with the identifier as a parameter:
336334

337335
```php
338336
use MarcReichel\IGDBLaravel\Models\Game;
@@ -342,10 +340,9 @@ $game = Game::find(1905);
342340

343341
##### FindOrFail
344342

345-
`find` returns `null` if no result were found. If you want to throw an Exception
346-
instead use `findOrFail`. This will throw an
347-
`MarcReichel\IGDBLaravel\Exceptions\ModelNotFoundException` if no result were
348-
found.
343+
`find` returns `null` if no result were found. If you want to throw an Exception instead use `findOrFail`. This will
344+
throw an
345+
`MarcReichel\IGDBLaravel\Exceptions\ModelNotFoundException` if no result were found.
349346

350347
#### Relationships (Extends)
351348

@@ -357,9 +354,8 @@ use MarcReichel\IGDBLaravel\Models\Game;
357354
$game = Game::with(['cover', 'artworks'])->get();
358355
```
359356

360-
By default, every field (`*`) of the relationship is selected.
361-
If you want to define the fields of the relationship yourself you have to define
362-
the relationship as the array-key and the fields as an array:
357+
By default, every field (`*`) of the relationship is selected. If you want to define the fields of the relationship
358+
yourself you have to define the relationship as the array-key and the fields as an array:
363359

364360
```php
365361
use MarcReichel\IGDBLaravel\Models\Game;
@@ -397,8 +393,7 @@ if ($game) {
397393

398394
#### Query-Builder-based approach
399395

400-
If you used the Query Builder itself you must check if a property exists
401-
yourself.
396+
If you used the Query Builder itself you must check if a property exists yourself.
402397

403398
## ✨ Webhooks (since v2.3.0)
404399

@@ -408,15 +403,28 @@ Since version 2.3.0 of this package you can create webhooks and handle their req
408403

409404
#### Configuration
410405

411-
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):
406+
Inside your `config/igdb.php` file you need to have a `webhook_path` and `webhook_secret` of your choice like so (you
407+
only need to create this if you upgraded from a prior version of this package. New installations have this configured
408+
automatically):
412409

413410
```php
414411
return [
415412
// ...
416413
// Other configs
417414
// ...
418-
419-
'webhook_secret' => env('IGDB_WEBHOOK_SECRET', null)
415+
416+
/*
417+
* Path where the webhooks should be handled.
418+
*/
419+
'webhook_path' => 'igdb-webhook/handle',
420+
421+
/*
422+
* The webhook secret.
423+
*
424+
* This needs to be a string of your choice in order to use the webhook
425+
* functionality.
426+
*/
427+
'webhook_secret' => env('IGDB_WEBHOOK_SECRET', null),
420428
];
421429
```
422430

@@ -426,35 +434,6 @@ And then set a secret inside your `.env` file:
426434
IGDB_WEBHOOK_SECRET=yoursecret
427435
```
428436

429-
#### Routing
430-
431-
Create a POST route where you want to handle the incoming webhook requests and simply call `Webhook::handle($request)`:
432-
433-
```php
434-
use Illuminate\Http\Request;
435-
use MarcReichel\IGDBLaravel\Models\Webhook;
436-
437-
Route::post('webhook/handle', function (Request $request) {
438-
return Webhook::handle($request);
439-
})->name('handle-webhook');
440-
```
441-
442-
Add the route to the `VerifyCsrfToken` middleware (in `app/Http/Middleware/VerifyCsrfToken.php`):
443-
444-
```diff
445-
class VerifyCsrfToken extends Middleware
446-
{
447-
/**
448-
* The URIs that should be excluded from CSRF verification.
449-
*
450-
* @var array
451-
*/
452-
protected $except = [
453-
+ 'webhook/handle'
454-
];
455-
}
456-
```
457-
458437
That's it!
459438

460439
### Creating a webhook
@@ -463,25 +442,28 @@ Let's say we want to be informed whenever a new game is created on https://igdb.
463442

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

466-
For this we create a webhook like so:
445+
For this we create a webhook like so (for example inside a controller):
467446

468447
```php
448+
use MarcReichel\IGDBLaravel\Enums\Webhook\Method;
469449
use MarcReichel\IGDBLaravel\Models\Game;
450+
use Illuminate\Routing\Controller;
470451

471-
Game::createWebhook(route('handle-webhook'), 'create');
452+
class ExampleController extends Controller
453+
{
454+
public function createWebhook()
455+
{
456+
Game::createWebhook(Method::CREATE)
457+
}
458+
}
472459
```
473460

474-
The first parameter describes the route where we want to handle the webhook.
475-
The second parameter needs to be one of `create`, `update` or `delete` according to
476-
which event we want to listen for.
477-
478461
### Listen for events
479462

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

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

486468
```php
487469
use MarcReichel\IGDBLaravel\Events\GameCreated;
@@ -490,14 +472,52 @@ use Illuminate\Support\Facades\Event;
490472
public function boot()
491473
{
492474
Event::listen(function (GameCreated $event) {
493-
// $event->game holds the game data
475+
// $event->data holds the (unexpanded!) data (of the game in this case)
494476
});
495477
}
496478
```
497479

498480
[Here](src/Events) you can find a list of all available events.
499481

500-
Further information on how to set up event listeners can be found on the [official docs](https://laravel.com/docs/events).
482+
Further information on how to set up event listeners can be found on
483+
the [official docs](https://laravel.com/docs/events).
484+
485+
### Manage webhooks via CLI
486+
487+
#### List your webhooks
488+
489+
```bash
490+
$ php artisan igdb:webhooks
491+
```
492+
493+
#### Create a webhook
494+
495+
```bash
496+
$ php artisan igdb:webhooks:create {model?} {--method=}
497+
```
498+
499+
You can also just call `php artisan igdb:webhooks:create` without any arguments. The command will then ask for the
500+
required data interactively.
501+
502+
The `model` parameter needs to be the (studly cased) class name of a model (e.g. `Game`).
503+
504+
The `--method` option needs to be one of `create`, `update` or `delete` accordingly for which event you want to listen.
505+
506+
#### Reactivate a webhook
507+
508+
```bash
509+
$ php artisan igdb:webhooks:reactivate {id}
510+
```
511+
512+
For `{id}` insert the id of the (inactive) webhook.
513+
514+
#### Delete a webhook
515+
516+
```bash
517+
$ php artisan igdb:webhooks:delete {id?} {--A|all}
518+
```
519+
520+
You may provide the `id` of a webhook to delete it or use the `-A`/`--all` flag to delete all your registered webhooks.
501521

502522
## Testing
503523

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
],
1212
"type": "library",
1313
"require": {
14-
"php": "^7.3 | ^7.4 | ^8.0",
14+
"php": "^8.0",
1515
"laravel/framework": "^8.40.0",
1616
"guzzlehttp/guzzle": "~6.0|~7.0",
1717
"ext-json": "*"

0 commit comments

Comments
 (0)