From eed7f16d65cd5d866049defe6a9138732fcdb28a Mon Sep 17 00:00:00 2001
From: Beno!t POLASZEK
- * An offset to check for.
- *
- * The return value will be casted to boolean if non-boolean was returned. + * An offset to check for. + *
+ * + * @return bool true on success or false on failure. + * + *+ * The return value will be casted to boolean if non-boolean was returned. + * * @since 5.0.0 */ public function offsetExists($offset) @@ -143,12 +120,16 @@ public function offsetExists($offset) } /** - * Offset to retrieve - * @link https://php.net/manual/en/arrayaccess.offsetget.php + * Offset to retrieve. + * + * @see https://php.net/manual/en/arrayaccess.offsetget.php + * * @param mixed $offset
- * The offset to retrieve. - *
- * @return mixed Can return all value types. + * The offset to retrieve. + * + * + * @return mixed can return all value types + * * @since 5.0.0 */ public function offsetGet($offset) @@ -157,15 +138,19 @@ public function offsetGet($offset) } /** - * Offset to set - * @link https://php.net/manual/en/arrayaccess.offsetset.php + * Offset to set. + * + * @see https://php.net/manual/en/arrayaccess.offsetset.php + * * @param mixed $offset- * The offset to assign the value to. - *
- * @param mixed $value- * The value to set. - *
+ * The offset to assign the value to. + * + * @param mixed $value+ * The value to set. + *
+ * * @return void + * * @since 5.0.0 */ public function offsetSet($offset, $value) @@ -174,12 +159,16 @@ public function offsetSet($offset, $value) } /** - * Offset to unset - * @link https://php.net/manual/en/arrayaccess.offsetunset.php + * Offset to unset. + * + * @see https://php.net/manual/en/arrayaccess.offsetunset.php + * * @param mixed $offset- * The offset to unset. - *
+ * The offset to unset. + * + * * @return void + * * @since 5.0.0 */ public function offsetUnset($offset) diff --git a/src/Model/Response/PushResponse.php b/src/Model/Response/PushResponse.php index 09dc499..a3aeb26 100644 --- a/src/Model/Response/PushResponse.php +++ b/src/Model/Response/PushResponse.php @@ -25,8 +25,6 @@ final class PushResponse /** * WebPushResponse constructor. - * @param UserSubscriptionInterface $subscription - * @param int $statusCode */ public function __construct(UserSubscriptionInterface $subscription, int $statusCode) { @@ -34,33 +32,21 @@ public function __construct(UserSubscriptionInterface $subscription, int $status $this->statusCode = $statusCode; } - /** - * @return UserSubscriptionInterface - */ public function getSubscription(): UserSubscriptionInterface { return $this->subscription; } - /** - * @return int - */ public function getStatusCode(): int { return $this->statusCode; } - /** - * @return bool - */ public function isExpired(): bool { return in_array($this->statusCode, [self::NOT_FOUND, self::GONE]); } - /** - * @return bool - */ public function isSuccessFul(): bool { return self::SUCCESS === $this->statusCode; diff --git a/src/Model/Subscription/UserSubscriptionInterface.php b/src/Model/Subscription/UserSubscriptionInterface.php index 92c449e..77ef6dd 100644 --- a/src/Model/Subscription/UserSubscriptionInterface.php +++ b/src/Model/Subscription/UserSubscriptionInterface.php @@ -6,46 +6,33 @@ interface UserSubscriptionInterface { - /** * Return the user associated to this subscription. - * - * @return UserInterface */ public function getUser(): UserInterface; /** * Return the hash of this subscription. Can be a fingerprint or a cookie. - * - * @return string */ public function getSubscriptionHash(): string; /** * Return the subscriber's HTTP endpoint. - * - * @return string */ public function getEndpoint(): string; /** * Return the subscriber's public key. - * - * @return string */ public function getPublicKey(): string; /** * Return the subscriber auth token. - * - * @return string */ public function getAuthToken(): string; /** * Content-encoding (default: aesgcm). - * - * @return string */ public function getContentEncoding(): string; } diff --git a/src/Model/Subscription/UserSubscriptionManagerInterface.php b/src/Model/Subscription/UserSubscriptionManagerInterface.php index 66a6156..307e457 100644 --- a/src/Model/Subscription/UserSubscriptionManagerInterface.php +++ b/src/Model/Subscription/UserSubscriptionManagerInterface.php @@ -6,42 +6,26 @@ interface UserSubscriptionManagerInterface { - /** * Create a user/subscription association. - * - * @param UserInterface $user - * @param string $subscriptionHash - * @param array $subscription - * @param array $options - * @return UserSubscriptionInterface */ public function factory(UserInterface $user, string $subscriptionHash, array $subscription, array $options = []): UserSubscriptionInterface; /** * Return a string representation of the subscription's endpoint. * Example: md5($endpoint). - * - * @param string $endpoint - * @param UserInterface $user - * @return string */ public function hash(string $endpoint, UserInterface $user): string; /** * Return the subscription attached to this user. - * - * @param UserInterface $user - * @param string $subscriptionHash - * @return UserSubscriptionInterface|null */ public function getUserSubscription(UserInterface $user, string $subscriptionHash): ?UserSubscriptionInterface; /** * Return the list of known subscriptions for this user. - * A user can have several subscriptions (on chrome, firefox, etc.) + * A user can have several subscriptions (on chrome, firefox, etc.). * - * @param UserInterface $user * @return iterable|UserSubscriptionInterface[] */ public function findByUser(UserInterface $user): iterable; @@ -49,23 +33,16 @@ public function findByUser(UserInterface $user): iterable; /** * Return the list of known subscriptions for this hash. * Several users can share the same hash. - * - * @param string $subscriptionHash - * @return iterable */ public function findByHash(string $subscriptionHash): iterable; /** * Store the user/subscription association. - * - * @param UserSubscriptionInterface $userSubscription */ public function save(UserSubscriptionInterface $userSubscription): void; /** * Remove the user/subscription association. - * - * @param UserSubscriptionInterface $userSubscription */ public function delete(UserSubscriptionInterface $userSubscription): void; } diff --git a/src/Model/Subscription/UserSubscriptionManagerRegistry.php b/src/Model/Subscription/UserSubscriptionManagerRegistry.php index d4f174e..b507c9f 100644 --- a/src/Model/Subscription/UserSubscriptionManagerRegistry.php +++ b/src/Model/Subscription/UserSubscriptionManagerRegistry.php @@ -15,8 +15,6 @@ final class UserSubscriptionManagerRegistry implements UserSubscriptionManagerIn private $registry = []; /** - * @param string $userClass - * @param UserSubscriptionManagerInterface $userSubscriptionManager * @throws \InvalidArgumentException */ public function register(string $userClass, UserSubscriptionManagerInterface $userSubscriptionManager) @@ -38,7 +36,7 @@ public function register(string $userClass, UserSubscriptionManagerInterface $us /** * @param UserInterface|string $userClass - * @return UserSubscriptionManagerInterface + * * @throws RuntimeException * @throws ServiceNotFoundException * @throws \InvalidArgumentException @@ -47,13 +45,7 @@ public function register(string $userClass, UserSubscriptionManagerInterface $us public function getManager($userClass): UserSubscriptionManagerInterface { if (!is_a($userClass, UserInterface::class, true)) { - throw new \InvalidArgumentException( - sprintf( - 'Expected class or object that implements %s, %s given', - UserInterface::class, - is_object($userClass) ? get_class($userClass) : gettype($userClass) - ) - ); + throw new \InvalidArgumentException(sprintf('Expected class or object that implements %s, %s given', UserInterface::class, is_object($userClass) ? get_class($userClass) : gettype($userClass))); } if (is_object($userClass)) { @@ -73,7 +65,7 @@ public function getManager($userClass): UserSubscriptionManagerInterface } /** - * @inheritDoc + * {@inheritdoc} */ public function factory(UserInterface $user, string $subscriptionHash, array $subscription, array $options = []): UserSubscriptionInterface { @@ -81,7 +73,7 @@ public function factory(UserInterface $user, string $subscriptionHash, array $su } /** - * @inheritDoc + * {@inheritdoc} */ public function hash(string $endpoint, UserInterface $user): string { @@ -89,7 +81,7 @@ public function hash(string $endpoint, UserInterface $user): string } /** - * @inheritDoc + * {@inheritdoc} */ public function getUserSubscription(UserInterface $user, string $subscriptionHash): ?UserSubscriptionInterface { @@ -97,7 +89,7 @@ public function getUserSubscription(UserInterface $user, string $subscriptionHas } /** - * @inheritDoc + * {@inheritdoc} */ public function findByUser(UserInterface $user): iterable { @@ -105,7 +97,7 @@ public function findByUser(UserInterface $user): iterable } /** - * @inheritDoc + * {@inheritdoc} */ public function findByHash(string $subscriptionHash): iterable { @@ -117,7 +109,7 @@ public function findByHash(string $subscriptionHash): iterable } /** - * @inheritDoc + * {@inheritdoc} */ public function save(UserSubscriptionInterface $userSubscription): void { @@ -125,7 +117,7 @@ public function save(UserSubscriptionInterface $userSubscription): void } /** - * @inheritDoc + * {@inheritdoc} */ public function delete(UserSubscriptionInterface $userSubscription): void { diff --git a/src/Sender/NullPushMessageSender.php b/src/Sender/NullPushMessageSender.php index 2e04418..5d17afe 100644 --- a/src/Sender/NullPushMessageSender.php +++ b/src/Sender/NullPushMessageSender.php @@ -9,7 +9,7 @@ final class NullPushMessageSender implements PushMessagerSenderInterface /** * Does nothing. * - * @inheritDoc + * {@inheritdoc} */ public function push(PushMessage $message, iterable $subscriptions): iterable { diff --git a/src/Sender/PushMessageSender.php b/src/Sender/PushMessageSender.php index 7c0e66d..c98478b 100644 --- a/src/Sender/PushMessageSender.php +++ b/src/Sender/PushMessageSender.php @@ -3,8 +3,8 @@ namespace BenTools\WebPushBundle\Sender; use BenTools\WebPushBundle\Model\Message\PushMessage; -use BenTools\WebPushBundle\Model\Subscription\UserSubscriptionInterface; use BenTools\WebPushBundle\Model\Response\PushResponse; +use BenTools\WebPushBundle\Model\Subscription\UserSubscriptionInterface; use GuzzleHttp\Client; use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\RequestException; @@ -42,16 +42,12 @@ class PushMessageSender implements PushMessagerSenderInterface /** * PushMessageSender constructor. - * @param array $auth - * @param array $defaultOptions - * @param ClientInterface|null $client */ public function __construct( array $auth = [], array $defaultOptions = [], ClientInterface $client = null ) { - if (isset($auth['VAPID'])) { $auth['VAPID']['validated'] = false; } @@ -62,9 +58,8 @@ public function __construct( } /** - * @param PushMessage $message - * @param iterable $subscriptions * @return PushResponse[] + * * @throws \ErrorException * @throws \InvalidArgumentException * @throws \LogicException @@ -100,7 +95,6 @@ public function push(PushMessage $message, iterable $subscriptions): iterable return new PushResponse($subscription, $response->getStatusCode()); }) ->otherwise(function (\Throwable $reason) use ($subscription) { - if ($reason instanceof RequestException && $reason->hasResponse()) { return new PushResponse($subscription, $reason->getResponse()->getStatusCode()); } @@ -121,12 +115,9 @@ public function push(PushMessage $message, iterable $subscriptions): iterable return $promise->wait(); } - /** - * @return bool - */ public function isAutomaticPadding(): bool { - return $this->maxPaddingLength !== 0; + return 0 !== $this->maxPaddingLength; } /** @@ -140,8 +131,6 @@ public function getMaxPaddingLength() /** * @param int|bool $maxPaddingLength Max padding length * - * @return self - * * @throws \Exception */ public function setMaxPaddingLength($maxPaddingLength): self @@ -150,9 +139,9 @@ public function setMaxPaddingLength($maxPaddingLength): self 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).'); } elseif ($maxPaddingLength < 0) { throw new \Exception('Padding length should be positive or zero.'); - } elseif ($maxPaddingLength === true) { + } elseif (true === $maxPaddingLength) { $this->maxPaddingLength = Encryption::MAX_COMPATIBILITY_PAYLOAD_LENGTH; - } elseif ($maxPaddingLength === false) { + } elseif (false === $maxPaddingLength) { $this->maxPaddingLength = 0; } else { $this->maxPaddingLength = $maxPaddingLength; @@ -161,9 +150,6 @@ public function setMaxPaddingLength($maxPaddingLength): self return $this; } - /** - * @return array - */ public function getDefaultOptions(): array { return $this->defaultOptions; diff --git a/src/Sender/PushMessagerSenderInterface.php b/src/Sender/PushMessagerSenderInterface.php index e3d268c..de3adc6 100644 --- a/src/Sender/PushMessagerSenderInterface.php +++ b/src/Sender/PushMessagerSenderInterface.php @@ -3,8 +3,8 @@ namespace BenTools\WebPushBundle\Sender; use BenTools\WebPushBundle\Model\Message\PushMessage; -use BenTools\WebPushBundle\Model\Subscription\UserSubscriptionInterface; use BenTools\WebPushBundle\Model\Response\PushResponse; +use BenTools\WebPushBundle\Model\Subscription\UserSubscriptionInterface; interface PushMessagerSenderInterface { @@ -12,8 +12,8 @@ interface PushMessagerSenderInterface * Push a notification. * The implementation MUST adapt the payload with proper padding, etc. * - * @param PushMessage $message * @param UserSubscriptionInterface[] $subscriptions + * * @return PushResponse[] */ public function push(PushMessage $message, iterable $subscriptions): iterable; diff --git a/src/Sender/RequestBuilder.php b/src/Sender/RequestBuilder.php index cedfb73..3ff45ae 100644 --- a/src/Sender/RequestBuilder.php +++ b/src/Sender/RequestBuilder.php @@ -6,23 +6,20 @@ use BenTools\WebPushBundle\Model\Message\PushMessage; use BenTools\WebPushBundle\Model\Subscription\UserSubscriptionInterface; use GuzzleHttp\Psr7\Request; +use function GuzzleHttp\Psr7\stream_for; use GuzzleHttp\Psr7\Uri; use Minishlink\WebPush\Encryption; use Minishlink\WebPush\Utils; use Minishlink\WebPush\VAPID; use Psr\Http\Message\RequestInterface; -use function GuzzleHttp\Psr7\stream_for; final class RequestBuilder { private const FCM_BASE_URL = 'https://fcm.googleapis.com'; /** - * @param PushMessage $message - * @param UserSubscriptionInterface $subscription - * @param int $ttl - * @param int $maxPaddingLength - * @return RequestInterface + * @param int $maxPaddingLength + * * @throws \ErrorException * @throws \InvalidArgumentException */ @@ -36,13 +33,11 @@ public function createRequest( $request = $this->withOptionalHeaders($request, $message); $request = $request->withHeader('TTL', $ttl); - if (null !== $message->getPayload() && null !== $subscription->getPublicKey() && null !== $subscription->getAuthToken()) { $request = $request ->withHeader('Content-Type', 'application/octet-stream') ->withHeader('Content-Encoding', $subscription->getContentEncoding()); - $payload = $this->getNormalizedPayload($message->getPayload(), $subscription->getContentEncoding(), $maxPaddingLength); $encrypted = Encryption::encrypt( @@ -53,39 +48,33 @@ public function createRequest( ); if ('aesgcm' === $subscription->getContentEncoding()) { - $request = $request->withHeader('Encryption', 'salt=' . Base64Url::encode($encrypted['salt'])) - ->withHeader('Crypto-Key', 'dh=' . Base64Url::encode($encrypted['localPublicKey'])); + $request = $request->withHeader('Encryption', 'salt='.Base64Url::encode($encrypted['salt'])) + ->withHeader('Crypto-Key', 'dh='.Base64Url::encode($encrypted['localPublicKey'])); } $encryptionContentCodingHeader = Encryption::getContentCodingHeader($encrypted['salt'], $encrypted['localPublicKey'], $subscription->getContentEncoding()); - $content = $encryptionContentCodingHeader . $encrypted['cipherText']; + $content = $encryptionContentCodingHeader.$encrypted['cipherText']; return $request ->withBody(stream_for($content)) ->withHeader('Content-Length', Utils::safeStrlen($content)); } - return $request ->withHeader('Content-Length', 0); } /** - * @param RequestInterface $request - * @param array $vapid - * @param UserSubscriptionInterface $subscription - * @return RequestInterface * @throws \ErrorException * @throws \InvalidArgumentException */ public function withVAPIDAuthentication(RequestInterface $request, array $vapid, UserSubscriptionInterface $subscription): RequestInterface { - $endpoint = $subscription->getEndpoint(); - $audience = parse_url($endpoint, PHP_URL_SCHEME) . '://' . parse_url($endpoint, PHP_URL_HOST); + $audience = parse_url($endpoint, PHP_URL_SCHEME).'://'.parse_url($endpoint, PHP_URL_HOST); if (!parse_url($audience)) { - throw new \ErrorException('Audience "' . $audience . '"" could not be generated.'); + throw new \ErrorException('Audience "'.$audience.'"" could not be generated.'); } $vapidHeaders = VAPID::getVapidHeaders($audience, $vapid['subject'], $vapid['publicKey'], $vapid['privateKey'], $subscription->getContentEncoding()); @@ -94,12 +83,12 @@ public function withVAPIDAuthentication(RequestInterface $request, array $vapid, if ('aesgcm' === $subscription->getContentEncoding()) { if ($request->hasHeader('Crypto-Key')) { - $request = $request->withHeader('Crypto-Key', $request->getHeaderLine('Crypto-Key') . ';' . $vapidHeaders['Crypto-Key']); + $request = $request->withHeader('Crypto-Key', $request->getHeaderLine('Crypto-Key').';'.$vapidHeaders['Crypto-Key']); } else { $headers['Crypto-Key'] = $vapidHeaders['Crypto-Key']; $request->withHeader('Crypto-Key', $vapidHeaders['Crypto-Key']); } - } else if ('aes128gcm' === $subscription->getContentEncoding() && substr($endpoint, 0, strlen(self::FCM_BASE_URL)) === self::FCM_BASE_URL) { + } elseif ('aes128gcm' === $subscription->getContentEncoding() && self::FCM_BASE_URL === substr($endpoint, 0, strlen(self::FCM_BASE_URL))) { $request = $request->withUri(new Uri(str_replace('fcm/send', 'wp', $endpoint))); } @@ -107,20 +96,14 @@ public function withVAPIDAuthentication(RequestInterface $request, array $vapid, } /** - * @param RequestInterface $request - * @param string $apiKey - * @return RequestInterface * @throws \InvalidArgumentException */ public function withGCMAuthentication(RequestInterface $request, string $apiKey): RequestInterface { - return $request->withHeader('Authorization', 'key=' . $apiKey); + return $request->withHeader('Authorization', 'key='.$apiKey); } /** - * @param RequestInterface $request - * @param PushMessage $message - * @return RequestInterface * @throws \InvalidArgumentException */ private function withOptionalHeaders(RequestInterface $request, PushMessage $message): RequestInterface @@ -135,10 +118,8 @@ private function withOptionalHeaders(RequestInterface $request, PushMessage $mes } /** - * @param null|string $payload - * @param string $contentEncoding - * @param mixed $automaticPadding - * @return null|string + * @param mixed $automaticPadding + * * @throws \ErrorException */ private function getNormalizedPayload(?string $payload, string $contentEncoding, $automaticPadding): ?string @@ -147,7 +128,7 @@ private function getNormalizedPayload(?string $payload, string $contentEncoding, return null; } if (Utils::safeStrlen($payload) > Encryption::MAX_PAYLOAD_LENGTH) { - throw new \ErrorException('Size of payload must not be greater than ' . Encryption::MAX_PAYLOAD_LENGTH . ' bytes.'); + throw new \ErrorException('Size of payload must not be greater than '.Encryption::MAX_PAYLOAD_LENGTH.' bytes.'); } return Encryption::padPayload($payload, $automaticPadding, $contentEncoding); diff --git a/src/Twig/WebPushTwigExtension.php b/src/Twig/WebPushTwigExtension.php index 019d294..65957ab 100644 --- a/src/Twig/WebPushTwigExtension.php +++ b/src/Twig/WebPushTwigExtension.php @@ -18,9 +18,9 @@ public function __construct(?string $publicKey) } /** - * @inheritDoc + * {@inheritdoc} */ - public function getGlobals() + public function getGlobals(): array { return [ 'bentools_webpush' => [ diff --git a/src/WebPushBundle.php b/src/WebPushBundle.php index efc59df..70147e6 100644 --- a/src/WebPushBundle.php +++ b/src/WebPushBundle.php @@ -10,7 +10,7 @@ class WebPushBundle extends Bundle { /** - * @inheritDoc + * {@inheritdoc} */ public function getContainerExtension() {