OrderCreateFromCartRequest::httpRequest()   A
last analyzed

Complexity

Conditions 6
Paths 32

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 22
ccs 0
cts 0
cp 0
rs 9.2222
c 0
b 0
f 0
cc 6
nc 32
nop 0
crap 42
1
<?php
2
/**
3
 * @author @jenschude <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Request\Orders;
7
8
use Commercetools\Core\Model\Cart\CartReference;
9
use Commercetools\Core\Model\State\StateReference;
10
use Commercetools\Core\Request\InStores\InStoreRequestDecorator;
11
use Commercetools\Core\Request\InStores\InStoreTrait;
12
use Psr\Http\Message\ResponseInterface;
13
use Commercetools\Core\Client\HttpMethod;
14
use Commercetools\Core\Client\JsonRequest;
15
use Commercetools\Core\Model\Common\Context;
16
use Commercetools\Core\Request\AbstractApiRequest;
17
use Commercetools\Core\Response\AbstractApiResponse;
18
use Commercetools\Core\Response\ResourceResponse;
19
use Commercetools\Core\Model\Order\Order;
20
use Commercetools\Core\Response\ApiResponseInterface;
21
use Commercetools\Core\Model\MapperInterface;
22
23
/**
24
 * @package Commercetools\Core\Request\Orders
25
 * @link https://docs.commercetools.com/http-api-projects-orders.html#create-order-from-cart
26
 * @method Order mapResponse(ApiResponseInterface $response)
27
 * @method Order mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
28
 * @method OrderCreateFromCartRequest|InStoreRequestDecorator inStore($storeKey)
29
 */
30
class OrderCreateFromCartRequest extends AbstractApiRequest
31
{
32
    use InStoreTrait;
33
34
    const ID = 'id';
35
    const CART = 'cart';
36
    const VERSION = 'version';
37
    const ORDER_NUMBER = 'orderNumber';
38
    const PAYMENT_STATE = 'paymentState';
39
    const ORDER_STATE = 'orderState';
40
    const STATE = 'state';
41
    const SHIPMENT_STATE = 'shipmentState';
42
43
    /**
44
     * @deprecated use $cart instead
45
     */
46
    protected $cartId;
47
    /**
48
     * @var CartReference
49
     */
50
    protected $cart;
51
    protected $version;
52
    protected $orderNumber;
53
    protected $paymentState;
54 109
    protected $orderState;
55
    protected $state;
56 109
    protected $shipmentState;
57
58
    protected $resultClass = Order::class;
59
60
    /**
61
     * @param CartReference|string $cart
62
     * @param int $version
63 112
     * @param Context $context
64
     */
65 112
    public function __construct($cart, $version = null, Context $context = null)
66
    {
67 112
        parent::__construct(OrdersEndpoint::endpoint(), $context);
68
69
        if ($cart instanceof CartReference) {
70
            $this->setCart($cart)->setVersion($version);
71
        } else {
72
            $this->setCart(CartReference::ofId($cart))->setVersion($version);
73 109
        }
74
    }
75 109
76
    /**
77
     * @return string
78
     */
79
    public function getCart()
80
    {
81
        return $this->cart;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->cart returns the type Commercetools\Core\Model\Cart\CartReference which is incompatible with the documented return type string.
Loading history...
82 112
    }
83
84 112
    /**
85
     * @param CartReference $cart
86 112
     * @return $this
87
     */
88
    public function setCart($cart)
89
    {
90
        $this->cart = $cart;
91
92 23
        return $this;
93
    }
94 23
95
    /**
96
     * @deprecated use getCart instead
97
     * @return string
98
     */
99
    public function getCartId()
100
    {
101 23
        if ($this->cart == null) {
102
            return null;
103 23
        }
104
        return $this->cart->getId();
105 23
    }
106
107
    /**
108
     * @deprecated use setCart instead
109
     * @param string $cartId
110
     * @return $this
111 3
     */
112
    public function setCartId($cartId)
113 3
    {
114
        $this->cart = CartReference::ofId($cartId);
115
116
        return $this;
117
    }
118
119
    /**
120 3
     * @return int
121
     */
122 3
    public function getVersion()
123
    {
124 3
        return $this->version;
125
    }
126
127
    /**
128
     * @param int $version
129
     * @return $this
130 3
     */
131
    public function setVersion($version)
132 3
    {
133
        $this->version = $version;
134
135
        return $this;
136
    }
137
138
    /**
139 3
     * @return string
140
     */
141 3
    public function getOrderNumber()
142
    {
143 3
        return $this->orderNumber;
144
    }
145
146
    /**
147
     * @param string $orderNumber
148
     * @return $this
149 3
     */
150
    public function setOrderNumber($orderNumber)
151 3
    {
152
        $this->orderNumber = $orderNumber;
153
154
        return $this;
155
    }
156
157
    /**
158 3
     * @return string
159
     */
160 3
    public function getPaymentState()
161
    {
162 3
        return $this->paymentState;
163
    }
164
165
    /**
166
     * @param string $paymentState
167
     * @return $this
168 3
     */
169
    public function setPaymentState($paymentState)
170 3
    {
171
        $this->paymentState = $paymentState;
172
173
        return $this;
174
    }
175
176
    /**
177 3
     * @return string
178
     */
179 3
    public function getOrderState()
180
    {
181 3
        return $this->orderState;
182
    }
183
184
    /**
185
     * @param string $orderState
186
     * @return $this
187
     */
188
    public function setOrderState($orderState)
189 112
    {
190
        $this->orderState = $orderState;
191 112
192 112
        return $this;
193 112
    }
194
195
    /**
196
     * @return StateReference
197
     */
198
    public function getState()
199
    {
200
        return $this->state;
201 112
    }
202
203 112
    /**
204
     * @param StateReference $state
205
     * @return $this
206
     */
207
    public function setState(StateReference $state)
208
    {
209
        $this->state = $state;
210
211 107
        return $this;
212
    }
213 107
214
    /**
215
     * @return string
216
     */
217
    public function getShipmentState()
218
    {
219
        return $this->shipmentState;
220 109
    }
221
222
    /**
223 109
     * @param string $shipmentState
224 109
     * @return $this
225
     */
226 109
    public function setShipmentState($shipmentState)
227 3
    {
228
        $this->shipmentState = $shipmentState;
229 109
230 23
        return $this;
231
    }
232 109
233 3
    /**
234
     * @deprecated use ofCartAndVersion instead
235 109
     * @param string $cartId
236 3
     * @param int $version
237
     * @param Context $context
238 109
     * @return static
239 3
     */
240
    public static function ofCartIdAndVersion($cartId, $version, Context $context = null)
241 109
    {
242
        return new static($cartId, $version, $context);
243
    }
244
245
    /**
246
     * @param CartReference|string $cart
247
     * @param int $version
248
     * @param Context $context
249
     * @return static
250
     */
251
    public static function ofCartAndVersion($cart, $version, Context $context = null)
252
    {
253
        return new static($cart, $version, $context);
254
    }
255
256
    /**
257
     * @param ResponseInterface $response
258
     * @return AbstractApiResponse
259
     * @internal
260
     */
261
    public function buildResponse(ResponseInterface $response)
262
    {
263
        return new ResourceResponse($response, $this, $this->getContext());
264
    }
265
266
    /**
267
     * @return JsonRequest
268
     * @internal
269
     */
270
    public function httpRequest()
271
    {
272
        $payload = [
273
            static::CART => $this->getCart(),
274
            static::VERSION => $this->getVersion(),
275
        ];
276
        if (!is_null($this->paymentState)) {
277
            $payload[static::PAYMENT_STATE] = $this->getPaymentState();
278
        }
279
        if (!is_null($this->orderNumber)) {
280
            $payload[static::ORDER_NUMBER] = $this->getOrderNumber();
281
        }
282
        if (!is_null($this->orderState)) {
283
            $payload[static::ORDER_STATE] = $this->getOrderState();
284
        }
285
        if (!is_null($this->state)) {
286
            $payload[static::STATE] = $this->getState();
287
        }
288
        if (!is_null($this->shipmentState)) {
289
            $payload[static::SHIPMENT_STATE] = $this->getShipmentState();
290
        }
291
        return new JsonRequest(HttpMethod::POST, $this->getPath(), $payload);
292
    }
293
}
294