src/Point/Doctrine/Entity/Account.php line 36

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\AccountRepository;
  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\ManyToOne;
  17. use Doctrine\ORM\Mapping\OneToMany;
  18. use Doctrine\ORM\Mapping\Table;
  19. use Symfony\Component\Serializer\Annotation\Groups;
  20. use Symfony\Component\Validator\Constraints\NotBlank;
  21. use Symfony\Component\Validator\Constraints\NotNull;
  22. #[ApiResource(
  23.     collectionOperations: ['post''get'],
  24.     itemOperations: [
  25.         'put' => ['security' => 'is_granted("item", object)'],
  26.         'get' => ['security' => 'is_granted("item", object)'],
  27.     ],
  28.     normalizationContext: ['groups' => ['read']]
  29. )]
  30. #[Entity(repositoryClassAccountRepository::class)]
  31. #[Table(name'`point_account`')]
  32. class Account
  33. {
  34.     #[Id]
  35.     #[Column(type'integer')]
  36.     #[GeneratedValue]
  37.     #[Groups('read')]
  38.     private ?int $id null;
  39.     #[Column]
  40.     #[NotBlank]
  41.     #[Groups('read')]
  42.     private string $name;
  43.     #[ManyToOne(targetEntityProfile::class, inversedBy'accounts')]
  44.     #[JoinColumn(onDelete'SET NULL')]
  45.     #[NotNull]
  46.     #[Groups('read')]
  47.     #[ApiProperty(readableLinkfalsewritableLinkfalse)]
  48.     private ?Profile $profile null;
  49.     #[Column(type'datetime_immutable')]
  50.     #[Groups('read')]
  51.     private DateTimeImmutable $createdAt;
  52.     /**
  53.      * @var Collection<int, Wallet>
  54.      */
  55.     #[OneToMany(mappedBy'account'targetEntityWallet::class)]
  56.     private Collection $wallets;
  57.     /**
  58.      * @var Collection<int, Transaction>
  59.      */
  60.     #[OneToMany(mappedBy'account'targetEntityTransaction::class)]
  61.     private Collection $transactions;
  62.     public function __construct()
  63.     {
  64.         $this->createdAt = new DateTimeImmutable();
  65.         $this->wallets = new ArrayCollection();
  66.         $this->transactions = new ArrayCollection();
  67.     }
  68.     public function getId(): ?int
  69.     {
  70.         return $this->id;
  71.     }
  72.     public function getName(): string
  73.     {
  74.         return $this->name;
  75.     }
  76.     public function setName(string $name): void
  77.     {
  78.         $this->name $name;
  79.     }
  80.     public function getProfile(): ?Profile
  81.     {
  82.         return $this->profile;
  83.     }
  84.     public function setProfile(?Profile $profile): void
  85.     {
  86.         $this->profile $profile;
  87.     }
  88.     public function getCreatedAt(): DateTimeImmutable
  89.     {
  90.         return $this->createdAt;
  91.     }
  92.     /**
  93.      * @return Collection<int, Wallet>
  94.      */
  95.     public function getWallets(): Collection
  96.     {
  97.         return $this->wallets;
  98.     }
  99.     /**
  100.      * @return Collection<int, Transaction>
  101.      */
  102.     public function getTransactions(): Collection
  103.     {
  104.         return $this->transactions;
  105.     }
  106.     /**
  107.      * @return Collection<int, Wallet>
  108.      */
  109.     public function getRemainingWallets(): Collection
  110.     {
  111.         return $this->wallets->filter(static fn (Wallet $wallet) => !$wallet->isExpired() && $wallet->getBalance() > 0);
  112.     }
  113.     #[Groups('read')]
  114.     public function getBalance(): int
  115.     {
  116.         return intval(
  117.             array_sum(
  118.                 $this->getRemainingWallets()->map(static fn (Wallet $wallet) => $wallet->getBalance())->toArray()
  119.             )
  120.         );
  121.     }
  122. }