Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - add Symfony Uid Support #199

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"symfony/framework-bundle": "^4.4 | ^5.0 | ^6.0",
"symfony/phpunit-bridge": "^5.0 | ^6.0",
"vimeo/psalm": "^4.3",
"doctrine/doctrine-bundle": "^2.0.3"
"doctrine/doctrine-bundle": "^2.0.3",
"symfony/uid": "^v5.1 | ^6.0"
},
"conflict": {
"doctrine/orm": "<2.7",
Expand Down
27 changes: 27 additions & 0 deletions src/Persistence/Repository/ResetPasswordRequestRepositoryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

namespace SymfonyCasts\Bundle\ResetPassword\Persistence\Repository;

use Symfony\Component\Uid\AbstractUid;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\Uuid;
use SymfonyCasts\Bundle\ResetPassword\Model\ResetPasswordRequestInterface;

/**
Expand Down Expand Up @@ -41,6 +44,8 @@ public function findResetPasswordRequest(string $selector): ?ResetPasswordReques

public function getMostRecentNonExpiredRequestDate(object $user): ?\DateTimeInterface
{
$params = $this->getQueryParams($user);

// Normally there is only 1 max request per use, but written to be flexible
/** @var ResetPasswordRequestInterface $resetPasswordRequest */
$resetPasswordRequest = $this->createQueryBuilder('t')
Expand All @@ -61,6 +66,8 @@ public function getMostRecentNonExpiredRequestDate(object $user): ?\DateTimeInte

public function removeResetPasswordRequest(ResetPasswordRequestInterface $resetPasswordRequest): void
{
$params = $this->getQueryParams($resetPasswordRequest->getUser());

$this->createQueryBuilder('t')
->delete()
->where('t.user = :user')
Expand All @@ -82,4 +89,24 @@ public function removeExpiredResetPasswordRequests(): int

return $query->execute();
}

private function getQueryParams(object $user): array
{
$paramValue = $user;
$paramType = null;

if (method_exists($paramValue, 'getId') && class_exists(AbstractUid::class)) {
if ($paramValue->getId instanceof Uuid) {
$paramType = 'uuid';
$paramValue = $paramValue->getId();
}

if ($paramValue->getId instanceof Ulid) {
$paramType = 'ulid';
$paramValue = $paramValue->getId();
}
}

return ['value' => $paramValue, 'type' => $paramType];
}
}
78 changes: 78 additions & 0 deletions tests/Fixtures/Entity/ResetPasswordTestFixtureUuidRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/*
* This file is part of the SymfonyCasts ResetPasswordBundle 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\ResetPassword\Tests\Fixtures\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;
use SymfonyCasts\Bundle\ResetPassword\Model\ResetPasswordRequestInterface;

/**
* @author Jesse Rushlow <[email protected]>
* @author Ryan Weaver <[email protected]>
*
* @internal
* @ORM\Entity(repositoryClass="SymfonyCasts\Bundle\ResetPassword\Tests\Fixtures\ResetPasswordTestFixtureRequestUuidRepository")
*/
final class ResetPasswordTestFixtureUuidRequest implements ResetPasswordRequestInterface
{
/**
* @ORM\Id()
* @ORM\Column(type="uuid")
*/
public $id;

/**
* @ORM\Column(type="string", nullable=true)
*/
public $selector;

/**
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
public $expiresAt;

/**
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
public $requestedAt;

/**
* @ORM\ManyToOne(targetEntity="ResetPasswordTestFixtureUser")
*/
public $user;

public function __construct()
{
$this->id = Uuid::v4();
}

public function getRequestedAt(): \DateTimeInterface
{
return $this->requestedAt;
}

public function isExpired(): bool
{
return $this->expiresAt->getTimestamp() <= time();
}

public function getExpiresAt(): \DateTimeInterface
{
}

public function getHashedToken(): string
{
}

public function getUser(): object
{
return $this->user;
}
}
43 changes: 43 additions & 0 deletions tests/Fixtures/ResetPasswordTestFixtureRequestUuidRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the SymfonyCasts ResetPasswordBundle 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\ResetPassword\Tests\Fixtures;

use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use SymfonyCasts\Bundle\ResetPassword\Model\ResetPasswordRequestInterface;
use SymfonyCasts\Bundle\ResetPassword\Persistence\Repository\ResetPasswordRequestRepositoryTrait;
use SymfonyCasts\Bundle\ResetPassword\Persistence\ResetPasswordRequestRepositoryInterface;
use SymfonyCasts\Bundle\ResetPassword\Tests\Fixtures\Entity\ResetPasswordTestFixtureUuidRequest;

/**
* @author Jesse Rushlow <[email protected]>
* @author Ryan Weaver <[email protected]>
*
* @internal
*/
final class ResetPasswordTestFixtureRequestUuidRepository extends ServiceEntityRepository implements ResetPasswordRequestRepositoryInterface
{
use ResetPasswordRequestRepositoryTrait;

private $manager;

public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, ResetPasswordTestFixtureUuidRequest::class);
}

public function createResetPasswordRequest(
object $user,
\DateTimeInterface $expiresAt,
string $selector,
string $hashedToken
): ResetPasswordRequestInterface {
}
}
Loading