publicKey = $this->generateToken(64); $this->user = $user; $this->basketLine = new ArrayCollection(); $this->status = TransactionStatusAction::TRANSACTION_CREATED; $this->total = 0; $this->totalPayer = 0; $this->idTransactionExternal = ''; $this->dateTimeCreated = new \DateTime(); } /** * @param int $length * @return bool|string */ private function generateToken($length = 64){ $hash = ""; while(strlen($hash) < $length){ try { $hash .= hash('sha512', random_bytes($length)); } catch (\Exception $e) { $hash .= hash('sha512', mt_rand(0,$length*10000)); } } return substr($hash, 0,$length); } /** * use to change the pk if its not unique */ public function regeneratePublicKey(){ $this->publicKey = $this->generateToken(); } public function getId(): ?int { return $this->id; } /** * @return string|null */ public function getPublicKey(): ?string { return $this->publicKey; } /** * @return Collection|BasketLineInterface[] */ public function getBasketLine(): Collection { return $this->basketLine; } /** * @param BasketLineInterface $_basketLine * @return TransactionInterface */ public function addBasketLine(BasketLineInterface $_basketLine): TransactionInterface { if (!$this->basketLine->contains($_basketLine)) { $this->basketLine[] = $_basketLine; $_basketLine->setTransaction($this); } $this->total = 0; foreach ($this->basketLine as $basketline){ /** * @var $basketline BasketLineInterface */ $this->total += $basketline->getTotalBasketLine(); } return $this; } /** * @param BasketLineInterface $_basketLine * @return TransactionInterface */ public function removeBasketLine(BasketLineInterface $_basketLine): TransactionInterface { if ($this->basketLine->contains($_basketLine)) { $this->basketLine->removeElement($_basketLine); // set the owning side to null (unless already changed) if ($_basketLine->getTransaction() === $this) { $_basketLine->setTransaction(null); } } $this->total = 0; foreach ($this->basketLine as $basketline){ /** * @var $basketline BasketLineInterface */ $this->total += $basketline->getTotalBasketLine(); } return $this; } /** * @return float|null */ public function getTotal(): ?float { return $this->total; } /** * @return UserBasketInterface */ public function getUser(): UserBasketInterface { return $this->user; } /** * @return int|null */ public function getStatus(): ?int { return $this->status; } /** * @param int $_status * @return TransactionInterface * @throws \Exception */ public function setStatus(int $_status): TransactionInterface { $this->status = $_status; $this->dateTimePayment = new \DateTime(); return $this; } public function getInformation(): ?string { return $this->information; } /** * @param string|null $_information * @return TransactionInterface */ public function setInformation(?string $_information): TransactionInterface { $this->information = $_information; return $this; } public function setTotal(?float $total): self { $this->total = $total; return $this; } public function setUser(?UserBasketInterface $user): self { $this->user = $user; return $this; } public function setPublicKey(string $publicKey): self { $this->publicKey = $publicKey; return $this; } /** * @param PaymentMode $paymentMode * @return Transaction */ public function setPaymentMode(PaymentMode $paymentMode): TransactionInterface { $this->paymentMode = $paymentMode; return $this; } /** * @return PaymentMode */ public function getPaymentMode(): PaymentMode { return $this->paymentMode; } /** * @return float */ public function getTotalPayer(): float { return $this->totalPayer; } /** * @param float $totalPayer * @return Transaction */ public function setTotalPayer(float $totalPayer): Transaction { $this->totalPayer = $totalPayer; return $this; } /** * @return string */ public function getIdTransactionExternal(): string { return $this->idTransactionExternal; } /** * @param string $idTransactionExternal * @return Transaction */ public function setIdTransactionExternal(string $idTransactionExternal): Transaction { $this->idTransactionExternal = $idTransactionExternal; return $this; } /** * @return \DateTime */ public function getDateTimeCreated(): \DateTime { return $this->dateTimeCreated; } public function setDateTimeCreated(\DateTimeInterface $dateTimeCreated): self { $this->dateTimeCreated = $dateTimeCreated; return $this; } /** * @return \DateTime */ public function getDateTimePayment(): \DateTime { return $this->dateTimePayment; } public function setDateTimePayment(?\DateTimeInterface $dateTimePayment): self { $this->dateTimePayment = $dateTimePayment; return $this; } }