Skip to content

Commit

Permalink
[1.x] fix unsupported events result in errors (#417)
Browse files Browse the repository at this point in the history
- Fix #416
  • Loading branch information
imdhemy committed Jun 11, 2024
1 parent 57bae2b commit 487cc34
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Events/EventFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ public function create(ServerNotification $notification): PurchaseEvent
->lower()
->studly()
->prepend(self::NAMESPACES[$provider].'\\');
assert(class_exists($className), new LogicException("Class $className does not exist"));

if (! class_exists($className)) {
return new FallbackEvent($notification);
}

assert(
is_subclass_of($className, PurchaseEvent::class),
new LogicException("Class $className is not a subclass of PurchaseEvent")
is_a($className, PurchaseEvent::class, true),
new LogicException("Invalid event class: $className")
);

return new $className($notification);
Expand Down
9 changes: 9 additions & 0 deletions src/Events/FallbackEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Imdhemy\Purchases\Events;

final class FallbackEvent extends PurchaseEvent
{
}
9 changes: 9 additions & 0 deletions tests/Events/EventFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Imdhemy\Purchases\Events\AppStore\Refund;
use Imdhemy\Purchases\Events\AppStore\Revoke;
use Imdhemy\Purchases\Events\EventFactory;
use Imdhemy\Purchases\Events\FallbackEvent;
use Imdhemy\Purchases\Events\GooglePlay\SubscriptionCanceled;
use Imdhemy\Purchases\Events\GooglePlay\SubscriptionDeferred;
use Imdhemy\Purchases\Events\GooglePlay\SubscriptionExpired;
Expand Down Expand Up @@ -109,6 +110,10 @@ public function googlePlayEventsProvider(): array
GoogleNotification::SUBSCRIPTION_EXPIRED,
SubscriptionExpired::class,
],
[
'InvalidType',
FallbackEvent::class,
],
];

foreach ($data as &$item) {
Expand Down Expand Up @@ -170,6 +175,10 @@ public function appStoreEventsProvider(): array
AppstoreNotification::REVOKE,
Revoke::class,
],
[
'InvalidType',
FallbackEvent::class,
],
];

foreach ($data as &$item) {
Expand Down

0 comments on commit 487cc34

Please sign in to comment.