QuoteRepository.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /**
  3. * This file is part of the Klarna KP module
  4. *
  5. * (c) Klarna Bank AB (publ)
  6. *
  7. * For the full copyright and license information, please view the NOTICE
  8. * and LICENSE files that were distributed with this source code.
  9. */
  10. namespace Klarna\Kp\Model;
  11. use Klarna\Kp\Api\CreditApiInterface;
  12. use Klarna\Kp\Api\QuoteInterface;
  13. use Klarna\Kp\Api\QuoteRepositoryInterface;
  14. use Klarna\Kp\Model\ResourceModel\Quote as QuoteResource;
  15. use Magento\Framework\Exception\CouldNotSaveException;
  16. use Magento\Framework\Exception\NoSuchEntityException;
  17. use Magento\Quote\Api\Data\CartInterface as MageQuoteInterface;
  18. /**
  19. * Class QuoteRepository
  20. *
  21. * @package Klarna\Kp\Model
  22. */
  23. class QuoteRepository implements QuoteRepositoryInterface
  24. {
  25. /**
  26. * @var QuoteFactory
  27. */
  28. private $quoteFactory;
  29. /**
  30. * @var QuoteResource
  31. */
  32. private $resourceModel;
  33. /**
  34. * Holds a cache of instances to avoid unnecessary db and API calls
  35. *
  36. * @var array
  37. */
  38. private $instancesById = [];
  39. /**
  40. * Holds a cache of instances to avoid unnecessary db and API calls
  41. *
  42. * @var array
  43. */
  44. private $instances = [];
  45. /**
  46. * @var CreditApiInterface
  47. */
  48. private $api;
  49. /**
  50. * QuoteRepository constructor.
  51. *
  52. * @param QuoteFactory $quoteFactory
  53. * @param QuoteResource $resourceModel
  54. * @param CreditApiInterface $api
  55. * @codeCoverageIgnore
  56. */
  57. public function __construct(
  58. QuoteFactory $quoteFactory,
  59. QuoteResource $resourceModel,
  60. CreditApiInterface $api
  61. ) {
  62. $this->quoteFactory = $quoteFactory;
  63. $this->resourceModel = $resourceModel;
  64. $this->api = $api;
  65. }
  66. /**
  67. * Get quote by Magento quote
  68. *
  69. * @param MageQuoteInterface $mageQuote
  70. * @return QuoteInterface
  71. * @throws \Magento\Framework\Exception\NoSuchEntityException
  72. * @throws \Magento\Framework\Exception\LocalizedException
  73. */
  74. public function getActiveByQuote(MageQuoteInterface $mageQuote)
  75. {
  76. $quoteId = $this->resourceModel->getActiveByQuote($mageQuote);
  77. if (!$quoteId) {
  78. throw NoSuchEntityException::singleField('quote_id', $mageQuote->getId());
  79. }
  80. return $this->loadQuote('load', 'payments_quote_id', $quoteId);
  81. }
  82. /**
  83. * Load quote with different methods
  84. *
  85. * @param string $loadMethod
  86. * @param string $loadField
  87. * @param int $identifier
  88. * @throws NoSuchEntityException
  89. * @return QuoteInterface
  90. */
  91. public function loadQuote($loadMethod, $loadField, $identifier)
  92. {
  93. /** @var QuoteInterface $quote */
  94. $quote = $this->quoteFactory->create();
  95. $quote->$loadMethod($identifier, $loadField);
  96. if (!$quote->getId()) {
  97. throw NoSuchEntityException::singleField($loadField, $identifier);
  98. }
  99. return $quote;
  100. }
  101. /**
  102. * Delete quote by ID
  103. *
  104. * @param int $id
  105. * @return void
  106. * @throws \Magento\Framework\Exception\NoSuchEntityException
  107. */
  108. public function deleteById($id)
  109. {
  110. $this->delete($this->getById($id));
  111. }
  112. /**
  113. * Delete quote
  114. *
  115. * @param QuoteInterface $quote
  116. * @return void
  117. */
  118. public function delete(QuoteInterface $quote)
  119. {
  120. $quoteId = $quote->getId();
  121. $sessionId = $quote->getSessionId();
  122. $authToken = $quote->getAuthorizationToken();
  123. if ($authToken) { // Only need to call cancel if the Authorization Token is set
  124. $this->api->cancelOrder($authToken, $sessionId);
  125. }
  126. $this->resourceModel->delete($quote);
  127. unset($this->instances[$sessionId]);
  128. unset($this->instancesById[$quoteId]);
  129. }
  130. /**
  131. * Get quote by ID
  132. *
  133. * @param int $quoteId
  134. * @param bool $forceReload
  135. * @return QuoteInterface
  136. * @throws \Magento\Framework\Exception\NoSuchEntityException
  137. */
  138. public function getById($quoteId, $forceReload = false)
  139. {
  140. if (!isset($this->instancesById[$quoteId]) || $forceReload) {
  141. /** @var QuoteInterface $quote */
  142. $quote = $this->loadQuote('load', 'payments_quote_id', $quoteId);
  143. $this->cacheInstance($quote);
  144. }
  145. return $this->instancesById[$quoteId];
  146. }
  147. /**
  148. * Cache instance locally in memory to avoid additional DB calls
  149. *
  150. * @param QuoteInterface $quote
  151. */
  152. private function cacheInstance(QuoteInterface $quote)
  153. {
  154. $this->instancesById[$quote->getId()] = $quote;
  155. $this->instances[$quote->getSessionId()] = $quote;
  156. }
  157. /**
  158. * Mark quote as inactive and cancel it with API
  159. *
  160. * @param QuoteInterface $quote
  161. */
  162. public function markInactive(QuoteInterface $quote)
  163. {
  164. $quote->setIsActive(0);
  165. $this->save($quote);
  166. if ($quote->getAuthorizationToken()) {
  167. $this->api->cancelOrder($quote->getAuthorizationToken(), $quote->getSessionId());
  168. }
  169. }
  170. /**
  171. * Save Klarna Quote
  172. *
  173. * @param QuoteInterface $quote
  174. * @return \Klarna\Kp\Api\QuoteInterface
  175. * @throws CouldNotSaveException
  176. */
  177. public function save(QuoteInterface $quote)
  178. {
  179. try {
  180. return $this->resourceModel->save($quote);
  181. } catch (\Exception $e) {
  182. throw new CouldNotSaveException(__($e->getMessage()));
  183. }
  184. }
  185. /**
  186. * Load quote by session_id
  187. *
  188. * @param string $sessionId
  189. * @param bool $forceReload
  190. * @return QuoteInterface
  191. * @throws \Magento\Framework\Exception\NoSuchEntityException
  192. */
  193. public function getBySessionId($sessionId, $forceReload = false)
  194. {
  195. if ($forceReload || !isset($this->instances[$sessionId])) {
  196. $quote = $this->loadQuote('load', 'session_id', $sessionId);
  197. $this->cacheInstance($quote);
  198. }
  199. return $this->instances[$sessionId];
  200. }
  201. }