Test Failed
Pull Request — develop (#678)
by Jens
20:34
created

OrderCreateFromCartRequest::setState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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
    protected $cart;
48
    protected $version;
49
    protected $orderNumber;
50
    protected $paymentState;
51
    protected $orderState;
52
    protected $state;
53
    protected $shipmentState;
54 109
55
    protected $resultClass = Order::class;
56 109
57
    /**
58
     * @param CartReference|string $cart
59
     * @param int $version
60
     * @param Context $context
61
     */
62
    public function __construct($cart, $version = null, Context $context = null)
63 112
    {
64
        parent::__construct(OrdersEndpoint::endpoint(), $context);
65 112
66
        if ($cart instanceof CartReference) {
67 112
            $this->setCart($cart)->setVersion($version);
68
        } else {
69
            $this->setCart(CartReference::ofId($cart))->setVersion($version);
70
        }
71
    }
72
73 109
    /**
74
     * @return string
75 109
     */
76
    public function getCart()
77
    {
78
        return $this->cart;
79
    }
80
81
    /**
82 112
     * @param CartReference $cart
83
     * @return $this
84 112
     */
85
    public function setCart($cart)
86 112
    {
87
        $this->cart = $cart;
88
89
        return $this;
90
    }
91
92 23
    /**
93
     * @deprecated use getCart instead
94 23
     * @return string
95
     */
96
    public function getCartId()
97
    {
98
        return $this->cartId;
0 ignored issues
show
Deprecated Code introduced by
The property Commercetools\Core\Reque...romCartRequest::$cartId has been deprecated: use $cart instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

98
        return /** @scrutinizer ignore-deprecated */ $this->cartId;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
99
    }
100
101 23
    /**
102
     * @deprecated use setCart instead
103 23
     * @param string $cartId
104
     * @return $this
105 23
     */
106
    public function setCartId($cartId)
107
    {
108
        $this->cartId = $cartId;
0 ignored issues
show
Deprecated Code introduced by
The property Commercetools\Core\Reque...romCartRequest::$cartId has been deprecated: use $cart instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

108
        /** @scrutinizer ignore-deprecated */ $this->cartId = $cartId;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
109
110
        return $this;
111 3
    }
112
113 3
    /**
114
     * @return int
115
     */
116
    public function getVersion()
117
    {
118
        return $this->version;
119
    }
120 3
121
    /**
122 3
     * @param int $version
123
     * @return $this
124 3
     */
125
    public function setVersion($version)
126
    {
127
        $this->version = $version;
128
129
        return $this;
130 3
    }
131
132 3
    /**
133
     * @return string
134
     */
135
    public function getOrderNumber()
136
    {
137
        return $this->orderNumber;
138
    }
139 3
140
    /**
141 3
     * @param string $orderNumber
142
     * @return $this
143 3
     */
144
    public function setOrderNumber($orderNumber)
145
    {
146
        $this->orderNumber = $orderNumber;
147
148
        return $this;
149 3
    }
150
151 3
    /**
152
     * @return string
153
     */
154
    public function getPaymentState()
155
    {
156
        return $this->paymentState;
157
    }
158 3
159
    /**
160 3
     * @param string $paymentState
161
     * @return $this
162 3
     */
163
    public function setPaymentState($paymentState)
164
    {
165
        $this->paymentState = $paymentState;
166
167
        return $this;
168 3
    }
169
170 3
    /**
171
     * @return string
172
     */
173
    public function getOrderState()
174
    {
175
        return $this->orderState;
176
    }
177 3
178
    /**
179 3
     * @param string $orderState
180
     * @return $this
181 3
     */
182
    public function setOrderState($orderState)
183
    {
184
        $this->orderState = $orderState;
185
186
        return $this;
187
    }
188
189 112
    /**
190
     * @return StateReference
191 112
     */
192 112
    public function getState()
193 112
    {
194
        return $this->state;
195
    }
196
197
    /**
198
     * @param StateReference $state
199
     * @return $this
200
     */
201 112
    public function setState(StateReference $state)
202
    {
203 112
        $this->state = $state;
204
205
        return $this;
206
    }
207
208
    /**
209
     * @return string
210
     */
211 107
    public function getShipmentState()
212
    {
213 107
        return $this->shipmentState;
214
    }
215
216
    /**
217
     * @param string $shipmentState
218
     * @return $this
219
     */
220 109
    public function setShipmentState($shipmentState)
221
    {
222
        $this->shipmentState = $shipmentState;
223 109
224 109
        return $this;
225
    }
226 109
227 3
    /**
228
     * @deprecated use ofCartAndVersion instead
229 109
     * @param string $cartId
230 23
     * @param int $version
231
     * @param Context $context
232 109
     * @return static
233 3
     */
234
    public static function ofCartIdAndVersion($cartId, $version, Context $context = null)
235 109
    {
236 3
        return new static($cartId, $version, $context);
237
    }
238 109
239 3
    /**
240
     * @param CartReference|string $cart
241 109
     * @param int $version
242
     * @param Context $context
243
     * @return static
244
     */
245
    public static function ofCartAndVersion($cart, $version, Context $context = null)
246
    {
247
        return new static($cart, $version, $context);
248
    }
249
250
    /**
251
     * @param ResponseInterface $response
252
     * @return AbstractApiResponse
253
     * @internal
254
     */
255
    public function buildResponse(ResponseInterface $response)
256
    {
257
        return new ResourceResponse($response, $this, $this->getContext());
258
    }
259
260
    /**
261
     * @return JsonRequest
262
     * @internal
263
     */
264
    public function httpRequest()
265
    {
266
        $payload = [
267
            static::ID => $this->getCart()->getId(),
268
            static::CART => $this->getCart(),
269
            static::VERSION => $this->getVersion(),
270
        ];
271
        if (!is_null($this->paymentState)) {
272
            $payload[static::PAYMENT_STATE] = $this->getPaymentState();
273
        }
274
        if (!is_null($this->orderNumber)) {
275
            $payload[static::ORDER_NUMBER] = $this->getOrderNumber();
276
        }
277
        if (!is_null($this->orderState)) {
278
            $payload[static::ORDER_STATE] = $this->getOrderState();
279
        }
280
        if (!is_null($this->state)) {
281
            $payload[static::STATE] = $this->getState();
282
        }
283
        if (!is_null($this->shipmentState)) {
284
            $payload[static::SHIPMENT_STATE] = $this->getShipmentState();
285
        }
286
        return new JsonRequest(HttpMethod::POST, $this->getPath(), $payload);
287
    }
288
}
289