src/Point/Doctrine/Entity/Transfer.php line 39

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Point\Doctrine\Entity;
  4. use ApiPlatform\Core\Annotation\ApiProperty;
  5. use ApiPlatform\Core\Annotation\ApiResource;
  6. use App\Point\Doctrine\Repository\TransferRepository;
  7. use App\Program\Doctrine\Entity\Profile;
  8. use DateTimeImmutable;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use Doctrine\Common\Collections\Collection;
  11. use Doctrine\ORM\Mapping\Column;
  12. use Doctrine\ORM\Mapping\Entity;
  13. use Doctrine\ORM\Mapping\GeneratedValue;
  14. use Doctrine\ORM\Mapping\Id;
  15. use Doctrine\ORM\Mapping\JoinColumn;
  16. use Doctrine\ORM\Mapping\JoinTable;
  17. use Doctrine\ORM\Mapping\ManyToMany;
  18. use Doctrine\ORM\Mapping\ManyToOne;
  19. use Doctrine\ORM\Mapping\Table;
  20. use Symfony\Component\Serializer\Annotation\Groups;
  21. use Symfony\Component\Validator\Constraints\Callback;
  22. use Symfony\Component\Validator\Constraints\GreaterThan;
  23. use Symfony\Component\Validator\Constraints\NotBlank;
  24. use Symfony\Component\Validator\Constraints\NotEqualTo;
  25. use Symfony\Component\Validator\Constraints\NotNull;
  26. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  27. #[ApiResource(
  28.     collectionOperations: ['get''post'],
  29.     itemOperations: ['get' => ['security' => 'is_granted("item", object)']],
  30.     normalizationContext: ['groups' => ['read']],
  31.     denormalizationContext: ['groups' => ['write']]
  32. )]
  33. #[Entity(repositoryClassTransferRepository::class)]
  34. #[Table(name'`point_transfer`')]
  35. class Transfer
  36. {
  37.     #[Id]
  38.     #[Column(type'integer')]
  39.     #[GeneratedValue]
  40.     #[Groups('read')]
  41.     private ?int $id null;
  42.     #[Column(type'datetime_immutable')]
  43.     #[Groups('read')]
  44.     private DateTimeImmutable $createdAt;
  45.     /**
  46.      * @var Collection<int, Transaction>
  47.      */
  48.     #[ManyToMany(targetEntityTransaction::class, cascade: ['persist'])]
  49.     #[JoinTable(name'`point_transfer_transactions`')]
  50.     #[Groups('read')]
  51.     #[ApiProperty(readableLinkfalse)]
  52.     private Collection $transactions;
  53.     #[Column(type'integer')]
  54.     #[NotBlank]
  55.     #[GreaterThan(0)]
  56.     #[Groups(['read''write'])]
  57.     private int $points 0;
  58.     #[ManyToOne(targetEntityAccount::class)]
  59.     #[JoinColumn(nullablefalseonDelete'CASCADE')]
  60.     #[NotNull]
  61.     #[Groups(['read''write'])]
  62.     #[ApiProperty(readableLinkfalsewritableLinkfalse)]
  63.     private Account $from;
  64.     #[ManyToOne(targetEntityAccount::class)]
  65.     #[JoinColumn(nullablefalseonDelete'CASCADE')]
  66.     #[NotNull]
  67.     #[NotEqualTo(propertyPath'from'message'Vous ne pouvez pas transférer de l\'argent à la même personne.')]
  68.     #[Groups(['read''write'])]
  69.     #[ApiProperty(readableLinkfalsewritableLinkfalse)]
  70.     private Account $to;
  71.     #[Column(nullabletrue)]
  72.     #[Groups(['read''write'])]
  73.     private ?string $comment null;
  74.     #[ManyToOne(targetEntityTransferType::class)]
  75.     #[JoinColumn(nullabletrueonDelete'CASCADE')]
  76.     #[Groups(['read''write'])]
  77.     #[ApiProperty(readableLinkfalsewritableLinkfalse)]
  78.     private ?TransferType $type null;
  79.     public function __construct()
  80.     {
  81.         $this->createdAt = new DateTimeImmutable();
  82.         $this->transactions = new ArrayCollection();
  83.     }
  84.     public function getId(): ?int
  85.     {
  86.         return $this->id;
  87.     }
  88.     public function getCreatedAt(): DateTimeImmutable
  89.     {
  90.         return $this->createdAt;
  91.     }
  92.     /**
  93.      * @return Collection<int, Transaction>
  94.      */
  95.     public function getTransactions(): ArrayCollection|Collection
  96.     {
  97.         return $this->transactions;
  98.     }
  99.     public function getPoints(): int
  100.     {
  101.         return $this->points;
  102.     }
  103.     public function setPoints(int $points): void
  104.     {
  105.         $this->points $points;
  106.     }
  107.     public function getFrom(): Account
  108.     {
  109.         return $this->from;
  110.     }
  111.     public function setFrom(Account $from): void
  112.     {
  113.         $this->from $from;
  114.     }
  115.     public function getTo(): Account
  116.     {
  117.         return $this->to;
  118.     }
  119.     public function setTo(Account $to): void
  120.     {
  121.         $this->to $to;
  122.     }
  123.     #[Callback]
  124.     public function validate(ExecutionContextInterface $context): void
  125.     {
  126.         if ($this->from->getBalance() < $this->points) {
  127.             $context->buildViolation('Le nombre de points est supérieur au solde de points de l\'émetteur.')
  128.                 ->atPath('points')
  129.                 ->addViolation();
  130.         }
  131.         /** @var Profile $fromProfile */
  132.         $fromProfile $this->from->getProfile();
  133.         /** @var Profile $toProfile */
  134.         $toProfile $this->to->getProfile();
  135.         if ($fromProfile->getProgram() !== $toProfile->getProgram()) {
  136.             $context->buildViolation('Le destinataire n\'appartient pas au même programme que l\'émetteur.')
  137.                 ->atPath('to')
  138.                 ->addViolation();
  139.         }
  140.     }
  141.     public function getComment(): ?string
  142.     {
  143.         return $this->comment;
  144.     }
  145.     public function setComment(?string $comment): void
  146.     {
  147.         $this->comment $comment;
  148.     }
  149.     public function getType(): ?TransferType
  150.     {
  151.         return $this->type;
  152.     }
  153.     public function setType(?TransferType $type): void
  154.     {
  155.         $this->type $type;
  156.     }
  157. }