-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWorkflowProvider.php
More file actions
197 lines (182 loc) · 5.65 KB
/
Copy pathWorkflowProvider.php
File metadata and controls
197 lines (182 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
/**
* Copyright © Klarna Bank AB (publ)
*
* For the full copyright and license information, please view the NOTICE
* and LICENSE files that were distributed with this source code.
*/
declare(strict_types=1);
namespace Klarna\Kco\Model;
use Klarna\Base\Exception as KlarnaException;
use Klarna\Kco\Api\QuoteRepositoryInterface as KcoQuoteRepositoryInterface;
use Magento\Quote\Model\QuoteRepository as MagentoQuoteRepository;
use Klarna\Kco\Api\QuoteInterface;
use Klarna\Base\Api\OrderInterface;
use Klarna\Base\Model\OrderRepository as KlarnaOrderRepository;
use Magento\Sales\Model\OrderRepository as MagentoOrderRepository;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Sales\Api\Data\OrderInterface as MagentoOrder;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\InputException;
/**
* Providing quotes and orders for the checkout workflow based on a given klarna order id
*
* @internal
*/
class WorkflowProvider
{
/**
* @var KcoQuoteRepositoryInterface
*/
private $kcoQuoteRepository;
/**
* @var MagentoQuoteRepository
*/
private $magentoQuoteRepository;
/**
* @var KlarnaOrderRepository
*/
private $klarnaOrderRepository;
/**
* @var MagentoOrderRepository
*/
private $magentoOrderRepository;
/**
* @var QuoteInterface
*/
private $kcoQuote;
/**
* @var CartInterface
*/
private $magentoQuote;
/**
* @var OrderInterface
*/
private $klarnaOrder;
/**
* @var MagentoOrder
*/
private $magentoOrder;
/**
* @var string
*/
private $klarnaOrderId;
/**
* @param KcoQuoteRepositoryInterface $kcoQuoteRepository
* @param MagentoQuoteRepository $magentoQuoteRepository
* @param KlarnaOrderRepository $klarnaOrderRepository
* @param MagentoOrderRepository $magentoOrderRepository
* @codeCoverageIgnore
*/
public function __construct(
KcoQuoteRepositoryInterface $kcoQuoteRepository,
MagentoQuoteRepository $magentoQuoteRepository,
KlarnaOrderRepository $klarnaOrderRepository,
MagentoOrderRepository $magentoOrderRepository
) {
$this->kcoQuoteRepository = $kcoQuoteRepository;
$this->magentoQuoteRepository = $magentoQuoteRepository;
$this->klarnaOrderRepository = $klarnaOrderRepository;
$this->magentoOrderRepository = $magentoOrderRepository;
}
/**
* Setting the klarna order id
*
* @param string $klarnaOrderId
* @throws KlarnaException
*/
public function setKlarnaOrderId(string $klarnaOrderId): void
{
if (empty($klarnaOrderId)) {
throw new KlarnaException(__('The provided Kustom order id is empty'));
}
$this->klarnaOrderId = $klarnaOrderId;
}
/**
* Getting back the kco quote
*
* @throws KlarnaException
* @return QuoteInterface
*/
public function getKcoQuote(): QuoteInterface
{
if ($this->kcoQuote === null) {
try {
$this->kcoQuote = $this->kcoQuoteRepository->getByCheckoutId($this->klarnaOrderId);
} catch (NoSuchEntityException $e) {
throw new KlarnaException(__(
'No Kustom Kco quote could be found with the provided Kustom order id: %1',
$this->klarnaOrderId
));
}
}
return $this->kcoQuote;
}
/**
* Getting back the Magento quote
*
* @throws KlarnaException
* @return CartInterface
*/
public function getMagentoQuote(): CartInterface
{
if ($this->magentoQuote === null) {
try {
$this->magentoQuote = $this->magentoQuoteRepository->get($this->getKcoQuote()->getQuoteId());
} catch (NoSuchEntityException $e) {
throw new KlarnaException(__(
'No active Magento quote could be found with the provided quote id: %1',
$this->getKcoQuote()->getQuoteId()
));
}
}
return $this->magentoQuote;
}
/**
* Getting back the klarna order
*
* @throws KlarnaException
* @return OrderInterface
*/
public function getKlarnaOrder(): OrderInterface
{
if ($this->klarnaOrder === null) {
try {
$this->klarnaOrder = $this->klarnaOrderRepository->getByKlarnaOrderId($this->klarnaOrderId);
} catch (NoSuchEntityException $e) {
throw new KlarnaException(__(
'No Kustom order entry could be found with the provided Kustom order id: %1',
$this->klarnaOrderId
));
}
}
return $this->klarnaOrder;
}
/**
* Getting back the Magento order
*
* @throws KlarnaException
* @return MagentoOrder
*/
public function getMagentoOrder(): MagentoOrder
{
if ($this->magentoOrder === null) {
try {
$this->magentoOrder = $this->magentoOrderRepository->get($this->getKlarnaOrder()->getOrderId());
} catch (NoSuchEntityException|InputException $e) {
throw new KlarnaException(__(
'No Magento order could be found with the provided Kustom order id: %1',
$this->klarnaOrderId
));
}
}
return $this->magentoOrder;
}
/**
* Clearing the klarna order instance so that later a new instance can be requested from the database.
*/
public function clearKlarnaOrder(): void
{
$this->klarnaOrder = null;
}
}