Passed
Pull Request — master (#17)
by Cesar
02:31
created

CreateOrderMethod   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setOrder() 0 5 1
A getOrder() 0 10 2
A prepareRequest() 0 11 2
A call() 0 7 2
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
$this->order is always a sub-type of Pagantis\OrdersApiClient\Model\Order.
Loading history...
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
    public function getOrder()
60
    {
61
        $response = $this->getResponse();
62
        if ($response instanceof Response) {
0 ignored issues
show
introduced by
$response is always a sub-type of Httpful\Response.
Loading history...
63
            $order = new Order();
64
            $order->import($this->getResponse()->body);
65
            return $order;
66
        }
67
68
        return false;
69
    }
70
71
    /**
72
     * prepareRequest
73
     *
74
     */
75
    protected function prepareRequest()
76
    {
77
        if (!$this->request instanceof Request) {
0 ignored issues
show
introduced by
$this->request is always a sub-type of Httpful\Request.
Loading history...
78
            $this->request = $this->getRequest()
79
                ->method(Http::POST)
80
                ->uri(
81
                    $this->apiConfiguration->getBaseUri()
82
                    . self::ENDPOINT
83
                )
84
                ->sendsType(Mime::JSON)
85
                ->body(json_encode($this->order->export()))
86
            ;
87
        }
88
    }
89
}
90