Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
stability: [prefer-lowest, prefer-stable]
include:
- laravel: 11.*
testbench: 9.*
testbench: ^9.2
- laravel: 12.*
testbench: 10.*

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ composer.phar
composer.lock
.phpunit.result.cache
.phpunit.cache
.phpstan.cache
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
"minishlink/web-push": "^9.0"
},
"require-dev": {
"laravel/pint": "^1.21",
"mockery/mockery": "^1.0",
"orchestra/testbench": "^9.0|^10.0",
"phpunit/phpunit": "^10.5|^11.5.3"
"orchestra/testbench": "^9.2|^10.0",
"larastan/larastan": "^3.1",
"phpunit/phpunit": "^10.5|^11.5.3",
"rector/rector": "^2.0"
},
"autoload": {
"psr-4": {
Expand Down
19 changes: 19 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
parameters:
ignoreErrors:
-
message: '#^Method NotificationChannels\\WebPush\\PushSubscription\:\:findByEndpoint\(\) should return static\(NotificationChannels\\WebPush\\PushSubscription\)\|null but returns NotificationChannels\\WebPush\\PushSubscription\|null\.$#'
identifier: return.type
count: 1
path: src/PushSubscription.php

-
message: '#^Method NotificationChannels\\WebPush\\Test\\User\:\:pushSubscriptions\(\) should return Illuminate\\Database\\Eloquent\\Relations\\MorphMany\<NotificationChannels\\WebPush\\PushSubscription, \$this\(NotificationChannels\\WebPush\\Test\\User\)\> but returns Illuminate\\Database\\Eloquent\\Relations\\MorphMany\<Illuminate\\Database\\Eloquent\\Model, \$this\(NotificationChannels\\WebPush\\Test\\User\)\>\.$#'
identifier: return.type
count: 1
path: tests/User.php

-
message: '#^Unable to resolve the template type TRelatedModel in call to method Illuminate\\Database\\Eloquent\\Model\:\:morphMany\(\)$#'
identifier: argument.templateType
count: 1
path: tests/User.php
14 changes: 14 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
includes:
- ./vendor/larastan/larastan/extension.neon
- ./phpstan-baseline.neon

parameters:

paths:
- src
- tests
- migrations/create_push_subscriptions_table.php.stub

level: 6

tmpDir: .phpstan.cache
16 changes: 16 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use Rector\Config\RectorConfig;

return RectorConfig::configure()
->withPaths([
__DIR__.'/src',
__DIR__.'/tests',
])
->withPreparedSets(
deadCode: true,
codeQuality: true,
codingStyle: true,
typeDeclarations: true,
)
->withPhpSets(php82: true);
27 changes: 5 additions & 22 deletions src/Events/NotificationFailed.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,21 @@
namespace NotificationChannels\WebPush\Events;

use Illuminate\Queue\SerializesModels;
use Minishlink\WebPush\MessageSentReport;
use NotificationChannels\WebPush\PushSubscription;
use NotificationChannels\WebPush\WebPushMessage;

class NotificationFailed
{
use SerializesModels;

/**
* @var \Minishlink\WebPush\MessageSentReport
*/
public $report;

/**
* @var \NotificationChannels\WebPush\PushSubscription
*/
public $subscription;

/**
* @var \NotificationChannels\WebPush\WebPushMessage
*/
public $message;

/**
* Create a new event instance.
*
* @param \Minishlink\WebPush\MessageSentReport $report
* @param \NotificationChannels\WebPush\PushSubscription $subscription
* @param \NotificationChannels\WebPush\WebPushMessage $message
* @return void
*/
public function __construct($report, $subscription, $message)
public function __construct(public MessageSentReport $report, public PushSubscription $subscription, public WebPushMessage $message)
{
$this->report = $report;
$this->subscription = $subscription;
$this->message = $message;
//
}
}
27 changes: 5 additions & 22 deletions src/Events/NotificationSent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,21 @@
namespace NotificationChannels\WebPush\Events;

use Illuminate\Queue\SerializesModels;
use Minishlink\WebPush\MessageSentReport;
use NotificationChannels\WebPush\PushSubscription;
use NotificationChannels\WebPush\WebPushMessage;

class NotificationSent
{
use SerializesModels;

/**
* @var \Minishlink\WebPush\MessageSentReport
*/
public $report;

/**
* @var \NotificationChannels\WebPush\PushSubscription
*/
public $subscription;

/**
* @var \NotificationChannels\WebPush\WebPushMessage
*/
public $message;

/**
* Create a new event instance.
*
* @param \Minishlink\WebPush\MessageSentReport $report
* @param \NotificationChannels\WebPush\PushSubscription $subscription
* @param \NotificationChannels\WebPush\WebPushMessage $message
* @return void
*/
public function __construct($report, $subscription, $message)
public function __construct(public MessageSentReport $report, public PushSubscription $subscription, public WebPushMessage $message)
{
$this->report = $report;
$this->subscription = $subscription;
$this->message = $message;
//
}
}
29 changes: 10 additions & 19 deletions src/HasPushSubscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,25 @@

namespace NotificationChannels\WebPush;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\MorphMany;

trait HasPushSubscriptions
{
/**
* Get all of the subscriptions.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
* @return \Illuminate\Database\Eloquent\Relations\MorphMany<PushSubscription, $this>
*/
public function pushSubscriptions()
public function pushSubscriptions(): MorphMany
{
return $this->morphMany(config('webpush.model'), 'subscribable');
}

/**
* Update (or create) subscription.
*
* @param string $endpoint
* @param string|null $key
* @param string|null $token
* @param string|null $contentEncoding
* @return \NotificationChannels\WebPush\PushSubscription
*/
public function updatePushSubscription($endpoint, $key = null, $token = null, $contentEncoding = null)
public function updatePushSubscription(string $endpoint, ?string $key = null, ?string $token = null, ?string $contentEncoding = null): PushSubscription
{
$subscription = app(config('webpush.model'))->findByEndpoint($endpoint);

Expand All @@ -50,23 +47,17 @@ public function updatePushSubscription($endpoint, $key = null, $token = null, $c

/**
* Determine if the model owns the given subscription.
*
* @param \NotificationChannels\WebPush\PushSubscription $subscription
* @return bool
*/
public function ownsPushSubscription($subscription)
public function ownsPushSubscription(PushSubscription $subscription): bool
{
return (string) $subscription->subscribable_id === (string) $this->getKey() &&
$subscription->subscribable_type === $this->getMorphClass();
}

/**
* Delete subscription by endpoint.
*
* @param string $endpoint
* @return void
*/
public function deletePushSubscription($endpoint)
public function deletePushSubscription(string $endpoint): void
{
$this->pushSubscriptions()
->where('endpoint', $endpoint)
Expand All @@ -76,9 +67,9 @@ public function deletePushSubscription($endpoint)
/**
* Get all of the subscriptions.
*
* @return \Illuminate\Database\Eloquent\Collection
* @return \Illuminate\Database\Eloquent\Collection<array-key, \NotificationChannels\WebPush\PushSubscription>
*/
public function routeNotificationForWebPush()
public function routeNotificationForWebPush(): Collection
{
return $this->pushSubscriptions;
}
Expand Down
25 changes: 11 additions & 14 deletions src/PushSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
namespace NotificationChannels\WebPush;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;

/**
* @property int|string $subscribable_id
* @property class-string $subscribable_type
* @property string $endpoint
* @property string|null $public_key
* @property string|null $auth_token
* @property string|null $content_encoding
* @property \Illuminate\Database\Eloquent\Model $subscribable
*/
class PushSubscription extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'endpoint',
Expand All @@ -28,16 +28,16 @@ class PushSubscription extends Model
/**
* Create a new model instance.
*
* @param array $attributes
* @param array<string, mixed> $attributes
* @return void
*/
public function __construct(array $attributes = [])
{
if (! isset($this->connection)) {
if ($this->connection === null) {
$this->setConnection(config('webpush.database_connection'));
}

if (! isset($this->table)) {
if ($this->table === null) {
$this->setTable(config('webpush.table_name'));
}

Expand All @@ -47,21 +47,18 @@ public function __construct(array $attributes = [])
/**
* Get the model related to the subscription.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
* @return \Illuminate\Database\Eloquent\Relations\MorphTo<\Illuminate\Database\Eloquent\Model, $this>
*/
public function subscribable()
public function subscribable(): MorphTo
{
return $this->morphTo();
}

/**
* Find a subscription by the given endpint.
*
* @param string $endpoint
* @return static|null
* Find a subscription by the given endpoint.
*/
public static function findByEndpoint($endpoint)
public static function findByEndpoint(string $endpoint): ?static
{
return static::where('endpoint', $endpoint)->first();
return static::firstWhere('endpoint', $endpoint);
}
}
19 changes: 4 additions & 15 deletions src/ReportHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,26 @@

namespace NotificationChannels\WebPush;

use Illuminate\Contracts\Events\Dispatcher;
use Minishlink\WebPush\MessageSentReport;
use NotificationChannels\WebPush\Events\NotificationFailed;
use NotificationChannels\WebPush\Events\NotificationSent;

class ReportHandler implements ReportHandlerInterface
{
/**
* @var \Illuminate\Contracts\Events\Dispatcher
*/
protected $events;

/**
* Create a new report handler.
*
* @param \Illuminate\Contracts\Events\Dispatcher $events
* @return void
*/
public function __construct(Dispatcher $events)
public function __construct(protected \Illuminate\Contracts\Events\Dispatcher $events)
{
$this->events = $events;
//
}

/**
* Handle a message sent report.
*
* @param \Minishlink\WebPush\MessageSentReport $report
* @param \NotificationChannels\WebPush\PushSubscription $subscription
* @param \NotificationChannels\WebPush\WebPushMessage $message
* @return void
*/
public function handleReport($report, $subscription, $message)
public function handleReport(MessageSentReport $report, PushSubscription $subscription, WebPushMessage $message): void
{
if ($report->isSuccess()) {
$this->events->dispatch(new NotificationSent($report, $subscription, $message));
Expand Down
9 changes: 3 additions & 6 deletions src/ReportHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

namespace NotificationChannels\WebPush;

use Minishlink\WebPush\MessageSentReport;

interface ReportHandlerInterface
{
/**
* Handle a message sent report.
*
* @param \Minishlink\WebPush\MessageSentReport $report
* @param \NotificationChannels\WebPush\PushSubscription $subscription
* @param \NotificationChannels\WebPush\WebPushMessage $message
* @return void
*/
public function handleReport($report, $subscription, $message);
public function handleReport(MessageSentReport $report, PushSubscription $subscription, WebPushMessage $message): void;
}
Loading