Skip to content

Commit

Permalink
VSVGVQ-51: add UrlSuffixGenerator and interface, add timezone to date…
Browse files Browse the repository at this point in the history
…time in registration, remove find on user in registrationdoctrinerepository
  • Loading branch information
stejes committed Jun 7, 2018
1 parent 4c3c8b0 commit 54e7881
Show file tree
Hide file tree
Showing 14 changed files with 201 additions and 64 deletions.
14 changes: 0 additions & 14 deletions src/Registration/HashCodeGenerator.php

This file was deleted.

17 changes: 9 additions & 8 deletions src/Registration/Models/Registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace VSV\GVQ_API\Registration\Models;

use Ramsey\Uuid\UuidInterface;
use VSV\GVQ_API\Registration\ValueObjects\UrlSuffix;
use VSV\GVQ_API\User\Models\User;

class Registration
Expand All @@ -12,9 +13,9 @@ class Registration
*/
private $id;
/**
* @var string
* @var UrlSuffix
*/
private $hashCode;
private $urlSuffix;

/**
* @var User
Expand All @@ -33,20 +34,20 @@ class Registration

/**
* @param UuidInterface $id
* @param string $hashCode
* @param UrlSuffix $hashCode
* @param User $user
* @param \DateTimeImmutable $createdOn
* @param bool $passwordReset
*/
public function __construct(
UuidInterface $id,
string $hashCode,
UrlSuffix $hashCode,
User $user,
\DateTimeImmutable $createdOn,
bool $passwordReset
) {
$this->id = $id;
$this->hashCode = $hashCode;
$this->urlSuffix = $hashCode;
$this->user = $user;
$this->createdOn = $createdOn;
$this->passwordReset = $passwordReset;
Expand All @@ -61,11 +62,11 @@ public function getId(): UuidInterface
}

/**
* @return string
* @return UrlSuffix
*/
public function getHashCode(): string
public function getUrlSuffix(): UrlSuffix
{
return $this->hashCode;
return $this->urlSuffix;
}

/**
Expand Down
22 changes: 13 additions & 9 deletions src/Registration/Repositories/Entities/RegistrationEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,30 @@
use Ramsey\Uuid\Uuid;
use VSV\GVQ_API\Common\Repositories\Entities\Entity;
use VSV\GVQ_API\Registration\Models\Registration;
use VSV\GVQ_API\Registration\ValueObjects\UrlSuffix;
use VSV\GVQ_API\User\Repositories\Entities\UserEntity;

/**
* @ORM\Entity()
* @ORM\Table(name="registration")
* @ORM\Table(name="registration", indexes={
* @ORM\Index(name="url_suffix_idx", columns={"url_suffix"}),
* @ORM\Index(name="user_id_idx", columns={"user_id"})
* })
*/
class RegistrationEntity extends Entity
{
/**
* @var string
*
* @ORM\Column(type="string", name="hash_code", length=22, nullable=false)
* @ORM\Column(type="string", name="url_suffix", length=22, nullable=false, unique=true)
*/
private $hashCode;
private $urlSuffix;

/**
* @var UserEntity
*
* @ORM\OneToOne(targetEntity="VSV\GVQ_API\User\Repositories\Entities\UserEntity", fetch="EAGER")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false)
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false, unique=true)
*/
private $userEntity;

Expand Down Expand Up @@ -57,7 +61,7 @@ public function __construct(
bool $passwordReset
) {
parent::__construct($id);
$this->hashCode = $hashCode;
$this->urlSuffix = $hashCode;
$this->userEntity = $userEntity;
$this->createdOn = $createdOn;
$this->passwordReset = $passwordReset;
Expand All @@ -72,7 +76,7 @@ public static function fromRegistration(Registration $registration): Registratio
{
return new RegistrationEntity(
$registration->getId()->toString(),
$registration->getHashCode(),
$registration->getUrlSuffix()->toNative(),
UserEntity::fromUser($registration->getUser()),
$registration->getCreatedOn(),
$registration->isPasswordReset()
Expand All @@ -86,7 +90,7 @@ public function toRegistration(): Registration
{
return new Registration(
Uuid::fromString($this->getId()),
$this->getHashCode(),
new UrlSuffix($this->getUrlSuffix()),
$this->getUserEntity()->toUser(),
$this->getCreatedOn(),
$this->isPasswordReset()
Expand All @@ -96,9 +100,9 @@ public function toRegistration(): Registration
/**
* @return string
*/
public function getHashCode(): string
public function getUrlSuffix(): string
{
return $this->hashCode;
return $this->urlSuffix;
}

/**
Expand Down
25 changes: 6 additions & 19 deletions src/Registration/Repositories/RegistrationDoctrineRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,21 @@ public function save(Registration $registration): void
{
$registrationEntity = RegistrationEntity::fromRegistration($registration);

/** @var UserEntity $userEntity */
$userEntity = $this->entityManager->find(
UserEntity::class,
$registrationEntity->getUserEntity()->getId()
);

if ($userEntity == null) {
throw new \InvalidArgumentException(
'User with id: '.$registrationEntity->getUserEntity()->getId().' not found.'
);
}

$registrationEntity->setUserEntity($userEntity);

$this->entityManager->persist($registrationEntity);
//the user object inside registration is not managed, therefore we need to use merge instead of persist
$this->entityManager->merge($registrationEntity);
$this->entityManager->flush();
}

/**
* @param string $hashCode
* @return Registration
* @param string $urlSuffix
* @return Registration|null
*/
public function getByHashCode(string $hashCode): ?Registration
public function getByUrlSuffix(string $urlSuffix): ?Registration
{
/** @var RegistrationEntity|null $registrationEntity */
$registrationEntity = $this->objectRepository->findOneBy(
[
'hashCode' => $hashCode,
'urlSuffix' => $urlSuffix,
]
);

Expand Down
6 changes: 6 additions & 0 deletions src/Registration/Repositories/RegistrationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ interface RegistrationRepository
* @param Registration $registration
*/
public function save(Registration $registration): void;

/**
* @param string $urlSuffix
* @return Registration|null
*/
public function getByUrlSuffix(string $urlSuffix): ?Registration;
}
10 changes: 10 additions & 0 deletions src/Registration/UrlSuffixGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace VSV\GVQ_API\Registration;

use VSV\GVQ_API\Registration\ValueObjects\UrlSuffix;

interface UrlSuffixGenerator
{
public function createUrlSuffix(): UrlSuffix;
}
13 changes: 13 additions & 0 deletions src/Registration/UrlSuffixHashGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types=1);

namespace VSV\GVQ_API\Registration;

use VSV\GVQ_API\Registration\ValueObjects\UrlSuffix;

class UrlSuffixHashGenerator implements UrlSuffixGenerator
{
public function createUrlSuffix(): UrlSuffix
{
return new UrlSuffix(bin2hex(random_bytes(11)));
}
}
47 changes: 47 additions & 0 deletions src/Registration/ValueObjects/UrlSuffix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php declare(strict_types=1);

namespace VSV\GVQ_API\Registration\ValueObjects;

class UrlSuffix
{
/**
* @var string
*/
private $value;

/**
* @var int
*/
private $minLimit = 22;

/**
* @param string $value
*/
public function __construct(string $value)
{
if (strlen($value) < $this->minLimit) {
throw new \InvalidArgumentException(
'Value has to be at least '.$this->minLimit.' characters long, '.strlen($value).' given.'
);
}

$this->value = $value;
}

/**
* @return string
*/
public function toNative(): string
{
return $this->value;
}

/**
* @param UrlSuffix $suffix
* @return bool
*/
public function equals(UrlSuffix $suffix): bool
{
return $this->toNative() === $suffix->toNative();
}
}
5 changes: 1 addition & 4 deletions tests/Common/Repositories/AbstractDoctrineRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ protected function setUp(): void
{
$configuration = Setup::createAnnotationMetadataConfiguration(
[
__DIR__.'/../../../src/Question/Repositories/Entities',
__DIR__.'/../../../src/User/Repositories/Entities',
__DIR__.'/../../../src/Company/Repositories/Entities',
__DIR__.'/../../../src/Registration/Repositories/Entities',
__DIR__.'/../../../src/'
],
true,
null,
Expand Down
9 changes: 5 additions & 4 deletions tests/Factory/ModelsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
use VSV\GVQ_API\Question\Models\Question;
use VSV\GVQ_API\Question\Models\Questions;
use VSV\GVQ_API\Question\ValueObjects\Year;
use VSV\GVQ_API\Registration\HashCodeGenerator;
use VSV\GVQ_API\Registration\UrlSuffixHashGenerator;
use VSV\GVQ_API\Registration\Models\Registration;
use VSV\GVQ_API\Registration\ValueObjects\UrlSuffix;
use VSV\GVQ_API\User\Models\User;
use VSV\GVQ_API\User\ValueObjects\Email;
use VSV\GVQ_API\User\ValueObjects\Password;
Expand Down Expand Up @@ -266,9 +267,9 @@ public static function createRegistration(): Registration
{
return new Registration(
Uuid::fromString('00f20af9-c2f5-4bfb-9424-5c0c29fbc2e3'),
'd2c63a605ae27c13e43e26fe2c97a36c4556846dd3ef',
new UrlSuffix('d2c63a605ae27c13e43e26fe2c97a36c4556846dd3ef'),
self::createUser(),
new \DateTimeImmutable('2020-02-02'),
new \DateTimeImmutable('2020-02-02', new \DateTimeZone('GMT+1')),
false
);
}
Expand All @@ -281,7 +282,7 @@ public static function createRegistrationWithAlternateUser(): Registration
{
return new Registration(
Uuid::fromString('00f20af9-c2f5-4bfb-9424-5c0c29fbc2e3'),
'd2c63a605ae27c13e43e26fe2c97a36c4556846dd3ef',
new UrlSuffix('d2c63a605ae27c13e43e26fe2c97a36c4556846dd3ef'),
self::createAlternateUser(),
new \DateTimeImmutable('2020-02-02'),
false
Expand Down
7 changes: 4 additions & 3 deletions tests/Registration/Models/RegistrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\TestCase;
use VSV\GVQ_API\Factory\ModelsFactory;
use VSV\GVQ_API\Registration\ValueObjects\UrlSuffix;

class RegistrationTest extends TestCase
{
Expand All @@ -26,8 +27,8 @@ protected function setUp(): void
public function it_stores_a_hashcode(): void
{
$this->assertEquals(
'd2c63a605ae27c13e43e26fe2c97a36c4556846dd3ef',
$this->registration->getHashCode()
new UrlSuffix('d2c63a605ae27c13e43e26fe2c97a36c4556846dd3ef'),
$this->registration->getUrlSuffix()
);
}

Expand All @@ -49,7 +50,7 @@ public function it_stores_a_user(): void
public function it_stores_a_created_on(): void
{
$this->assertEquals(
new \DateTimeImmutable('2020-02-02'),
new \DateTimeImmutable('2020-02-02', new \DateTimeZone('GMT+1')),
$this->registration->getCreatedOn()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace VSV\GVQ_API\Registration\Repositories;

use Doctrine\ORM\ORMInvalidArgumentException;
use PhpParser\Node\Expr\AssignOp\Mod;
use VSV\GVQ_API\Common\Repositories\AbstractDoctrineRepositoryTest;
use VSV\GVQ_API\Factory\ModelsFactory;
Expand Down Expand Up @@ -59,7 +60,7 @@ public function it_can_save_a_registration(): void
{
$this->registrationDoctrineRepository->save($this->registration);

$foundRegistration = $this->registrationDoctrineRepository->getByHashCode(
$foundRegistration = $this->registrationDoctrineRepository->getByUrlSuffix(
'd2c63a605ae27c13e43e26fe2c97a36c4556846dd3ef'
);

Expand All @@ -74,9 +75,9 @@ public function it_throws_on_saving_with_an_invalid_user(): void
{
$registration = ModelsFactory::createRegistrationWithAlternateUser();

$this->expectException(\InvalidArgumentException::class);
$this->expectException(ORMInvalidArgumentException::class);
$this->expectExceptionMessage(
'User with id: '.$registration->getUser()->getId()->toString().' not found.'
'A new entity was found through the relationship'
);

$this->registrationDoctrineRepository->save($registration);
Expand Down
Loading

0 comments on commit 54e7881

Please sign in to comment.