Skip to content

Commit

Permalink
web-push-libs#267: Define the SubscriptionInterface (web-push-libs#268
Browse files Browse the repository at this point in the history
)

* web-push-libs#267: Define the `SubscriptionInterface`

* Rename Composer package

* Revert the package name

* Revert the package name
  • Loading branch information
BR0kEN- authored Aug 2, 2020
1 parent 3615e3c commit c3372d7
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 15 deletions.
10 changes: 5 additions & 5 deletions src/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class Notification
{
/** @var Subscription */
/** @var SubscriptionInterface */
private $subscription;

/** @var null|string */
Expand All @@ -30,12 +30,12 @@ class Notification
/**
* Notification constructor.
*
* @param Subscription $subscription
* @param SubscriptionInterface $subscription
* @param null|string $payload
* @param array $options
* @param array $auth
*/
public function __construct(Subscription $subscription, ?string $payload, array $options, array $auth)
public function __construct(SubscriptionInterface $subscription, ?string $payload, array $options, array $auth)
{
$this->subscription = $subscription;
$this->payload = $payload;
Expand All @@ -44,9 +44,9 @@ public function __construct(Subscription $subscription, ?string $payload, array
}

/**
* @return Subscription
* @return SubscriptionInterface
*/
public function getSubscription(): Subscription
public function getSubscription(): SubscriptionInterface
{
return $this->subscription;
}
Expand Down
14 changes: 7 additions & 7 deletions src/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Minishlink\WebPush;

class Subscription
class Subscription implements SubscriptionInterface
{
/** @var string */
private $endpoint;
Expand Down Expand Up @@ -60,10 +60,10 @@ public function __construct(
* Subscription factory.
*
* @param array $associativeArray (with keys endpoint, publicKey, authToken, contentEncoding)
* @return Subscription
* @return self
* @throws \ErrorException
*/
public static function create(array $associativeArray): Subscription
public static function create(array $associativeArray): self
{
if (array_key_exists('keys', $associativeArray) && is_array($associativeArray['keys'])) {
return new self(
Expand All @@ -89,31 +89,31 @@ public static function create(array $associativeArray): Subscription
}

/**
* @return string
* {@inheritDoc}
*/
public function getEndpoint(): string
{
return $this->endpoint;
}

/**
* @return null|string
* {@inheritDoc}
*/
public function getPublicKey(): ?string
{
return $this->publicKey;
}

/**
* @return null|string
* {@inheritDoc}
*/
public function getAuthToken(): ?string
{
return $this->authToken;
}

/**
* @return null|string
* {@inheritDoc}
*/
public function getContentEncoding(): ?string
{
Expand Down
39 changes: 39 additions & 0 deletions src/SubscriptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

/*
* This file is part of the WebPush library.
*
* (c) Louis Lagrange <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Minishlink\WebPush;

/**
* @author Sergii Bondarenko <[email protected]>
*/
interface SubscriptionInterface {
/**
* @return string
*/
public function getEndpoint(): string;

/**
* @return null|string
*/
public function getPublicKey(): ?string;

/**
* @return null|string
*/
public function getAuthToken(): ?string;

/**
* @return null|string
*/
public function getContentEncoding(): ?string;
}
4 changes: 2 additions & 2 deletions src/WebPush.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function __construct(array $auth = [], array $defaultOptions = [], ?int $
/**
* Send a notification.
*
* @param Subscription $subscription
* @param SubscriptionInterface $subscription
* @param string|null $payload If you want to send an array, json_encode it
* @param bool $flush If you want to flush directly (usually when you send only one notification)
* @param array $options Array with several options tied to this notification. If not set, will use the default options that you can set in the WebPush object
Expand All @@ -115,7 +115,7 @@ public function __construct(array $auth = [], array $defaultOptions = [], ?int $
*
* @throws \ErrorException
*/
public function sendNotification(Subscription $subscription, ?string $payload = null, bool $flush = false, array $options = [], array $auth = [])
public function sendNotification(SubscriptionInterface $subscription, ?string $payload = null, bool $flush = false, array $options = [], array $auth = [])
{
if (isset($payload)) {
if (Utils::safeStrlen($payload) > Encryption::MAX_PAYLOAD_LENGTH) {
Expand Down
3 changes: 2 additions & 1 deletion tests/WebPushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Minishlink\WebPush\WebPush;
use Minishlink\WebPush\Subscription;
use Minishlink\WebPush\SubscriptionInterface;

final class WebPushTest extends PHPUnit\Framework\TestCase
{
Expand Down Expand Up @@ -88,7 +89,7 @@ public function notificationProvider(): array
/**
* @dataProvider notificationProvider
*
* @param Subscription $subscription
* @param SubscriptionInterface $subscription
* @param string $payload
* @throws ErrorException
*/
Expand Down

0 comments on commit c3372d7

Please sign in to comment.