Skip to content

Commit a055d9d

Browse files
committed
ISSUE-345: refactor
1 parent ad6651e commit a055d9d

8 files changed

+52
-22
lines changed

config/services.yml

-8
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ services:
1010
autowire: true
1111
tags: ['controller.service_arguments']
1212

13-
# Symfony\Component\Serializer\SerializerInterface:
14-
# autowire: true
15-
# autoconfigure: true
16-
1713
my.secure_handler:
1814
class: PhpList\RestBundle\ViewHandler\SecuredViewHandler
1915

@@ -29,7 +25,3 @@ services:
2925
PhpList\Core\Domain\Repository\Messaging\SubscriberListRepository:
3026
autowire: true
3127
autoconfigure: true
32-
33-
PhpList\RestBundle\Validator\RequestValidator:
34-
autowire: true
35-
autoconfigure: true

config/services/validators.yml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
services:
2+
PhpList\RestBundle\Validator\RequestValidator:
3+
autowire: true
4+
autoconfigure: true
5+
6+
PhpList\RestBundle\Validator\UniqueEmailValidator:
7+
autowire: true
8+
autoconfigure: true
9+
tags: [ 'validator.constraint_validator' ]

src/Controller/SubscriberController.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44

55
namespace PhpList\RestBundle\Controller;
66

7+
use OpenApi\Attributes as OA;
8+
use PhpList\Core\Domain\Repository\Subscription\SubscriberRepository;
9+
use PhpList\Core\Security\Authentication;
10+
use PhpList\RestBundle\Controller\Traits\AuthenticationTrait;
711
use PhpList\RestBundle\Entity\SubscriberRequest;
812
use PhpList\RestBundle\Service\Manager\SubscriberManager;
913
use PhpList\RestBundle\Validator\RequestValidator;
1014
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11-
use PhpList\Core\Domain\Repository\Subscription\SubscriberRepository;
12-
use PhpList\Core\Security\Authentication;
13-
use PhpList\RestBundle\Controller\Traits\AuthenticationTrait;
1415
use Symfony\Component\HttpFoundation\JsonResponse;
1516
use Symfony\Component\HttpFoundation\Request;
1617
use Symfony\Component\HttpFoundation\Response;
1718
use Symfony\Component\Routing\Attribute\Route;
1819
use Symfony\Component\Serializer\SerializerInterface;
19-
use OpenApi\Attributes as OA;
2020

2121
/**
2222
* This controller provides REST API access to subscribers.

src/Entity/RequestInterface.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestBundle\Entity;
6+
7+
interface RequestInterface
8+
{
9+
10+
}

src/Entity/SubscriberRequest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
namespace PhpList\RestBundle\Entity;
66

7+
use PhpList\RestBundle\Validator as CustomAssert;
78
use Symfony\Component\Validator\Constraints as Assert;
8-
use PhpList\RestBundle\Validator\UniqueEmail;
99

10-
class SubscriberRequest
10+
class SubscriberRequest implements RequestInterface
1111
{
1212
#[Assert\NotBlank]
1313
#[Assert\Email]
14-
#[UniqueEmail]
14+
#[CustomAssert\UniqueEmail]
1515
public string $email;
1616

1717
#[Assert\Type(type: 'bool')]

src/Validator/RequestValidator.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpList\RestBundle\Validator;
66

7+
use PhpList\RestBundle\Entity\RequestInterface;
78
use Symfony\Component\HttpFoundation\Request;
89
use Symfony\Component\Serializer\SerializerInterface;
910
use Symfony\Component\Validator\Validator\ValidatorInterface;
@@ -16,7 +17,7 @@ public function __construct(
1617
private readonly ValidatorInterface $validator
1718
) {}
1819

19-
public function validate(Request $request, string $dtoClass): object
20+
public function validate(Request $request, string $dtoClass): RequestInterface
2021
{
2122
$dto = $this->serializer->deserialize($request->getContent(), $dtoClass, 'json');
2223

src/Validator/UniqueEmail.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,17 @@
66

77
use Symfony\Component\Validator\Constraint;
88

9-
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
9+
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
1010
class UniqueEmail extends Constraint
1111
{
12-
public string $message = 'The email "{{ value }}" is already in use.';
12+
public string $message = 'The email "{{ value }}" is already taken.';
13+
public string $mode = 'strict';
14+
15+
public function __construct(?string $mode = null, ?string $message = null, ?array $groups = null, $payload = null)
16+
{
17+
parent::__construct([], $groups, $payload);
18+
19+
$this->mode = $mode ?? $this->mode;
20+
$this->message = $message ?? $this->message;
21+
}
1322
}

src/Validator/UniqueEmailValidator.php

+13-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use PhpList\Core\Domain\Repository\Subscription\SubscriberRepository;
88
use Symfony\Component\Validator\Constraint;
99
use Symfony\Component\Validator\ConstraintValidator;
10+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
11+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1012

1113
class UniqueEmailValidator extends ConstraintValidator
1214
{
@@ -17,19 +19,26 @@ public function __construct(SubscriberRepository $subscriberRepository)
1719
$this->subscriberRepository = $subscriberRepository;
1820
}
1921

20-
public function validate($value, Constraint $constraint)
22+
public function validate($value, Constraint $constraint): void
2123
{
22-
/* @var $constraint UniqueEmail */
24+
if (!$constraint instanceof UniqueEmail) {
25+
throw new UnexpectedTypeException($constraint, UniqueEmail::class);
26+
}
2327

2428
if (null === $value || '' === $value) {
2529
return;
2630
}
2731

28-
if ($this->subscriberRepository->findOneByEmail($value)) {
32+
if (!is_string($value)) {
33+
throw new UnexpectedValueException($value, 'string');
34+
}
35+
36+
$existingUser = $this->subscriberRepository->findOneBy(['email' => $value]);
37+
38+
if ($existingUser) {
2939
$this->context->buildViolation($constraint->message)
3040
->setParameter('{{ value }}', $value)
3141
->addViolation();
3242
}
3343
}
3444
}
35-

0 commit comments

Comments
 (0)