1111use Magefan \GoogleTagManager \Model \ResourceModel \Transaction \CollectionFactory as TransactionCollectionFactory ;
1212use Magefan \GoogleTagManager \Model \TransactionFactory ;
1313use Magefan \GoogleTagManager \Model \TransactionRepository ;
14+ use Magefan \GoogleTagManager \Api \Transaction \LogInterface ;
15+ use Magento \Sales \Model \OrderFactory ;
1416use Magento \Sales \Api \Data \OrderInterface as Order ;
1517use Magento \Framework \Exception \CouldNotSaveException ;
1618use Psr \Log \LoggerInterface ;
1719
18- class Log
20+ class Log implements LogInterface
1921{
2022 /**
2123 * @var TransactionCollectionFactory
@@ -32,37 +34,47 @@ class Log
3234 */
3335 private $ transactionRepository ;
3436
37+ /**
38+ * @var OrderFactory
39+ */
40+ private $ orderFactory ;
41+
3542 /**
3643 * @var LoggerInterface
3744 */
3845 private $ logger ;
3946
47+
4048 /**
41- * Log constructor.
4249 * @param TransactionCollectionFactory $transactionCollectionFactory
4350 * @param TransactionFactory $transactionFactory
4451 * @param TransactionRepository $transactionRepository
52+ * @param OrderFactory $orderFactory
4553 * @param LoggerInterface $logger
4654 */
4755 public function __construct (
4856 TransactionCollectionFactory $ transactionCollectionFactory ,
4957 TransactionFactory $ transactionFactory ,
5058 TransactionRepository $ transactionRepository ,
59+ OrderFactory $ orderFactory ,
5160 LoggerInterface $ logger
5261 ) {
5362 $ this ->transactionCollectionFactory = $ transactionCollectionFactory ;
5463 $ this ->transactionFactory = $ transactionFactory ;
5564 $ this ->transactionRepository = $ transactionRepository ;
65+ $ this ->orderFactory = $ orderFactory ;
5666 $ this ->logger = $ logger ;
5767 }
5868
5969 /**
60- * @param Order $order
70+ * @param mixed $order
6171 * @param string $requester
6272 * @return void
6373 */
64- public function logTransaction (Order $ order , string $ requester )
74+ public function logTransaction ($ order , string $ requester )
6575 {
76+ $ order = $ this ->getOrder ($ order );
77+
6678 $ transactionModel = $ this ->transactionFactory ->create ();
6779
6880 $ transactionModel ->setTransactionId ((string )$ order ->getIncrementId ());
@@ -78,18 +90,20 @@ public function logTransaction(Order $order, string $requester)
7890 }
7991
8092 /**
81- * @param Order $order
93+ * @param mixed $order
8294 * @param string $requester
83- * @return void
95+ * @return bool
8496 */
85- public function isTransactionUnique (Order $ order , string $ requester ): bool
97+ public function isTransactionUnique ($ order , string $ requester ): bool
8698 {
99+ $ order = $ this ->getOrder ($ order );
100+
87101 $ transactionsForRequesterByTransactionId = $ this ->transactionFactory ->create ()->getCollection ()->addFieldToFilter (
88102 'requester ' ,
89103 $ requester
90104 )->addFieldToFilter (
91105 'transaction_id ' ,
92- ( string ) $ order ->getIncrementId ()
106+ $ order ->getIncrementId ()
93107 )->addFieldToFilter (
94108 'store_id ' ,
95109 (int )$ order ->getStoreId ()
@@ -101,4 +115,25 @@ public function isTransactionUnique(Order $order, string $requester): bool
101115
102116 return true ;
103117 }
118+
119+ /**
120+ * @param $order
121+ * @return Order
122+ * @throws \Exception
123+ */
124+ private function getOrder ($ order ): Order
125+ {
126+ if (is_object ()) {
127+ if (!($ order instanceof Order)) {
128+ throw new \Exception ('Object is not instance of OrderInterface. ' );
129+ }
130+ } else {
131+ $ order = $ this ->orderFactory ->create ()->loadByIncrementId ((string )$ order );
132+ if (!$ order ->getId ()) {
133+ throw new \Exception ('Order with such ID does not exist. ' );
134+ }
135+ }
136+
137+ return $ order ;
138+ }
104139}
0 commit comments