Skip to content

Commit eed7f16

Browse files
committed
Fix Symfony4/5 compatibility + Fix CS
1 parent dc0b4cc commit eed7f16

20 files changed

+122
-242
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ php:
88
- '7.1'
99
- '7.2'
1010
- '7.3'
11+
- '7.4'
1112
- nightly
1213

1314
env:

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ We assume you have a minimum knowledge of how Push Notifications work, otherwise
2323
* You have an eCommerce app:
2424
* Notify your customer their order has been shipped
2525
* Notify your category manager they sell a product
26-
26+
2727

2828
## Summary
2929

@@ -43,7 +43,7 @@ This bundle is just the back-end part of the subscription process. For the front
4343
PHP7.1+ is required.
4444

4545
```bash
46-
composer require bentools/webpush-bundle 0.5.*
46+
composer require bentools/webpush-bundle 0.6.*
4747
```
4848

4949
If you're using Symfony 3, add the bundle to your kernel. With Symfony Flex, this should be done automatically.

composer.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,22 @@
1616
"ext-mbstring": "*",
1717
"ext-openssl": "*",
1818
"guzzlehttp/guzzle": "~6.0",
19-
"minishlink/web-push": "^4.0|^5.0",
20-
"symfony/http-kernel": "^3.0|^4.0"
19+
"minishlink/web-push": "~4.0|~5.0",
20+
"symfony/http-kernel": "~3.0|~4.0|~5.0"
2121
},
2222
"require-dev": {
2323
"bentools/doctrine-static": "1.0.x-dev",
2424
"doctrine/dbal": "~2.5",
25-
"nyholm/symfony-bundle-test": "^1.4",
26-
"phpunit/phpunit": "^5.0|^6.0|^7.0",
27-
"symfony/config": "^4.0",
28-
"symfony/dependency-injection": "^3.0|^4.0",
29-
"symfony/framework-bundle": "^3.0|^4.0",
30-
"symfony/http-foundation": "^3.0|^4.0",
31-
"symfony/routing": "^3.0|^4.0",
32-
"symfony/security": "^3.0|^4.0",
33-
"symfony/var-dumper": "^3.0|^4.0",
34-
"symfony/yaml": "^3.0|^4.0",
25+
"nyholm/symfony-bundle-test": "~1.4",
26+
"phpunit/phpunit": "~5.0|~6.0|~7.0",
27+
"symfony/config": "~4.0",
28+
"symfony/dependency-injection": "~3.0|~4.0|~5.0",
29+
"symfony/framework-bundle": "~3.0|~4.0|~5.0",
30+
"symfony/http-foundation": "~3.0|~4.0|~5.0",
31+
"symfony/routing": "~3.0|~4.0|~5.0",
32+
"symfony/security": "~3.0|~4.0|~5.0",
33+
"symfony/var-dumper": "~3.0|~4.0|~5.0",
34+
"symfony/yaml": "~3.0|~4.0|~5.0",
3535
"twig/twig": "~1.0|~2.0"
3636
},
3737
"autoload": {

src/Action/RegisterSubscriptionAction.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use BenTools\WebPushBundle\Model\Subscription\UserSubscriptionManagerRegistry;
66
use Symfony\Component\HttpFoundation\Request;
77
use Symfony\Component\HttpFoundation\Response;
8+
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
89
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
910
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
1011
use Symfony\Component\Security\Core\User\UserInterface;
@@ -18,18 +19,13 @@ final class RegisterSubscriptionAction
1819

1920
/**
2021
* RegisterSubscriptionAction constructor.
21-
* @param UserSubscriptionManagerRegistry $registry
2222
*/
2323
public function __construct(UserSubscriptionManagerRegistry $registry)
2424
{
2525
$this->registry = $registry;
2626
}
2727

2828
/**
29-
* @param UserInterface $user
30-
* @param string $subscriptionHash
31-
* @param array $subscription
32-
* @param array $options
3329
* @throws \InvalidArgumentException
3430
* @throws \RuntimeException
3531
* @throws \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
@@ -44,8 +40,6 @@ private function subscribe(UserInterface $user, string $subscriptionHash, array
4440
}
4541

4642
/**
47-
* @param UserInterface $user
48-
* @param string $subscriptionHash
4943
* @throws BadRequestHttpException
5044
* @throws \RuntimeException
5145
*/
@@ -54,18 +48,16 @@ private function unsubscribe(UserInterface $user, string $subscriptionHash)
5448
$manager = $this->registry->getManager($user);
5549
$subscription = $manager->getUserSubscription($user, $subscriptionHash);
5650
if (null === $subscription) {
57-
throw new BadRequestHttpException("Subscription hash not found");
51+
throw new BadRequestHttpException('Subscription hash not found');
5852
}
5953
$manager->delete($subscription);
6054
}
6155

62-
/**
63-
* @param Request $request
64-
* @param UserInterface $user
65-
* @return Response
66-
*/
67-
public function __invoke(Request $request, UserInterface $user): Response
56+
public function __invoke(Request $request, UserInterface $user = null): Response
6857
{
58+
if (null === $user) {
59+
throw new AccessDeniedHttpException('Not authenticated.');
60+
}
6961

7062
if (!in_array($request->getMethod(), ['POST', 'DELETE'])) {
7163
throw new MethodNotAllowedHttpException(['POST', 'DELETE']);

src/Command/WebPushGenerateKeysCommand.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
namespace BenTools\WebPushBundle\Command;
44

55
use Minishlink\WebPush\VAPID;
6-
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6+
use Symfony\Component\Console\Command\Command;
77
use Symfony\Component\Console\Input\InputInterface;
88
use Symfony\Component\Console\Output\OutputInterface;
99
use Symfony\Component\Console\Style\SymfonyStyle;
1010
use Symfony\Component\HttpKernel\Kernel;
1111

12-
final class WebPushGenerateKeysCommand extends ContainerAwareCommand
12+
final class WebPushGenerateKeysCommand extends Command
1313
{
14+
protected static $defaultName = 'webpush:generate:keys';
15+
1416
/**
1517
* {@inheritdoc}
1618
*/
@@ -24,7 +26,7 @@ protected function configure()
2426
/**
2527
* {@inheritdoc}
2628
*/
27-
protected function execute(InputInterface $input, OutputInterface $output)
29+
protected function execute(InputInterface $input, OutputInterface $output): int
2830
{
2931
$io = new SymfonyStyle($input, $output);
3032
$keys = VAPID::createVapidKeys();
@@ -50,5 +52,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
5052
private_key: '{$keys['privateKey']}'
5153
EOF
5254
);
55+
56+
return 0;
5357
}
5458
}

src/DependencyInjection/Configuration.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@
44

55
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
66
use Symfony\Component\Config\Definition\ConfigurationInterface;
7+
use Symfony\Component\HttpKernel\Kernel;
78

89
class Configuration implements ConfigurationInterface
910
{
1011
public function getConfigTreeBuilder()
1112
{
12-
$treeBuilder = new TreeBuilder();
13-
$rootNode = $treeBuilder->root('bentools_webpush');
13+
if (Kernel::MAJOR_VERSION < 4) {
14+
$treeBuilder = new TreeBuilder();
15+
$rootNode = $treeBuilder->root('bentools_webpush');
16+
} else {
17+
$treeBuilder = new TreeBuilder('bentools_webpush');
18+
$rootNode = $treeBuilder->getRootNode();
19+
}
1420

1521
$rootNode
1622
->children()

src/DependencyInjection/WebPushCompilerPass.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
final class WebPushCompilerPass implements CompilerPassInterface
1111
{
12-
1312
public function process(ContainerBuilder $container)
1413
{
1514
$registry = $container->getDefinition(UserSubscriptionManagerRegistry::class);

src/DependencyInjection/WebPushExtension.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* This is the class that loads and manages your bundle configuration.
1212
*
13-
* @link http://symfony.com/doc/current/cookbook/bundles/extension.html
13+
* @see http://symfony.com/doc/current/cookbook/bundles/extension.html
1414
*/
1515
class WebPushExtension extends Extension
1616
{
@@ -25,12 +25,12 @@ public function load(array $configs, ContainerBuilder $container)
2525
$container->setParameter('bentools_webpush.vapid_subject', $config['settings']['subject'] ?? $container->getParameter('router.request_context.host'));
2626
$container->setParameter('bentools_webpush.vapid_public_key', $config['settings']['public_key'] ?? null);
2727
$container->setParameter('bentools_webpush.vapid_private_key', $config['settings']['private_key'] ?? null);
28-
$loader = new XmlFileLoader($container, new FileLocator([__DIR__ . '/../Resources/config/']));
28+
$loader = new XmlFileLoader($container, new FileLocator([__DIR__.'/../Resources/config/']));
2929
$loader->load('services.xml');
3030
}
3131

3232
/**
33-
* @inheritDoc
33+
* {@inheritdoc}
3434
*/
3535
public function getAlias()
3636
{

src/Model/Message/PushMessage.php

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,16 @@
66
* A message is an enveloppe that contain:
77
* - An optional payload
88
* - An optional array of options (like TTL, Topic, etc)
9-
* - An optional array of authentication data (if different from the client)
9+
* - An optional array of authentication data (if different from the client).
1010
*/
1111
final class PushMessage
1212
{
13-
14-
private $payload, $options, $auth;
13+
private $payload;
14+
private $options;
15+
private $auth;
1516

1617
/**
1718
* PushMessage constructor.
18-
* @param null|string $payload
19-
* @param array $options
20-
* @param array $auth
2119
*/
2220
public function __construct(?string $payload = null, array $options = [], array $auth = [])
2321
{
@@ -26,46 +24,34 @@ public function __construct(?string $payload = null, array $options = [], array
2624
$this->auth = $auth;
2725
}
2826

29-
/**
30-
* @param null|string $payload
31-
*/
3227
public function setPayload(?string $payload): void
3328
{
3429
$this->payload = $payload;
3530
}
3631

37-
/**
38-
* @return null|string
39-
*/
4032
public function getPayload(): ?string
4133
{
4234
return $this->payload;
4335
}
4436

45-
/**
46-
* @param int $ttl
47-
*/
4837
public function setTTL(int $ttl): void
4938
{
5039
$this->options['TTL'] = $ttl;
5140
}
5241

53-
/**
54-
* @param null|string $topic
55-
*/
5642
public function setTopic(?string $topic): void
5743
{
5844
$this->options['topic'] = $topic;
5945
}
6046

6147
/**
62-
* @param null|string $urgency
6348
* @throws \InvalidArgumentException
6449
*/
6550
public function setUrgency(?string $urgency): void
6651
{
6752
if (null === $urgency) {
6853
unset($this->options['urgency']);
54+
6955
return;
7056
}
7157

@@ -76,9 +62,6 @@ public function setUrgency(?string $urgency): void
7662
$this->options['urgency'] = $urgency;
7763
}
7864

79-
/**
80-
* @return array
81-
*/
8265
public function getOptions(): array
8366
{
8467
return array_diff($this->options, array_filter($this->options, 'is_null'));
@@ -89,9 +72,6 @@ public function getOption(string $key)
8972
return $this->options[$key] ?? null;
9073
}
9174

92-
/**
93-
* @return array
94-
*/
9575
public function getAuth(): array
9676
{
9777
return $this->auth;

0 commit comments

Comments
 (0)