Skip to content

feat: add types #387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 30, 2024
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
43 changes: 8 additions & 35 deletions src/MessageSentReport.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
/**
* @author Igor Timoshenkov [[email protected]]
* @started: 03.09.2018 9:21
Expand All @@ -14,35 +14,12 @@
*/
class MessageSentReport implements \JsonSerializable
{
/**
* @var boolean
*/
protected $success;

/**
* @var RequestInterface
*/
protected $request;

/**
* @var ResponseInterface | null
*/
protected $response;

/**
* @var string
*/
protected $reason;

/**
* @param string $reason
*/
public function __construct(RequestInterface $request, ?ResponseInterface $response = null, bool $success = true, $reason = 'OK')
{
$this->request = $request;
$this->response = $response;
$this->success = $success;
$this->reason = $reason;
public function __construct(
protected RequestInterface $request,
protected ?ResponseInterface $response = null,
protected bool $success = true,
protected string $reason = 'OK'
) {
}

public function isSuccess(): bool
Expand Down Expand Up @@ -110,11 +87,7 @@ public function getRequestPayload(): string

public function getResponseContent(): ?string
{
if (!$this->response) {
return null;
}

return $this->response->getBody()->getContents();
return $this->response?->getBody()->getContents();
}

public function jsonSerialize(): array
Expand Down
28 changes: 10 additions & 18 deletions src/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,16 @@

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

/** @var null|string */
private $payload;

/** @var array Options : TTL, urgency, topic */
private $options;

/** @var array Auth details : VAPID */
private $auth;

public function __construct(SubscriptionInterface $subscription, ?string $payload, array $options, array $auth)
{
$this->subscription = $subscription;
$this->payload = $payload;
$this->options = $options;
$this->auth = $auth;
/**
* @param array $options Options: TTL, urgency, topic
* @param array $auth Auth details: VAPID
*/
public function __construct(
private SubscriptionInterface $subscription,
private ?string $payload,
private array $options,
private array $auth
) {
}

public function getSubscription(): SubscriptionInterface
Expand Down
27 changes: 5 additions & 22 deletions src/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,21 @@

class Subscription implements SubscriptionInterface
{
/** @var string */
private $endpoint;

/** @var null|string */
private $publicKey;

/** @var null|string */
private $authToken;

/** @var null|string */
private $contentEncoding;

/**
* @param string|null $contentEncoding (Optional) Must be "aesgcm"
* @throws \ErrorException
*/
public function __construct(
string $endpoint,
?string $publicKey = null,
?string $authToken = null,
?string $contentEncoding = null
private string $endpoint,
private ?string $publicKey = null,
private ?string $authToken = null,
private ?string $contentEncoding = null
) {
$this->endpoint = $endpoint;

if ($publicKey || $authToken || $contentEncoding) {
if($publicKey || $authToken || $contentEncoding) {
$supportedContentEncodings = ['aesgcm', 'aes128gcm'];
if ($contentEncoding && !in_array($contentEncoding, $supportedContentEncodings)) {
throw new \ErrorException('This content encoding ('.$contentEncoding.') is not supported.');
}

$this->publicKey = $publicKey;
$this->authToken = $authToken;
$this->contentEncoding = $contentEncoding ?: "aesgcm";
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/VAPID.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public static function validate(array $vapid): array
* @return array Returns an array with the 'Authorization' and 'Crypto-Key' values to be used as headers
* @throws \ErrorException
*/
public static function getVapidHeaders(string $audience, string $subject, string $publicKey, string $privateKey, string $contentEncoding, ?int $expiration = null)
public static function getVapidHeaders(string $audience, string $subject, string $publicKey, string $privateKey, string $contentEncoding, ?int $expiration = null): array
{
$expirationLimit = time() + 43200; // equal margin of error between 0 and 24h
if (null === $expiration || $expiration > $expirationLimit) {
Expand Down
50 changes: 16 additions & 34 deletions src/WebPush.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,45 +21,38 @@

class WebPush
{
/**
* @var Client
*/
protected $client;

/**
* @var array
*/
protected $auth;
protected Client $client;
protected array $auth;

/**
* @var null|array Array of array of Notifications
*/
protected $notifications;
protected ?array $notifications = null;

/**
* @var array Default options : TTL, urgency, topic, batchSize
* @var array Default options: TTL, urgency, topic, batchSize
*/
protected $defaultOptions;
protected array $defaultOptions;

/**
* @var int Automatic padding of payloads, if disabled, trade security for bandwidth
*/
protected $automaticPadding = Encryption::MAX_COMPATIBILITY_PAYLOAD_LENGTH;
protected int $automaticPadding = Encryption::MAX_COMPATIBILITY_PAYLOAD_LENGTH;

/**
* @var bool Reuse VAPID headers in the same flush session to improve performance
*/
protected $reuseVAPIDHeaders = false;
protected bool $reuseVAPIDHeaders = false;

/**
* @var array Dictionary for VAPID headers cache
*/
protected $vapidHeaders = [];
protected array $vapidHeaders = [];

/**
* WebPush constructor.
*
* @param array $auth Some servers needs authentication
* @param array $auth Some servers need authentication
* @param array $defaultOptions TTL, urgency, topic, batchSize
* @param int|null $timeout Timeout of POST request
*
Expand Down Expand Up @@ -281,20 +274,17 @@ public function isAutomaticPadding(): bool
return $this->automaticPadding !== 0;
}

/**
* @return int
*/
public function getAutomaticPadding()
public function getAutomaticPadding(): int
{
return $this->automaticPadding;
}

/**
* @param int|bool $automaticPadding Max padding length
* @param bool|int $automaticPadding Max padding length
*
* @throws \Exception
*/
public function setAutomaticPadding($automaticPadding): WebPush
public function setAutomaticPadding(bool|int $automaticPadding): WebPush
{
if ($automaticPadding > Encryption::MAX_PAYLOAD_LENGTH) {
throw new \Exception('Automatic padding is too large. Max is '.Encryption::MAX_PAYLOAD_LENGTH.'. Recommended max is '.Encryption::MAX_COMPATIBILITY_PAYLOAD_LENGTH.' for compatibility reasons (see README).');
Expand All @@ -311,20 +301,15 @@ public function setAutomaticPadding($automaticPadding): WebPush
return $this;
}

/**
* @return bool
*/
public function getReuseVAPIDHeaders()
public function getReuseVAPIDHeaders(): bool
{
return $this->reuseVAPIDHeaders;
}

/**
* Reuse VAPID headers in the same flush session to improve performance
*
* @return WebPush
*/
public function setReuseVAPIDHeaders(bool $enabled)
public function setReuseVAPIDHeaders(bool $enabled): WebPush
{
$this->reuseVAPIDHeaders = $enabled;

Expand All @@ -338,10 +323,8 @@ public function getDefaultOptions(): array

/**
* @param array $defaultOptions Keys 'TTL' (Time To Live, defaults 4 weeks), 'urgency', 'topic', 'batchSize'
*
* @return WebPush
*/
public function setDefaultOptions(array $defaultOptions)
public function setDefaultOptions(array $defaultOptions): WebPush
{
$this->defaultOptions['TTL'] = $defaultOptions['TTL'] ?? 2419200;
$this->defaultOptions['urgency'] = $defaultOptions['urgency'] ?? null;
Expand All @@ -357,10 +340,9 @@ public function countPendingNotifications(): int
}

/**
* @return array
* @throws \ErrorException
*/
protected function getVAPIDHeaders(string $audience, string $contentEncoding, array $vapid)
protected function getVAPIDHeaders(string $audience, string $contentEncoding, array $vapid): ?array
{
$vapidHeaders = null;

Expand Down