1 | <?php |
||
2 | |||
3 | namespace Pagantis\OrdersApiClient\Method; |
||
4 | |||
5 | use Httpful\Http; |
||
6 | use Httpful\Mime; |
||
7 | use Httpful\Request; |
||
8 | use Httpful\Response; |
||
9 | use Pagantis\OrdersApiClient\Exception\ClientException; |
||
10 | use Pagantis\OrdersApiClient\Model\Order; |
||
11 | |||
12 | /** |
||
13 | * Class CreateOrderMethod |
||
14 | * |
||
15 | * @package Pagantis\OrdersApiClient\Method |
||
16 | */ |
||
17 | class CreateOrderMethod extends AbstractMethod |
||
18 | { |
||
19 | /** |
||
20 | * Get Order Endpoint |
||
21 | */ |
||
22 | const ENDPOINT = '/orders'; |
||
23 | |||
24 | /** |
||
25 | * @var Order |
||
26 | */ |
||
27 | protected $order; |
||
28 | |||
29 | /** |
||
30 | * @param Order $order |
||
31 | * |
||
32 | * @return $this |
||
33 | */ |
||
34 | public function setOrder(Order $order) |
||
35 | { |
||
36 | $this->order = $order; |
||
37 | |||
38 | return $this; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @return $this|AbstractMethod |
||
43 | * @throws \Httpful\Exception\ConnectionErrorException |
||
44 | * @throws ClientException |
||
45 | * @throws \Pagantis\OrdersApiClient\Exception\HttpException |
||
46 | */ |
||
47 | public function call() |
||
48 | { |
||
49 | if ($this->order instanceof Order) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
50 | $this->prepareRequest(); |
||
51 | return $this->setResponse($this->request->send()); |
||
52 | } |
||
53 | throw new ClientException('Please Set Order'); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @return bool|Order |
||
58 | * |
||
59 | * @throws \Exception |
||
60 | */ |
||
61 | public function getOrder() |
||
62 | { |
||
63 | $response = $this->getResponse(); |
||
64 | if ($response instanceof Response) { |
||
0 ignored issues
–
show
|
|||
65 | $order = new Order(); |
||
66 | $order->import($this->getResponse()->body); |
||
67 | return $order; |
||
68 | } |
||
69 | |||
70 | return false; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * prepareRequest |
||
75 | * |
||
76 | */ |
||
77 | protected function prepareRequest() |
||
78 | { |
||
79 | if (!$this->request instanceof Request) { |
||
0 ignored issues
–
show
|
|||
80 | $this->request = $this->getRequest() |
||
81 | ->method(Http::POST) |
||
82 | ->uri( |
||
83 | $this->apiConfiguration->getBaseUri() |
||
84 | . self::ENDPOINT |
||
85 | ) |
||
86 | ->sendsType(Mime::JSON) |
||
87 | ->body(json_encode($this->order->export())) |
||
88 | ; |
||
89 | } |
||
90 | } |
||
91 | } |
||
92 |