Skip to content

Commit fd2d54a

Browse files
authored
feat: add types (web-push-libs#387)
simplify constructor
1 parent 29bd5b1 commit fd2d54a

File tree

5 files changed

+40
-110
lines changed

5 files changed

+40
-110
lines changed

src/MessageSentReport.php

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22
/**
33
* @author Igor Timoshenkov [[email protected]]
44
* @started: 03.09.2018 9:21
@@ -14,35 +14,12 @@
1414
*/
1515
class MessageSentReport implements \JsonSerializable
1616
{
17-
/**
18-
* @var boolean
19-
*/
20-
protected $success;
21-
22-
/**
23-
* @var RequestInterface
24-
*/
25-
protected $request;
26-
27-
/**
28-
* @var ResponseInterface | null
29-
*/
30-
protected $response;
31-
32-
/**
33-
* @var string
34-
*/
35-
protected $reason;
36-
37-
/**
38-
* @param string $reason
39-
*/
40-
public function __construct(RequestInterface $request, ?ResponseInterface $response = null, bool $success = true, $reason = 'OK')
41-
{
42-
$this->request = $request;
43-
$this->response = $response;
44-
$this->success = $success;
45-
$this->reason = $reason;
17+
public function __construct(
18+
protected RequestInterface $request,
19+
protected ?ResponseInterface $response = null,
20+
protected bool $success = true,
21+
protected string $reason = 'OK'
22+
) {
4623
}
4724

4825
public function isSuccess(): bool
@@ -110,11 +87,7 @@ public function getRequestPayload(): string
11087

11188
public function getResponseContent(): ?string
11289
{
113-
if (!$this->response) {
114-
return null;
115-
}
116-
117-
return $this->response->getBody()->getContents();
90+
return $this->response?->getBody()->getContents();
11891
}
11992

12093
public function jsonSerialize(): array

src/Notification.php

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,16 @@
1515

1616
class Notification
1717
{
18-
/** @var SubscriptionInterface */
19-
private $subscription;
20-
21-
/** @var null|string */
22-
private $payload;
23-
24-
/** @var array Options : TTL, urgency, topic */
25-
private $options;
26-
27-
/** @var array Auth details : VAPID */
28-
private $auth;
29-
30-
public function __construct(SubscriptionInterface $subscription, ?string $payload, array $options, array $auth)
31-
{
32-
$this->subscription = $subscription;
33-
$this->payload = $payload;
34-
$this->options = $options;
35-
$this->auth = $auth;
18+
/**
19+
* @param array $options Options: TTL, urgency, topic
20+
* @param array $auth Auth details: VAPID
21+
*/
22+
public function __construct(
23+
private SubscriptionInterface $subscription,
24+
private ?string $payload,
25+
private array $options,
26+
private array $auth
27+
) {
3628
}
3729

3830
public function getSubscription(): SubscriptionInterface

src/Subscription.php

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,21 @@
1515

1616
class Subscription implements SubscriptionInterface
1717
{
18-
/** @var string */
19-
private $endpoint;
20-
21-
/** @var null|string */
22-
private $publicKey;
23-
24-
/** @var null|string */
25-
private $authToken;
26-
27-
/** @var null|string */
28-
private $contentEncoding;
29-
3018
/**
3119
* @param string|null $contentEncoding (Optional) Must be "aesgcm"
3220
* @throws \ErrorException
3321
*/
3422
public function __construct(
35-
string $endpoint,
36-
?string $publicKey = null,
37-
?string $authToken = null,
38-
?string $contentEncoding = null
23+
private string $endpoint,
24+
private ?string $publicKey = null,
25+
private ?string $authToken = null,
26+
private ?string $contentEncoding = null
3927
) {
40-
$this->endpoint = $endpoint;
41-
42-
if ($publicKey || $authToken || $contentEncoding) {
28+
if($publicKey || $authToken || $contentEncoding) {
4329
$supportedContentEncodings = ['aesgcm', 'aes128gcm'];
4430
if ($contentEncoding && !in_array($contentEncoding, $supportedContentEncodings)) {
4531
throw new \ErrorException('This content encoding ('.$contentEncoding.') is not supported.');
4632
}
47-
48-
$this->publicKey = $publicKey;
49-
$this->authToken = $authToken;
5033
$this->contentEncoding = $contentEncoding ?: "aesgcm";
5134
}
5235
}

src/VAPID.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public static function validate(array $vapid): array
9797
* @return array Returns an array with the 'Authorization' and 'Crypto-Key' values to be used as headers
9898
* @throws \ErrorException
9999
*/
100-
public static function getVapidHeaders(string $audience, string $subject, string $publicKey, string $privateKey, string $contentEncoding, ?int $expiration = null)
100+
public static function getVapidHeaders(string $audience, string $subject, string $publicKey, string $privateKey, string $contentEncoding, ?int $expiration = null): array
101101
{
102102
$expirationLimit = time() + 43200; // equal margin of error between 0 and 24h
103103
if (null === $expiration || $expiration > $expirationLimit) {

src/WebPush.php

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,45 +21,38 @@
2121

2222
class WebPush
2323
{
24-
/**
25-
* @var Client
26-
*/
27-
protected $client;
28-
29-
/**
30-
* @var array
31-
*/
32-
protected $auth;
24+
protected Client $client;
25+
protected array $auth;
3326

3427
/**
3528
* @var null|array Array of array of Notifications
3629
*/
37-
protected $notifications;
30+
protected ?array $notifications = null;
3831

3932
/**
40-
* @var array Default options : TTL, urgency, topic, batchSize
33+
* @var array Default options: TTL, urgency, topic, batchSize
4134
*/
42-
protected $defaultOptions;
35+
protected array $defaultOptions;
4336

4437
/**
4538
* @var int Automatic padding of payloads, if disabled, trade security for bandwidth
4639
*/
47-
protected $automaticPadding = Encryption::MAX_COMPATIBILITY_PAYLOAD_LENGTH;
40+
protected int $automaticPadding = Encryption::MAX_COMPATIBILITY_PAYLOAD_LENGTH;
4841

4942
/**
5043
* @var bool Reuse VAPID headers in the same flush session to improve performance
5144
*/
52-
protected $reuseVAPIDHeaders = false;
45+
protected bool $reuseVAPIDHeaders = false;
5346

5447
/**
5548
* @var array Dictionary for VAPID headers cache
5649
*/
57-
protected $vapidHeaders = [];
50+
protected array $vapidHeaders = [];
5851

5952
/**
6053
* WebPush constructor.
6154
*
62-
* @param array $auth Some servers needs authentication
55+
* @param array $auth Some servers need authentication
6356
* @param array $defaultOptions TTL, urgency, topic, batchSize
6457
* @param int|null $timeout Timeout of POST request
6558
*
@@ -281,20 +274,17 @@ public function isAutomaticPadding(): bool
281274
return $this->automaticPadding !== 0;
282275
}
283276

284-
/**
285-
* @return int
286-
*/
287-
public function getAutomaticPadding()
277+
public function getAutomaticPadding(): int
288278
{
289279
return $this->automaticPadding;
290280
}
291281

292282
/**
293-
* @param int|bool $automaticPadding Max padding length
283+
* @param bool|int $automaticPadding Max padding length
294284
*
295285
* @throws \Exception
296286
*/
297-
public function setAutomaticPadding($automaticPadding): WebPush
287+
public function setAutomaticPadding(bool|int $automaticPadding): WebPush
298288
{
299289
if ($automaticPadding > Encryption::MAX_PAYLOAD_LENGTH) {
300290
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).');
@@ -311,20 +301,15 @@ public function setAutomaticPadding($automaticPadding): WebPush
311301
return $this;
312302
}
313303

314-
/**
315-
* @return bool
316-
*/
317-
public function getReuseVAPIDHeaders()
304+
public function getReuseVAPIDHeaders(): bool
318305
{
319306
return $this->reuseVAPIDHeaders;
320307
}
321308

322309
/**
323310
* Reuse VAPID headers in the same flush session to improve performance
324-
*
325-
* @return WebPush
326311
*/
327-
public function setReuseVAPIDHeaders(bool $enabled)
312+
public function setReuseVAPIDHeaders(bool $enabled): WebPush
328313
{
329314
$this->reuseVAPIDHeaders = $enabled;
330315

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

339324
/**
340325
* @param array $defaultOptions Keys 'TTL' (Time To Live, defaults 4 weeks), 'urgency', 'topic', 'batchSize'
341-
*
342-
* @return WebPush
343326
*/
344-
public function setDefaultOptions(array $defaultOptions)
327+
public function setDefaultOptions(array $defaultOptions): WebPush
345328
{
346329
$this->defaultOptions['TTL'] = $defaultOptions['TTL'] ?? 2419200;
347330
$this->defaultOptions['urgency'] = $defaultOptions['urgency'] ?? null;
@@ -357,10 +340,9 @@ public function countPendingNotifications(): int
357340
}
358341

359342
/**
360-
* @return array
361343
* @throws \ErrorException
362344
*/
363-
protected function getVAPIDHeaders(string $audience, string $contentEncoding, array $vapid)
345+
protected function getVAPIDHeaders(string $audience, string $contentEncoding, array $vapid): ?array
364346
{
365347
$vapidHeaders = null;
366348

0 commit comments

Comments
 (0)