Skip to content

Commit

Permalink
Fix Symfony4/5 compatibility + Fix CS
Browse files Browse the repository at this point in the history
  • Loading branch information
bpolaszek committed May 7, 2020
1 parent dc0b4cc commit eed7f16
Show file tree
Hide file tree
Showing 20 changed files with 122 additions and 242 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ php:
- '7.1'
- '7.2'
- '7.3'
- '7.4'
- nightly

env:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ We assume you have a minimum knowledge of how Push Notifications work, otherwise
* You have an eCommerce app:
* Notify your customer their order has been shipped
* Notify your category manager they sell a product


## Summary

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

```bash
composer require bentools/webpush-bundle 0.5.*
composer require bentools/webpush-bundle 0.6.*
```

If you're using Symfony 3, add the bundle to your kernel. With Symfony Flex, this should be done automatically.
Expand Down
24 changes: 12 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@
"ext-mbstring": "*",
"ext-openssl": "*",
"guzzlehttp/guzzle": "~6.0",
"minishlink/web-push": "^4.0|^5.0",
"symfony/http-kernel": "^3.0|^4.0"
"minishlink/web-push": "~4.0|~5.0",
"symfony/http-kernel": "~3.0|~4.0|~5.0"
},
"require-dev": {
"bentools/doctrine-static": "1.0.x-dev",
"doctrine/dbal": "~2.5",
"nyholm/symfony-bundle-test": "^1.4",
"phpunit/phpunit": "^5.0|^6.0|^7.0",
"symfony/config": "^4.0",
"symfony/dependency-injection": "^3.0|^4.0",
"symfony/framework-bundle": "^3.0|^4.0",
"symfony/http-foundation": "^3.0|^4.0",
"symfony/routing": "^3.0|^4.0",
"symfony/security": "^3.0|^4.0",
"symfony/var-dumper": "^3.0|^4.0",
"symfony/yaml": "^3.0|^4.0",
"nyholm/symfony-bundle-test": "~1.4",
"phpunit/phpunit": "~5.0|~6.0|~7.0",
"symfony/config": "~4.0",
"symfony/dependency-injection": "~3.0|~4.0|~5.0",
"symfony/framework-bundle": "~3.0|~4.0|~5.0",
"symfony/http-foundation": "~3.0|~4.0|~5.0",
"symfony/routing": "~3.0|~4.0|~5.0",
"symfony/security": "~3.0|~4.0|~5.0",
"symfony/var-dumper": "~3.0|~4.0|~5.0",
"symfony/yaml": "~3.0|~4.0|~5.0",
"twig/twig": "~1.0|~2.0"
},
"autoload": {
Expand Down
20 changes: 6 additions & 14 deletions src/Action/RegisterSubscriptionAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use BenTools\WebPushBundle\Model\Subscription\UserSubscriptionManagerRegistry;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\Security\Core\User\UserInterface;
Expand All @@ -18,18 +19,13 @@ final class RegisterSubscriptionAction

/**
* RegisterSubscriptionAction constructor.
* @param UserSubscriptionManagerRegistry $registry
*/
public function __construct(UserSubscriptionManagerRegistry $registry)
{
$this->registry = $registry;
}

/**
* @param UserInterface $user
* @param string $subscriptionHash
* @param array $subscription
* @param array $options
* @throws \InvalidArgumentException
* @throws \RuntimeException
* @throws \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
Expand All @@ -44,8 +40,6 @@ private function subscribe(UserInterface $user, string $subscriptionHash, array
}

/**
* @param UserInterface $user
* @param string $subscriptionHash
* @throws BadRequestHttpException
* @throws \RuntimeException
*/
Expand All @@ -54,18 +48,16 @@ private function unsubscribe(UserInterface $user, string $subscriptionHash)
$manager = $this->registry->getManager($user);
$subscription = $manager->getUserSubscription($user, $subscriptionHash);
if (null === $subscription) {
throw new BadRequestHttpException("Subscription hash not found");
throw new BadRequestHttpException('Subscription hash not found');
}
$manager->delete($subscription);
}

/**
* @param Request $request
* @param UserInterface $user
* @return Response
*/
public function __invoke(Request $request, UserInterface $user): Response
public function __invoke(Request $request, UserInterface $user = null): Response
{
if (null === $user) {
throw new AccessDeniedHttpException('Not authenticated.');
}

if (!in_array($request->getMethod(), ['POST', 'DELETE'])) {
throw new MethodNotAllowedHttpException(['POST', 'DELETE']);
Expand Down
10 changes: 7 additions & 3 deletions src/Command/WebPushGenerateKeysCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
namespace BenTools\WebPushBundle\Command;

use Minishlink\WebPush\VAPID;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\HttpKernel\Kernel;

final class WebPushGenerateKeysCommand extends ContainerAwareCommand
final class WebPushGenerateKeysCommand extends Command
{
protected static $defaultName = 'webpush:generate:keys';

/**
* {@inheritdoc}
*/
Expand All @@ -24,7 +26,7 @@ protected function configure()
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$keys = VAPID::createVapidKeys();
Expand All @@ -50,5 +52,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
private_key: '{$keys['privateKey']}'
EOF
);

return 0;
}
}
10 changes: 8 additions & 2 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\HttpKernel\Kernel;

class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('bentools_webpush');
if (Kernel::MAJOR_VERSION < 4) {
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('bentools_webpush');
} else {
$treeBuilder = new TreeBuilder('bentools_webpush');
$rootNode = $treeBuilder->getRootNode();
}

$rootNode
->children()
Expand Down
1 change: 0 additions & 1 deletion src/DependencyInjection/WebPushCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

final class WebPushCompilerPass implements CompilerPassInterface
{

public function process(ContainerBuilder $container)
{
$registry = $container->getDefinition(UserSubscriptionManagerRegistry::class);
Expand Down
6 changes: 3 additions & 3 deletions src/DependencyInjection/WebPushExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* This is the class that loads and manages your bundle configuration.
*
* @link http://symfony.com/doc/current/cookbook/bundles/extension.html
* @see http://symfony.com/doc/current/cookbook/bundles/extension.html
*/
class WebPushExtension extends Extension
{
Expand All @@ -25,12 +25,12 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('bentools_webpush.vapid_subject', $config['settings']['subject'] ?? $container->getParameter('router.request_context.host'));
$container->setParameter('bentools_webpush.vapid_public_key', $config['settings']['public_key'] ?? null);
$container->setParameter('bentools_webpush.vapid_private_key', $config['settings']['private_key'] ?? null);
$loader = new XmlFileLoader($container, new FileLocator([__DIR__ . '/../Resources/config/']));
$loader = new XmlFileLoader($container, new FileLocator([__DIR__.'/../Resources/config/']));
$loader->load('services.xml');
}

/**
* @inheritDoc
* {@inheritdoc}
*/
public function getAlias()
{
Expand Down
30 changes: 5 additions & 25 deletions src/Model/Message/PushMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@
* A message is an enveloppe that contain:
* - An optional payload
* - An optional array of options (like TTL, Topic, etc)
* - An optional array of authentication data (if different from the client)
* - An optional array of authentication data (if different from the client).
*/
final class PushMessage
{

private $payload, $options, $auth;
private $payload;
private $options;
private $auth;

/**
* PushMessage constructor.
* @param null|string $payload
* @param array $options
* @param array $auth
*/
public function __construct(?string $payload = null, array $options = [], array $auth = [])
{
Expand All @@ -26,46 +24,34 @@ public function __construct(?string $payload = null, array $options = [], array
$this->auth = $auth;
}

/**
* @param null|string $payload
*/
public function setPayload(?string $payload): void
{
$this->payload = $payload;
}

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

/**
* @param int $ttl
*/
public function setTTL(int $ttl): void
{
$this->options['TTL'] = $ttl;
}

/**
* @param null|string $topic
*/
public function setTopic(?string $topic): void
{
$this->options['topic'] = $topic;
}

/**
* @param null|string $urgency
* @throws \InvalidArgumentException
*/
public function setUrgency(?string $urgency): void
{
if (null === $urgency) {
unset($this->options['urgency']);

return;
}

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

/**
* @return array
*/
public function getOptions(): array
{
return array_diff($this->options, array_filter($this->options, 'is_null'));
Expand All @@ -89,9 +72,6 @@ public function getOption(string $key)
return $this->options[$key] ?? null;
}

/**
* @return array
*/
public function getAuth(): array
{
return $this->auth;
Expand Down
Loading

0 comments on commit eed7f16

Please sign in to comment.