Skip to content

Commit e5ebd72

Browse files
committed
Prepare for Laravel 9 Upgrade
1 parent cc31971 commit e5ebd72

18 files changed

+184
-98
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
build
2+
.idea/
3+
.phpunit.result.cache
24
composer.lock
35
docs
46
vendor

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ class MyCustomMailgunWebhookJob extends ProcessMailgunWebhookJob
245245
```
246246
### Handling multiple signing secrets
247247

248-
When needed might want to the package to handle multiple endpoints and secrets. Here's how to configurate that behaviour.
248+
When needed might want to the package to handle multiple endpoints and secrets. Here's how to configure that behaviour.
249249

250250
If you are using the `Route::mailgunWebhooks` macro, you can append the `configKey` as follows:
251251

@@ -259,7 +259,7 @@ Alternatively, if you are manually defining the route, you can add `configKey` l
259259
Route::post('webhooks/mailgun/{configKey}', 'BinaryCats\MailgunWebhooks\MailgunWebhooksController');
260260
```
261261

262-
If this route parameter is present the verify middleware will look for the secret using a different config key, by appending the given the parameter value to the default config key. E.g. If Mailgun posts to `webhooks/mailgun/my-named-secret` you'd add a new config named `signing_secret_my-named-secret`.
262+
If this route parameter is present verify middleware will look for the secret using a different config key, by appending the given the parameter value to the default config key. E.g. If Mailgun posts to `webhooks/mailgun/my-named-secret` you'd add a new config named `signing_secret_my-named-secret`.
263263

264264
Example config might look like:
265265

composer.json

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,40 @@
1818
}
1919
],
2020
"require": {
21-
"php": "^7.2|^8.0",
22-
"illuminate/support": "~5.8.0|^6.0|^7.0|^8.0",
21+
"php": "^7.4|^8.0",
22+
"illuminate/support": "^8.0|^9.0",
2323
"spatie/laravel-webhook-client": "^2.0"
2424
},
2525
"require-dev": {
26-
"orchestra/testbench": "~3.8.0|^4.0|^5.0|^6.0",
27-
"phpunit/phpunit": "^8.2|^9.0"
26+
"orchestra/testbench": "^6.0",
27+
"phpunit/phpunit": "^9.3.3"
2828
},
2929
"autoload": {
3030
"psr-4": {
31-
"BinaryCats\\MailgunWebhooks\\": "src"
31+
"BinaryCats\\MailgunWebhooks\\": "src/"
3232
}
3333
},
3434
"autoload-dev": {
3535
"psr-4": {
36-
"BinaryCats\\MailgunWebhooks\\Tests\\": "tests"
36+
"Tests\\": "tests/"
3737
}
3838
},
3939
"suggest": {
4040
"binary-cats/laravel-mail-helpers": "^6.0"
4141
},
4242
"scripts": {
43-
"test": "vendor/bin/phpunit --color=always",
44-
"check": [
45-
"php-cs-fixer fix --ansi --dry-run --diff",
46-
"phpcs --report-width=200 --report-summary --report-full src/ tests/ --standard=PSR2 -n",
47-
"phpmd src/,tests/ text ./phpmd.xml.dist"
48-
],
49-
"fix": [
50-
"php-cs-fixer fix --ansi"
51-
]
43+
"analyze": "./vendor/bin/phpstan analyse src --memory-limit=2G",
44+
"coverage": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-html coverage -d pcov.enabled",
45+
"test": "./vendor/bin/phpunit --color=always -vvv"
5246
},
5347
"config": {
48+
"optimize-autoloader": true,
5449
"sort-packages": true
5550
},
5651
"extra": {
52+
"branch-alias": {
53+
"dev-master": "2.x-dev"
54+
},
5755
"laravel": {
5856
"providers": [
5957
"BinaryCats\\MailgunWebhooks\\MailgunWebhooksServiceProvider"

config/mailgun-webhooks.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
* here. The key is the name of the Mailgun event type with the `.` replaced by a `_`.
1414
*
1515
* You can find a list of Mailgun webhook types here:
16-
* https://documentation.mailgun.com/en/latest/api-webhooks.html#webhooks.
16+
* https://documentation.mailgun.com/en/latest/user_manual.html#events.
17+
*
18+
* The package will automatically convert the keys to lowercase, but you should
19+
* be congnisant of the fact that array keys are case sensitive
1720
*/
1821
'jobs' => [
1922
// 'delivered' => \BinaryCats\MailgunWebhooks\Jobs\HandleDelivered::class,

phpunit.xml

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit bootstrap="vendor/autoload.php"
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
backupGlobals="false"
44
backupStaticAttributes="false"
5+
bootstrap="vendor/autoload.php"
56
colors="true"
6-
verbose="true"
77
convertErrorsToExceptions="true"
88
convertNoticesToExceptions="true"
99
convertWarningsToExceptions="true"
1010
processIsolation="true"
11-
stopOnFailure="false">
11+
stopOnFailure="false"
12+
verbose="true"
13+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
14+
<coverage>
15+
<include>
16+
<directory suffix=".php">src/</directory>
17+
</include>
18+
<report>
19+
<clover outputFile="build/logs/clover.xml"/>
20+
<html outputDirectory="build/coverage"/>
21+
<text outputFile="build/coverage.txt"/>
22+
</report>
23+
</coverage>
1224
<testsuites>
1325
<testsuite name="Binary Cats Test Suite">
1426
<directory>tests</directory>
1527
</testsuite>
1628
</testsuites>
17-
<filter>
18-
<whitelist>
19-
<directory suffix=".php">src/</directory>
20-
</whitelist>
21-
</filter>
2229
<logging>
23-
<log type="tap" target="build/report.tap"/>
24-
<log type="junit" target="build/report.junit.xml"/>
25-
<log type="coverage-html" target="build/coverage"/>
26-
<log type="coverage-text" target="build/coverage.txt"/>
27-
<log type="coverage-clover" target="build/logs/clover.xml"/>
30+
<junit outputFile="build/report.junit.xml"/>
2831
</logging>
2932
</phpunit>

src/Event.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,32 @@
44

55
use BinaryCats\MailgunWebhooks\Contracts\WebhookEvent;
66

7-
class Event implements WebhookEvent
7+
final class Event implements WebhookEvent
88
{
99
/**
1010
* Attributes from the event.
1111
*
12-
* @var array
12+
* @var mixed[]
1313
*/
14-
public $attributes = [];
14+
public array $attributes = [];
1515

1616
/**
1717
* Create new Event.
1818
*
19-
* @param array $attributes
19+
* @param mixed[] $attributes
2020
*/
21-
public function __construct($attributes)
21+
public function __construct(array $attributes)
2222
{
2323
$this->attributes = $attributes;
2424
}
2525

2626
/**
27-
* Construct the event.
27+
* Static event constructor
2828
*
29-
* @return Event
29+
* @param mixed[] $data
30+
* @return static
3031
*/
31-
public static function constructFrom($data): self
32+
public static function constructFrom(array $data): self
3233
{
3334
return new static($data);
3435
}

src/Exceptions/UnexpectedValueException.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
class UnexpectedValueException extends BaseUnexpectedValueException
88
{
9+
/**
10+
* @param \Illuminate\Http\Request $request
11+
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
12+
*/
913
public function render($request)
1014
{
1115
return response(['error' => $this->getMessage()], 400);

src/Exceptions/WebhookFailed.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,46 @@
55
use Exception;
66
use Spatie\WebhookClient\Models\WebhookCall;
77

8-
class WebhookFailed extends Exception
8+
final class WebhookFailed extends Exception
99
{
10+
/**
11+
* @return static
12+
*/
1013
public static function invalidSignature(): self
1114
{
1215
return new static('The signature is invalid.');
1316
}
1417

18+
/**
19+
* @return static
20+
*/
1521
public static function signingSecretNotSet(): self
1622
{
1723
return new static('The webhook signing secret is not set. Make sure that the `signing_secret` config key is set to the correct value.');
1824
}
1925

26+
/**
27+
* @param string $jobClass
28+
* @param \Spatie\WebhookClient\Models\WebhookCall $webhookCall
29+
* @return static
30+
*/
2031
public static function jobClassDoesNotExist(string $jobClass, WebhookCall $webhookCall): self
2132
{
22-
return new static("Could not process webhook id `{$webhookCall->id}` of type `{$webhookCall->type} because the configured jobclass `$jobClass` does not exist.");
33+
return new static("Could not process webhook id `{$webhookCall->getKey()}` of type `{$webhookCall->getAttribute('type')} because the configured jobclass `$jobClass` does not exist.");
2334
}
2435

36+
/**
37+
* @param \Spatie\WebhookClient\Models\WebhookCall $webhookCall
38+
* @return static
39+
*/
2540
public static function missingType(WebhookCall $webhookCall): self
2641
{
27-
return new static("Webhook call id `{$webhookCall->id}` did not contain a type. Valid Mailgun webhook calls should always contain a type.");
42+
return new static("Webhook call id `{$webhookCall->getKey()}` did not contain a type. Valid Mailgun webhook calls should always contain a type.");
2843
}
29-
44+
/**
45+
* @param \Illuminate\Http\Request $request
46+
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
47+
*/
3048
public function render($request)
3149
{
3250
return response(['error' => $this->getMessage()], 400);

src/Jobs/HandleDelivered.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ class HandleDelivered
1313
/**
1414
* Bind the implementation.
1515
*
16-
* @var Spatie\WebhookClient\Models\WebhookCall
16+
* @var \Spatie\WebhookClient\Models\WebhookCall
1717
*/
18-
protected $webhookCall;
18+
protected WebhookCall $webhookCall;
1919

2020
/**
2121
* Create new Job.
2222
*
23-
* @param Spatie\WebhookClient\Models\WebhookCall $webhookCall
23+
* @param \Spatie\WebhookClient\Models\WebhookCall $webhookCall
2424
*/
2525
public function __construct(WebhookCall $webhookCall)
2626
{

src/MailgunSignatureValidator.php

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,11 @@
99

1010
class MailgunSignatureValidator implements SignatureValidator
1111
{
12-
/**
13-
* Bind the implemetation.
14-
*
15-
* @var Illuminate\Http\Request
16-
*/
17-
protected $request;
18-
19-
/**
20-
* Inject the config.
21-
*
22-
* @var Spatie\WebhookClient\WebhookConfig
23-
*/
24-
protected $config;
25-
2612
/**
2713
* True if the signature has been valiates.
2814
*
29-
* @param Illuminate\Http\Request $request
30-
* @param Spatie\WebhookClient\WebhookConfig $config
15+
* @param \Illuminate\Http\Request $request
16+
* @param \Spatie\WebhookClient\WebhookConfig $config
3117
*
3218
* @return bool
3319
*/
@@ -40,6 +26,7 @@ public function isValid(Request $request, WebhookConfig $config): bool
4026
try {
4127
Webhook::constructEvent($request->all(), $signature, $secret);
4228
} catch (Exception $exception) {
29+
// make the app aware
4330
report($exception);
4431

4532
return false;
@@ -52,7 +39,7 @@ public function isValid(Request $request, WebhookConfig $config): bool
5239
* Validate the incoming signature' schema.
5340
*
5441
* @param \Illuminate\Http\Request $request
55-
* @return array
42+
* @return string[]
5643
*/
5744
protected function signature(Request $request): array
5845
{

src/MailgunWebhooksController.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ public function __invoke(Request $request, string $configKey = null)
3030
'process_webhook_job' => config('mailgun-webhooks.process_webhook_job'),
3131
]);
3232

33-
(new WebhookProcessor($request, $webhookConfig))->process();
34-
35-
return response()->json(['message' => 'ok']);
33+
return (new WebhookProcessor($request, $webhookConfig))->process();
3634
}
3735
}

src/ProcessMailgunWebhookJob.php

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use BinaryCats\MailgunWebhooks\Exceptions\WebhookFailed;
66
use Illuminate\Support\Arr;
7+
use Illuminate\Support\Str;
78
use Spatie\WebhookClient\ProcessWebhookJob;
89

910
class ProcessMailgunWebhookJob extends ProcessWebhookJob
@@ -24,29 +25,54 @@ public function handle()
2425
{
2526
$type = Arr::get($this->webhookCall, "payload.{$this->key}");
2627

27-
if (! $type) {
28+
if (!$type) {
2829
throw WebhookFailed::missingType($this->webhookCall);
2930
}
3031

31-
event("mailgun-webhooks::{$type}", $this->webhookCall);
32+
event($this->determineEventKey($type), $this->webhookCall);
3233

3334
$jobClass = $this->determineJobClass($type);
3435

35-
if ($jobClass === '') {
36+
if ('' === $jobClass) {
3637
return;
3738
}
3839

39-
if (! class_exists($jobClass)) {
40+
if (!class_exists($jobClass)) {
4041
throw WebhookFailed::jobClassDoesNotExist($jobClass, $this->webhookCall);
4142
}
4243

4344
dispatch(new $jobClass($this->webhookCall));
4445
}
4546

47+
/**
48+
* @param string $eventType
49+
* @return string
50+
*/
4651
protected function determineJobClass(string $eventType): string
4752
{
48-
$jobConfigKey = str_replace('.', '_', $eventType);
53+
return config($this->determineJobConfigKey($eventType), '');
54+
}
4955

50-
return config("mailgun-webhooks.jobs.{$jobConfigKey}", '');
56+
/**
57+
* @param string $eventType
58+
* @return string
59+
*/
60+
protected function determineJobConfigKey(string $eventType): string
61+
{
62+
return Str::of($eventType)
63+
->replace('.', '_')
64+
->prepend('mailgun-webhooks.jobs.')
65+
->lower();
66+
}
67+
68+
/**
69+
* @param string $eventType
70+
* @return string
71+
*/
72+
protected function determineEventKey(string $eventType): string
73+
{
74+
return Str::of($eventType)
75+
->prepend('mailgun-webhooks::')
76+
->lower();
5177
}
5278
}

0 commit comments

Comments
 (0)