class_transaction = Transaction::class; $this->entityManager = $_entityManager; $this->eventDispatcher = $container->get('event_dispatcher'); } /** * @param string $_publicKey * @return object|Transaction|null */ public function getByPublicKey(string $_publicKey){ return $this->entityManager->getRepository($this->class_transaction)->findOneBy(['publicKey' => $_publicKey]); } /** * @param UserBasketInterface $user * @return Transaction|null * @throws \Exception */ public function create(UserBasketInterface $user): ?TransactionInterface { $dateRef = (new \DateTime())->modify('- 10 seconds'); $transaction = $this->entityManager->getRepository($this->class_transaction)->fromDateTime($user, $dateRef); if($transaction === null){ $transaction = new $this->class_transaction($user); if(!$this->quickUpdate($transaction)) return null; } return $transaction; } /** * @param TransactionInterface $_transaction * @param BasketLineInterface $_basketLine * @return TransactionInterface */ public function addBasketLine(TransactionInterface $_transaction, BasketLineInterface $_basketLine): TransactionInterface { $transaction = $this->getByPublicKey($_transaction->getPublicKey()); if (!$transaction instanceof Transaction) return null; $transaction->addBasketLine($_basketLine); $this->quickUpdate($transaction); return $transaction; } public function removeAllBasketLine(TransactionInterface $_transaction){ $transaction = $this->getByPublicKey($_transaction->getPublicKey()); if (!$transaction instanceof Transaction) return null; foreach ($transaction->getBasketLine() as $item) { $transaction->removeBasketLine($item); } $this->quickUpdate($transaction); return $transaction; } /** * @param TransactionInterface $_transaction * @param BasketLineInterface $_basketLine * @return TransactionInterface */ public function removeBasketLine(TransactionInterface $_transaction, BasketLineInterface $_basketLine): TransactionInterface { $transaction = $this->getByPublicKey($_transaction->getPublicKey()); if (!$transaction instanceof Transaction) return null; $transaction->removeBasketLine($_basketLine); $this->quickUpdate($transaction); return $transaction; } /** * @param TransactionInterface $_transaction * @param PaymentMode $paymentMode * @return TransactionInterface * @throws \Exception */ public function confirmationTransaction(TransactionInterface $_transaction, PaymentMode $paymentMode): TransactionInterface { $transaction = $this->getByPublicKey($_transaction->getPublicKey()); if (!$transaction instanceof Transaction) return null; // We change status of Transaction $transaction->setStatus(TransactionStatusAction::TRANSACTION_WAITING_PAYMENT); $transaction->setPaymentMode($paymentMode); $this->update($transaction); // we dispatch event transaction waiting payment $event = new TransactionWaitingPaymentEvent($transaction); $this->eventDispatcher->dispatch($event, TransactionWaitingPaymentEvent::WAITING_PAYMENT); return $transaction; } /** * @param TransactionInterface $_transaction * @param PaymentMode $paymentMode * @return object|Transaction|null */ public function addPaymentMode(TransactionInterface $_transaction, PaymentMode $paymentMode){ $transaction = $this->getByPublicKey($_transaction->getPublicKey()); if (!$transaction instanceof Transaction) return null; $transaction->setPaymentMode($paymentMode); $this->quickUpdate($transaction); return $transaction; } public function update(TransactionInterface $element){ if(!$this->quickUpdate($element)) return false; $event = new TransactionResponseEvent($element); switch ($element->getStatus()){ case TransactionStatusAction::TRANSACTION_PAID : $this ->eventDispatcher->dispatch($event, TransactionResponseEvent::PAYMENT_SUCCESS); break; case TransactionStatusAction::TRANSACTION_WAITING_PAYMENT : $this ->eventDispatcher->dispatch($event, TransactionResponseEvent::WAITING_PAYMENT); break; default: return false; } return true; } /** * @param TransactionInterface $element * @return TransactionInterface|null */ public function quickUpdate(TransactionInterface $element): ?TransactionInterface { if (!$this->entityManager->contains($element)) { try { $this->entityManager->persist($element); } catch (ORMException $e) { return null; } } try { $this->entityManager->flush(); } catch (OptimisticLockException $e) { return null; } catch (ORMException $e) { return null; } return $element; } }