CreateOrderMethod   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setOrder() 0 5 1
A call() 0 7 2
A getOrder() 0 10 2
A prepareRequest() 0 11 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
     * @throws \Exception
60
     */
61
    public function getOrder()
62
    {
63
        $response = $this->getResponse();
64
        if ($response instanceof Response) {
0 ignored issues
show
introduced by
$response is always a sub-type of Httpful\Response.
Loading history...
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
introduced by
$this->request is always a sub-type of Httpful\Request.
Loading history...
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