ListOrdersMethod::setQueryParameters()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Pagantis\OrdersApiClient\Method;
4
5
use Httpful\Http;
6
use Httpful\Request;
7
use Httpful\Response;
8
use Pagantis\OrdersApiClient\Model\Order;
9
10
/**
11
 * Class ListOrdersMethod
12
 *
13
 * @package Pagantis\OrdersApiClient\Method
14
 */
15
class ListOrdersMethod extends AbstractMethod
16
{
17
    /**
18
     * Get Order Endpoint
19
     */
20
    const ENDPOINT = '/orders';
21
22
    /**
23
     * @var array $queryParameters
24
     */
25
    protected $queryParameters;
26
27
    /**
28
     * @param array $queryParameters
29
     *
30
     * @return $this
31
     */
32
    public function setQueryParameters(array $queryParameters)
33
    {
34
        $this->queryParameters = $queryParameters;
35
36
        return $this;
37
    }
38
39
    /**
40
     * @return array|bool
41
     *
42
     * @throws \Exception
43
     */
44
    public function getOrders()
45
    {
46
        $response = $this->getResponse();
47
        if ($response instanceof Response) {
0 ignored issues
show
introduced by
$response is always a sub-type of Httpful\Response.
Loading history...
48
            $responseBody = $response->body;
49
            $orders = array();
50
            foreach ($responseBody as $responseOrder) {
51
                $order = new Order();
52
                $order->import($responseOrder);
53
                $orders[] = $order;
54
            }
55
56
            return $orders;
57
        }
58
59
        return false;
60
    }
61
62
    /**
63
     * @return $this|AbstractMethod
64
     * @throws \Httpful\Exception\ConnectionErrorException
65
     * @throws \Pagantis\OrdersApiClient\Exception\HttpException
66
     */
67
    public function call()
68
    {
69
        $this->prepareRequest();
70
        return $this->setResponse($this->request->send());
71
    }
72
73
    /**
74
     * prepareRequest
75
     */
76
    public function prepareRequest()
77
    {
78
        if (!$this->request instanceof Request) {
0 ignored issues
show
introduced by
$this->request is always a sub-type of Httpful\Request.
Loading history...
79
            $this->request = $this->getRequest()
80
                ->method(Http::GET)
81
                ->uri(
82
                    $this->apiConfiguration->getBaseUri()
83
                    . self::ENDPOINT
84
                    . $this->addGetParameters($this->queryParameters)
85
                )
86
            ;
87
        }
88
    }
89
}
90