Skip to content

Commit 487cc34

Browse files
authored
[1.x] fix unsupported events result in errors (#417)
- Fix #416
1 parent 57bae2b commit 487cc34

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

src/Events/EventFactory.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,14 @@ public function create(ServerNotification $notification): PurchaseEvent
4242
->lower()
4343
->studly()
4444
->prepend(self::NAMESPACES[$provider].'\\');
45-
assert(class_exists($className), new LogicException("Class $className does not exist"));
45+
46+
if (! class_exists($className)) {
47+
return new FallbackEvent($notification);
48+
}
49+
4650
assert(
47-
is_subclass_of($className, PurchaseEvent::class),
48-
new LogicException("Class $className is not a subclass of PurchaseEvent")
51+
is_a($className, PurchaseEvent::class, true),
52+
new LogicException("Invalid event class: $className")
4953
);
5054

5155
return new $className($notification);

src/Events/FallbackEvent.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Imdhemy\Purchases\Events;
6+
7+
final class FallbackEvent extends PurchaseEvent
8+
{
9+
}

tests/Events/EventFactoryTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Imdhemy\Purchases\Events\AppStore\Refund;
2121
use Imdhemy\Purchases\Events\AppStore\Revoke;
2222
use Imdhemy\Purchases\Events\EventFactory;
23+
use Imdhemy\Purchases\Events\FallbackEvent;
2324
use Imdhemy\Purchases\Events\GooglePlay\SubscriptionCanceled;
2425
use Imdhemy\Purchases\Events\GooglePlay\SubscriptionDeferred;
2526
use Imdhemy\Purchases\Events\GooglePlay\SubscriptionExpired;
@@ -109,6 +110,10 @@ public function googlePlayEventsProvider(): array
109110
GoogleNotification::SUBSCRIPTION_EXPIRED,
110111
SubscriptionExpired::class,
111112
],
113+
[
114+
'InvalidType',
115+
FallbackEvent::class,
116+
],
112117
];
113118

114119
foreach ($data as &$item) {
@@ -170,6 +175,10 @@ public function appStoreEventsProvider(): array
170175
AppstoreNotification::REVOKE,
171176
Revoke::class,
172177
],
178+
[
179+
'InvalidType',
180+
FallbackEvent::class,
181+
],
173182
];
174183

175184
foreach ($data as &$item) {

0 commit comments

Comments
 (0)