Skip to content

Commit

Permalink
chore: remove deprecations (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentchalamon authored Jan 14, 2025
1 parent 240b39c commit 10e9613
Show file tree
Hide file tree
Showing 19 changed files with 43 additions and 105 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"suggest": {
"doctrine/doctrine-bundle": "To connect with Doctrine in Symfony project",
"doctrine/orm": "To connect with Doctrine",
"api-platform/core": "To connect with API Platform"
"api-platform/openapi": "To connect with API Platform"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 0 additions & 2 deletions config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@
<service id="coop_tilleuls_forgot_password.manager.forgot_password" class="CoopTilleuls\ForgotPasswordBundle\Manager\ForgotPasswordManager" public="true">
<argument type="service" id="coop_tilleuls_forgot_password.manager.password_token" />
<argument type="service" id="event_dispatcher" />
<argument type="service" id="coop_tilleuls_forgot_password.provider_chain"/>
</service>

<service id="coop_tilleuls_forgot_password.manager.password_token" class="CoopTilleuls\ForgotPasswordBundle\Manager\PasswordTokenManager" public="true">
<argument type="service" id="coop_tilleuls_forgot_password.provider_chain"/>
<argument/> <!-- token_generator -->
</service>

Expand Down
7 changes: 1 addition & 6 deletions features/app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,11 @@ public function registerBundles(): array
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
new FriendsOfBehat\SymfonyExtension\Bundle\FriendsOfBehatSymfonyExtensionBundle(),
new CoopTilleuls\ForgotPasswordBundle\CoopTilleulsForgotPasswordBundle(),
new ApiPlatform\Symfony\Bundle\ApiPlatformBundle(),
];
if ('jmsserializer' === $this->getEnvironment()) {
$bundles[] = new JMS\SerializerBundle\JMSSerializerBundle();
}
if (class_exists(ApiPlatform\Symfony\Bundle\ApiPlatformBundle::class)) {
$bundles[] = new ApiPlatform\Symfony\Bundle\ApiPlatformBundle();
} else {
// BC api-platform/core:^2.7
$bundles[] = new ApiPlatform\Core\Bridge\Symfony\Bundle\ApiPlatformBundle();
}

return $bundles;
}
Expand Down
24 changes: 12 additions & 12 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ public function resetDatabase(): void
*/
public function iHaveAValidToken(): void
{
$this->passwordTokenManager->createPasswordToken($this->createUser(), new DateTime('+1 day'));
$this->passwordTokenManager->createPasswordToken($this->createUser(), $this->providerChain->get('user'), new DateTime('+1 day'));
}

/**
* @Given I have an expired token
*/
public function iHaveAnExpiredToken(): void
{
$this->passwordTokenManager->createPasswordToken($this->createUser(), new DateTime('-1 minute'));
$this->passwordTokenManager->createPasswordToken($this->createUser(), $this->providerChain->get('user'), new DateTime('-1 minute'));
}

/**
Expand Down Expand Up @@ -166,7 +166,7 @@ public function theResponseShouldBeEmpty(): void
public function theRequestShouldBeInvalidWithMessage($message): void
{
Assert::assertEquals(
400,
422,
$this->client->getResponse()->getStatusCode(),
sprintf('Response is not valid: got %d', $this->client->getResponse()->getStatusCode())
);
Expand Down Expand Up @@ -213,7 +213,7 @@ public function iResetMyPasswordUsingNoParameter(): void
*/
public function iUpdateMyPassword(): void
{
$token = $this->passwordTokenManager->createPasswordToken($this->createUser(), new DateTime('+1 day'));
$token = $this->passwordTokenManager->createPasswordToken($this->createUser(), $this->providerChain->get('user'), new DateTime('+1 day'));

$this->client->request(
'POST',
Expand Down Expand Up @@ -246,7 +246,7 @@ public function thePasswordShouldHaveBeenUpdated(): void
*/
public function iUpdateMyPasswordUsingNoPassword(): void
{
$token = $this->passwordTokenManager->createPasswordToken($this->createUser(), new DateTime('+1 day'));
$token = $this->passwordTokenManager->createPasswordToken($this->createUser(), $this->providerChain->get('user'), new DateTime('+1 day'));

$this->client->request('POST', sprintf('/api/forgot-password/%s', $token->getToken()));
}
Expand Down Expand Up @@ -275,7 +275,7 @@ public function iUpdateMyPasswordUsingAnInvalidToken(): void
*/
public function iUpdateMyPasswordUsingWrongProvider(): void
{
$token = $this->passwordTokenManager->createPasswordToken($this->createAdmin(), new DateTime('+1 day'), $this->providerChain->get('admin'));
$token = $this->passwordTokenManager->createPasswordToken($this->createAdmin(), $this->providerChain->get('admin'), new DateTime('+1 day'));

$this->client->request(
'POST',
Expand All @@ -296,7 +296,7 @@ public function iUpdateMyPasswordUsingWrongProvider(): void
*/
public function iUpdateMyPasswordUsingAValidProviderButAnInvalidPasswordField(): void
{
$token = $this->passwordTokenManager->createPasswordToken($this->createAdmin(), new DateTime('+1 day'), $this->providerChain->get('admin'));
$token = $this->passwordTokenManager->createPasswordToken($this->createAdmin(), $this->providerChain->get('admin'), new DateTime('+1 day'));

$this->client->request(
'POST',
Expand All @@ -317,7 +317,7 @@ public function iUpdateMyPasswordUsingAValidProviderButAnInvalidPasswordField():
*/
public function iUpdateMyPasswordUsingAnExpiredToken(): void
{
$token = $this->passwordTokenManager->createPasswordToken($this->createUser(), new DateTime('-1 minute'));
$token = $this->passwordTokenManager->createPasswordToken($this->createUser(), $this->providerChain->get('user'), new DateTime('-1 minute'));

$this->client->request(
'POST',
Expand All @@ -338,7 +338,7 @@ public function iUpdateMyPasswordUsingAnExpiredToken(): void
*/
public function iGetAPasswordToken(): void
{
$token = $this->passwordTokenManager->createPasswordToken($this->createUser(), new DateTime('+1 day'));
$token = $this->passwordTokenManager->createPasswordToken($this->createUser(), $this->providerChain->get('user'), new DateTime('+1 day'));
$token->setToken('d7xtQlJVyN61TzWtrY6xy37zOxB66BqMSDXEbXBbo2Mw4Jjt9C');
$this->doctrine->getManager()->persist($token);
$this->doctrine->getManager()->flush();
Expand All @@ -363,7 +363,7 @@ public function iShouldGetAPasswordToken(): void
*/
public function iGetAPasswordTokenUsingAnExpiredToken(): void
{
$token = $this->passwordTokenManager->createPasswordToken($this->createUser(), new DateTime('-1 minute'));
$token = $this->passwordTokenManager->createPasswordToken($this->createUser(), $this->providerChain->get('user'), new DateTime('-1 minute'));

$this->client->request('GET', sprintf('/api/forgot-password/%s', $token->getToken()));
}
Expand Down Expand Up @@ -472,7 +472,7 @@ private function getOpenApiPaths(): array
204 => [
'description' => 'Valid email address, no matter if user exists or not',
],
400 => [
422 => [
'description' => 'Missing email parameter or invalid format',
],
],
Expand Down Expand Up @@ -547,7 +547,7 @@ private function getOpenApiPaths(): array
204 => [
'description' => 'Email address format valid, no matter if user exists or not',
],
400 => [
422 => [
'description' => 'Missing password parameter',
],
404 => [
Expand Down
2 changes: 1 addition & 1 deletion phpunit-legacy.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</testsuite>
</testsuites>

<source ignoreSuppressionOfDeprecations="true" ignoreIndirectDeprecations="true">
<source ignoreSuppressionOfDeprecations="true">
<include>
<directory>src</directory>
</include>
Expand Down
4 changes: 2 additions & 2 deletions src/Bridge/ApiPlatform/OpenApi/AbstractOpenApiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function __invoke(array $context = [])
204 => [
'description' => 'Valid email address, no matter if user exists or not',
],
400 => [
422 => [
'description' => 'Missing email parameter or invalid format',
],
])
Expand Down Expand Up @@ -174,7 +174,7 @@ public function __invoke(array $context = [])
204 => [
'description' => 'Email address format valid, no matter if user exists or not',
],
400 => [
422 => [
'description' => 'Missing password parameter',
],
404 => [
Expand Down
4 changes: 2 additions & 2 deletions src/Bridge/ApiPlatform/Serializer/DocumentationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function normalize($object, $format = null, array $context = []): array
204 => [
'description' => 'Valid email address, no matter if user exists or not',
],
400 => [
422 => [
'description' => 'Missing email parameter or invalid format',
],
],
Expand Down Expand Up @@ -146,7 +146,7 @@ public function normalize($object, $format = null, array $context = []): array
204 => [
'description' => 'Email address format valid, no matter if user exists or not',
],
400 => [
422 => [
'description' => 'Missing password parameter',
],
404 => [
Expand Down
19 changes: 1 addition & 18 deletions src/DependencyInjection/BCExtensionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,6 @@ public function load(array $configs, ContainerBuilder $container): void
throw new InvalidConfigurationException('Multiple "ForgotPassword" providers have been defined but none of them is set as default. Did you forget to set "default" option?');
}

// Build parameters
$container->setParameter('coop_tilleuls_forgot_password.password_token_class', $defaultProvider['password_token']['class']);
trigger_deprecation('tilleuls/forgot-password-bundle', '1.5', 'Container parameter "%s" is deprecated since 1.5 and will be removed without replacement in 2.0.', 'coop_tilleuls_forgot_password.password_token_class');
$container->setParameter('coop_tilleuls_forgot_password.password_token_expires_in', $defaultProvider['password_token']['expires_in']);
trigger_deprecation('tilleuls/forgot-password-bundle', '1.5', 'Container parameter "%s" is deprecated since 1.5 and will be removed without replacement in 2.0.', 'coop_tilleuls_forgot_password.password_token_expires_in');
$container->setParameter('coop_tilleuls_forgot_password.password_token_user_field', $defaultProvider['password_token']['user_field']);
trigger_deprecation('tilleuls/forgot-password-bundle', '1.5', 'Container parameter "%s" is deprecated since 1.5 and will be removed without replacement in 2.0.', 'coop_tilleuls_forgot_password.password_token_user_field');
$container->setParameter('coop_tilleuls_forgot_password.password_token_serialization_groups', $defaultProvider['password_token']['serialization_groups']);
trigger_deprecation('tilleuls/forgot-password-bundle', '1.5', 'Container parameter "%s" is deprecated since 1.5 and will be removed without replacement in 2.0.', 'coop_tilleuls_forgot_password.password_token_serialization_groups');
$container->setParameter('coop_tilleuls_forgot_password.user_class', $defaultProvider['user']['class']);
trigger_deprecation('tilleuls/forgot-password-bundle', '1.5', 'Container parameter "%s" is deprecated since 1.5 and will be removed without replacement in 2.0.', 'coop_tilleuls_forgot_password.user_class');
$authorizedFields = array_unique(array_merge($defaultProvider['user']['authorized_fields'], [$defaultProvider['user']['email_field']]));
$container->setParameter('coop_tilleuls_forgot_password.user_authorized_fields', $authorizedFields);
trigger_deprecation('tilleuls/forgot-password-bundle', '1.5', 'Container parameter "%s" is deprecated since 1.5 and will be removed without replacement in 2.0.', 'coop_tilleuls_forgot_password.user_authorized_fields');
$container->setParameter('coop_tilleuls_forgot_password.user_password_field', $defaultProvider['user']['password_field']);
trigger_deprecation('tilleuls/forgot-password-bundle', '1.5', 'Container parameter "%s" is deprecated since 1.5 and will be removed without replacement in 2.0.', 'coop_tilleuls_forgot_password.user_password_field');

$this->buildProvider($config, $container);

$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../../config'));
Expand All @@ -78,7 +61,7 @@ public function load(array $configs, ContainerBuilder $container): void

$container
->getDefinition('coop_tilleuls_forgot_password.manager.password_token')
->replaceArgument(1, new Reference($config['token_generator']));
->replaceArgument(0, new Reference($config['token_generator']));
}

private function buildProvider(array $config, ContainerBuilder $container): void
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/InvalidJsonHttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ final class InvalidJsonHttpException extends HttpException implements JsonHttpEx
{
public function __construct()
{
parent::__construct(400, 'Invalid JSON data.');
parent::__construct(422, 'Invalid JSON data.');
}
}
3 changes: 1 addition & 2 deletions src/Exception/MissingFieldHttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ final class MissingFieldHttpException extends HttpException implements JsonHttpE
{
public function __construct($fieldName)
{
trigger_deprecation('tilleuls/forgot-password-bundle', '1.5', 'Status code will change to "%s" in 2.0.', 422);
parent::__construct(400, \sprintf('Parameter "%s" is missing.', $fieldName));
parent::__construct(422, \sprintf('Parameter "%s" is missing.', $fieldName));
}
}
2 changes: 1 addition & 1 deletion src/Exception/NoParameterException.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ final class NoParameterException extends HttpException implements JsonHttpExcept
{
public function __construct()
{
parent::__construct(400, 'No parameter sent.');
parent::__construct(422, 'No parameter sent.');
}
}
2 changes: 1 addition & 1 deletion src/Exception/UnauthorizedFieldException.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ final class UnauthorizedFieldException extends HttpException implements JsonHttp
{
public function __construct($propertyName)
{
parent::__construct(400, \sprintf('The parameter "%s" is not authorized in your configuration.', $propertyName));
parent::__construct(422, \sprintf('The parameter "%s" is not authorized in your configuration.', $propertyName));
}
}
2 changes: 1 addition & 1 deletion src/Exception/UndefinedProviderException.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ final class UndefinedProviderException extends HttpException implements JsonHttp
{
public function __construct(string $message = 'This provider is not defined.')
{
parent::__construct(400, $message);
parent::__construct(422, $message);
}
}
22 changes: 4 additions & 18 deletions src/Manager/ForgotPasswordManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
use CoopTilleuls\ForgotPasswordBundle\Event\ForgotPasswordEvent;
use CoopTilleuls\ForgotPasswordBundle\Event\UpdatePasswordEvent;
use CoopTilleuls\ForgotPasswordBundle\Event\UserNotFoundEvent;
use CoopTilleuls\ForgotPasswordBundle\Provider\Provider;
use CoopTilleuls\ForgotPasswordBundle\Provider\ProviderChainInterface;
use CoopTilleuls\ForgotPasswordBundle\Provider\ProviderInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface;
Expand All @@ -29,18 +27,12 @@
*/
class ForgotPasswordManager
{
public function __construct(private readonly PasswordTokenManager $passwordTokenManager, private readonly EventDispatcherInterface $dispatcher, private readonly ProviderChainInterface $providerChain)
public function __construct(private readonly PasswordTokenManager $passwordTokenManager, private readonly EventDispatcherInterface $dispatcher)
{
}

public function resetPassword($propertyName, $value, ?ProviderInterface $provider = null): void
public function resetPassword($propertyName, $value, ProviderInterface $provider): void
{
/* @var null|Provider $provider */
if (!$provider) {
trigger_deprecation('tilleuls/forgot-password-bundle', '1.5', 'Parameter "%s" in method "%s" is recommended since 1.5 and will be mandatory in 2.0.', '$provider', __METHOD__);
$provider = $this->providerChain->get();
}

$context = [$propertyName => $value];

$user = $provider->getManager()->findOneBy($provider->getUserClass(), $context);
Expand All @@ -62,7 +54,7 @@ public function resetPassword($propertyName, $value, ?ProviderInterface $provide
$expiredAt = new \DateTime($provider->getPasswordTokenExpiredIn());
$expiredAt->setTime((int) $expiredAt->format('H'), (int) $expiredAt->format('i'), (int) $expiredAt->format('s'), 0);

$token = $this->passwordTokenManager->createPasswordToken($user, $expiredAt, $provider);
$token = $this->passwordTokenManager->createPasswordToken($user, $provider, $expiredAt);
}

// Generate password token
Expand All @@ -78,14 +70,8 @@ public function resetPassword($propertyName, $value, ?ProviderInterface $provide
*
* @return bool
*/
public function updatePassword(AbstractPasswordToken $passwordToken, $password, ?ProviderInterface $provider = null)
public function updatePassword(AbstractPasswordToken $passwordToken, $password, ProviderInterface $provider)
{
/* @var null|Provider $provider */
if (!$provider) {
trigger_deprecation('tilleuls/forgot-password-bundle', '1.5', 'Parameter "%s" in method "%s" is recommended since 1.5 and will be mandatory in 2.0.', '$provider', __METHOD__);
$provider = $this->providerChain->get();
}

// Update user password
if ($this->dispatcher instanceof ContractsEventDispatcherInterface) {
$this->dispatcher->dispatch(new UpdatePasswordEvent($passwordToken, $password));
Expand Down
Loading

0 comments on commit 10e9613

Please sign in to comment.