Skip to content

Commit

Permalink
Merge pull request #1367 from Mopolo/phpstan-level-2-correction-valid…
Browse files Browse the repository at this point in the history
…ators

Correction de validators pour suivre la doc officielle
  • Loading branch information
agallou authored Dec 2, 2023
2 parents 6ac1cff + d2fad48 commit 807153e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use AppBundle\Event\Ticket\TicketTypeAvailability;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

class AvailableTicketValidator extends ConstraintValidator
{
Expand All @@ -28,16 +29,14 @@ public function __construct(TicketTypeAvailability $ticketTypeAvailability, Even

public function validate($ticket, Constraint $constraint)
{
/**
* @var $ticket Ticket
*/
if (!$constraint instanceof AvailableTicket) {
throw new UnexpectedTypeException($constraint, AvailableTicket::class);
}

if (!($ticket instanceof Ticket) || $ticket->getTicketEventType() === null) {
return ;
}

/**
* @var $constraint AvailableTicket
*/
$event = $this->eventRepository->get($ticket->getTicketEventType()->getEventId());
if (
$ticket->getTicketEventType()->getDateEnd() < new \DateTime()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

class CorporateMemberValidator extends ConstraintValidator
{
Expand Down Expand Up @@ -40,9 +41,9 @@ public function __construct(

public function validate($tickets, Constraint $constraint)
{
/**
* @var $tickets Ticket[]
*/
if (!$constraint instanceof CorporateMember) {
throw new UnexpectedTypeException($constraint, CorporateMember::class);
}

$restrictedTickets = 0;
$eventId = null;
Expand All @@ -63,9 +64,6 @@ public function validate($tickets, Constraint $constraint)

$token = $this->tokenStorage->getToken();

/**
* @var $constraint CorporateMember
*/
if ($token === null) {
// Il faut etre connecté pour avoir accès aux tickets membre
$this->context->buildViolation($constraint->messageNotLoggedIn)
Expand All @@ -75,7 +73,7 @@ public function validate($tickets, Constraint $constraint)
}

/**
* @var $company CompanyMember
* @var CompanyMember $company
*/
$company = $this->companyMemberRepository->get($token->getUser()->getCompanyId());

Expand All @@ -97,7 +95,6 @@ public function validate($tickets, Constraint $constraint)
->atPath('tickets')
->addViolation()
;
return;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

class LoggedInMemberValidator extends ConstraintValidator
{
Expand All @@ -21,16 +22,14 @@ public function __construct(TokenStorageInterface $tokenStorage)

public function validate($ticket, Constraint $constraint)
{
/**
* @var $ticket Ticket
*/
if (!$constraint instanceof LoggedInMember) {
throw new UnexpectedTypeException($constraint, LoggedInMember::class);
}

if (!($ticket instanceof Ticket) || $ticket->getTicketEventType() === null || $ticket->getTicketEventType()->getTicketType()->getIsRestrictedToMembers() === false) {
return ;
}

/**
* @var $constraint LoggedInMember
*/
$token = $this->tokenStorage->getToken();
$message = null;
if ($token === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,22 @@
namespace AppBundle\Event\Validator\Constraints;

use AppBundle\Event\Model\Ticket;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

class PublicTicketValidator extends ConstraintValidator
{
/**
* @var TokenStorageInterface
*/
private $tokenStorage;

public function __construct(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}

public function validate($ticket, Constraint $constraint)
{
/**
* @var $ticket Ticket
*/
if (!$constraint instanceof PublicTicket) {
throw new UnexpectedTypeException($constraint, PublicTicket::class);
}

if (!($ticket instanceof Ticket) || $ticket->getTicketEventType() === null) {
return ;
}

/**
* @var $constraint PublicTicket
*/
if ($ticket->getTicketEventType()->getTicketType()->getIsRestrictedToMembers() === true) {
$this->context->buildViolation($constraint->messageNotLoggedIn)
->atPath('email')
Expand Down

0 comments on commit 807153e

Please sign in to comment.