Skip to content

Commit

Permalink
Allow Symfony 7 (#148)
Browse files Browse the repository at this point in the history
* Allow Symfony 7

* An empty commit

* Try to fix tests with UriSignerFactory

* Fix CS

* Drop SensitiveParameter

* Move UriSignerFactory into Factory namespace

* Drop Psalm dependency - we set it up in the CI

* Some minor tweaks + fix psalm issues

* Simplify Psalm config

* Create uri_signer service from the factory

* Add uri_signer service to the tests too

* Inject a specific service instead of factory

* Fix CS
  • Loading branch information
bocharsky-bw authored Dec 2, 2023
1 parent 9031eca commit 10a11a6
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 24 deletions.
13 changes: 6 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@
"require": {
"php": ">=7.2.5",
"ext-json": "*",
"symfony/config": "^5.4 | ^6.0",
"symfony/dependency-injection": "^5.4 | ^6.0",
"symfony/http-kernel": "^5.4 | ^6.0",
"symfony/routing": "^5.4 | ^6.0",
"symfony/config": "^5.4 | ^6.0 | ^7.0",
"symfony/dependency-injection": "^5.4 | ^6.0 | ^7.0",
"symfony/http-kernel": "^5.4 | ^6.0 | ^7.0",
"symfony/routing": "^5.4 | ^6.0 | ^7.0",
"symfony/deprecation-contracts": "^2.2 | ^3.0"
},
"require-dev": {
"doctrine/orm": "^2.7",
"doctrine/persistence": "^2.0",
"symfony/framework-bundle": "^5.4 | ^6.0",
"symfony/phpunit-bridge": "^5.4 | ^6.0",
"vimeo/psalm": "^4.3"
"symfony/framework-bundle": "^5.4 | ^6.0 | ^7.0",
"symfony/phpunit-bridge": "^5.4 | ^6.0 | ^7.0"
},
"autoload": {
"psr-4": {
Expand Down
21 changes: 12 additions & 9 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,23 @@
<RawObjectIteration errorLevel="info" />

<InvalidStringClass errorLevel="info" />
<PossiblyUndefinedMethod>

<UndefinedClass>
<errorLevel type="suppress">
<file name="src/DependencyInjection/Configuration.php"/>
<referencedClass name="Symfony\Component\HttpKernel\UriSigner"/>
</errorLevel>
</PossiblyUndefinedMethod>
<PossiblyNullArgument>
</UndefinedClass>
<UndefinedDocblockClass>
<errorLevel type="suppress">
<file name="src/DependencyInjection/SymfonyCastsVerifyEmailExtension.php"/>
<file name="src/Factory/UriSignerFactory.php"/>
<referencedClass name="Symfony\Component\HttpKernel\UriSigner" />
</errorLevel>
</PossiblyNullArgument>
<TooManyArguments>
</UndefinedDocblockClass>
<UndefinedMethod>
<errorLevel type="suppress">
<file name="src/Model/VerifyEmailSignatureComponents.php"/>
<file name="src/DependencyInjection/Configuration.php" />
<referencedMethod name="Symfony\Component\Config\Definition\Builder\NodeDefinition::children" />
</errorLevel>
</TooManyArguments>
</UndefinedMethod>
</issueHandlers>
</psalm>
3 changes: 3 additions & 0 deletions src/DependencyInjection/SymfonyCastsVerifyEmailExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public function load(array $configs, ContainerBuilder $container): void
$loader->load('verify_email_services.xml');

$configuration = $this->getConfiguration($configs, $container);
if (!$configuration) {
throw new \Exception('Configuration is not expected to be null');
}

$config = $this->processConfiguration($configuration, $configs);

Expand Down
41 changes: 41 additions & 0 deletions src/Factory/UriSignerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the SymfonyCasts VerifyEmailBundle package.
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace SymfonyCasts\Bundle\VerifyEmail\Factory;

use Symfony\Component\HttpFoundation\UriSigner;
use Symfony\Component\HttpKernel\UriSigner as LegacyUriSigner;

/**
* @author Victor Bocharsky <[email protected]>
* @author Ryan Weaver <[email protected]>
*/
class UriSignerFactory
{
private $secret;
private $parameter;

public function __construct(string $secret, string $parameter = '_hash')
{
$this->secret = $secret;
$this->parameter = $parameter;
}

/**
* @return UriSigner|LegacyUriSigner
*/
public function createUriSigner(): object
{
if (class_exists(UriSigner::class)) {
return new UriSigner($this->secret, $this->parameter);
}

return new LegacyUriSigner($this->secret, $this->parameter);
}
}
3 changes: 3 additions & 0 deletions src/Model/VerifyEmailSignatureComponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ public function getExpiresAtIntervalInstance(): \DateInterval
return $this->expiresAt->diff($createdAtTime);
}

/**
* @psalm-suppress UndefinedFunction
*/
private function triggerDeprecation(): void
{
trigger_deprecation(
Expand Down
6 changes: 5 additions & 1 deletion src/Resources/config/verify_email_services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@

<service id="symfonycasts.verify_email.query_utility" class="SymfonyCasts\Bundle\VerifyEmail\Util\VerifyEmailQueryUtility" public="false" />

<service id="symfonycasts.verify_email.uri_signer" class="Symfony\Component\HttpKernel\UriSigner">
<service id="symfonycasts.verify_email.uri_signer_factory" class="SymfonyCasts\Bundle\VerifyEmail\Factory\UriSignerFactory">
<argument>%kernel.secret%</argument>
<argument>signature</argument>
</service>

<service id="symfonycasts.verify_email.uri_signer" class="Symfony\Component\HttpFoundation\UriSigner">
<factory service="symfonycasts.verify_email.uri_signer_factory" method="createUriSigner" />
</service>

<service id="SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface" alias="symfonycasts.verify_email.helper" />

<service id="symfonycasts.verify_email.helper" class="SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelper">
Expand Down
2 changes: 1 addition & 1 deletion src/SymfonyCastsVerifyEmailBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SymfonyCastsVerifyEmailBundle extends Bundle
{
public function getContainerExtension(): ?ExtensionInterface
{
if (null === $this->extension) {
if (!$this->extension) {
$this->extension = new SymfonyCastsVerifyEmailExtension();
}

Expand Down
8 changes: 6 additions & 2 deletions src/VerifyEmailHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

namespace SymfonyCasts\Bundle\VerifyEmail;

use Symfony\Component\HttpKernel\UriSigner;
use Symfony\Component\HttpFoundation\UriSigner;
use Symfony\Component\HttpKernel\UriSigner as LegacyUriSigner;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use SymfonyCasts\Bundle\VerifyEmail\Exception\ExpiredSignatureException;
use SymfonyCasts\Bundle\VerifyEmail\Exception\InvalidSignatureException;
Expand All @@ -25,6 +26,9 @@
final class VerifyEmailHelper implements VerifyEmailHelperInterface
{
private $router;
/**
* @var UriSigner|LegacyUriSigner
*/
private $uriSigner;
private $queryUtility;
private $tokenGenerator;
Expand All @@ -34,7 +38,7 @@ final class VerifyEmailHelper implements VerifyEmailHelperInterface
*/
private $lifetime;

public function __construct(UrlGeneratorInterface $router, UriSigner $uriSigner, VerifyEmailQueryUtility $queryUtility, VerifyEmailTokenGenerator $generator, int $lifetime)
public function __construct(UrlGeneratorInterface $router, /* no typehint for BC with legacy PHP */ $uriSigner, VerifyEmailQueryUtility $queryUtility, VerifyEmailTokenGenerator $generator, int $lifetime)
{
$this->router = $router;
$this->uriSigner = $uriSigner;
Expand Down
11 changes: 9 additions & 2 deletions tests/FunctionalTests/VerifyEmailHelperFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ClockMock;
use Symfony\Component\HttpKernel\UriSigner;
use Symfony\Component\HttpFoundation\UriSigner;
use Symfony\Component\HttpKernel\UriSigner as LegacyUriSigner;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use SymfonyCasts\Bundle\VerifyEmail\Generator\VerifyEmailTokenGenerator;
use SymfonyCasts\Bundle\VerifyEmail\Util\VerifyEmailQueryUtility;
Expand Down Expand Up @@ -105,9 +106,15 @@ private function getTestSignedUri(): string

private function getHelper(): VerifyEmailHelperInterface
{
if (class_exists(UriSigner::class)) {
$uriSigner = new UriSigner('foo', 'signature');
} else {
$uriSigner = new LegacyUriSigner('foo', 'signature');
}

return new VerifyEmailHelper(
$this->mockRouter,
new UriSigner('foo', 'signature'),
$uriSigner,
new VerifyEmailQueryUtility(),
new VerifyEmailTokenGenerator('foo'),
3600
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function bundleServiceDefinitionDataProvider(): \Generator
$prefix = 'symfonycasts.verify_email.';

yield [$prefix.'query_utility'];
yield [$prefix.'uri_signer_factory'];
yield [$prefix.'uri_signer'];
yield [$prefix.'helper'];
yield [$prefix.'token_generator'];
Expand Down
9 changes: 7 additions & 2 deletions tests/UnitTests/VerifyEmailHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ClockMock;
use Symfony\Component\HttpKernel\UriSigner;
use Symfony\Component\HttpFoundation\UriSigner;
use Symfony\Component\HttpKernel\UriSigner as LegacyUriSigner;
use Symfony\Component\Routing\RouterInterface;
use SymfonyCasts\Bundle\VerifyEmail\Exception\ExpiredSignatureException;
use SymfonyCasts\Bundle\VerifyEmail\Exception\InvalidSignatureException;
Expand Down Expand Up @@ -39,7 +40,11 @@ protected function setUp(): void
ClockMock::register(VerifyEmailHelper::class);

$this->mockRouter = $this->createMock(RouterInterface::class);
$this->mockSigner = $this->createMock(UriSigner::class);
if (class_exists(UriSigner::class)) {
$this->mockSigner = $this->createMock(UriSigner::class);
} else {
$this->mockSigner = $this->createMock(LegacyUriSigner::class);
}
$this->mockQueryUtility = $this->createMock(VerifyEmailQueryUtility::class);
$this->tokenGenerator = $this->createMock(VerifyEmailTokenGenerator::class);
}
Expand Down

0 comments on commit 10a11a6

Please sign in to comment.