Completed
Push — master ( 6e6d73...1dcfbd )
by Cesar
14s queued 11s
created

Client   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 135
rs 10
c 0
b 0
f 0
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 3
A refundOrder() 0 10 2
A createOrder() 0 9 2
A getOrder() 0 9 2
A confirmOrder() 0 9 2
A listOrders() 0 9 2
1
<?php
2
3
namespace Pagantis\OrdersApiClient;
4
5
use Httpful\Exception\ConnectionErrorException;
6
use Pagantis\OrdersApiClient\Method\ConfirmOrderMethod;
7
use Pagantis\OrdersApiClient\Method\CreateOrderMethod;
8
use Pagantis\OrdersApiClient\Method\GetOrderMethod;
9
use Pagantis\OrdersApiClient\Method\ListOrdersMethod;
10
use Pagantis\OrdersApiClient\Method\RefundOrderMethod;
11
use Pagantis\OrdersApiClient\Model\ApiConfiguration;
12
use Pagantis\OrdersApiClient\Model\Order;
13
14
/**
15
 * Class Client
16
 *
17
 * @package Pagantis/OrdersApiClient
18
 */
19
class Client
20
{
21
    /**
22
     * @var ApiConfiguration
23
     */
24
    protected $apiConfiguration;
25
26
    /**
27
     * Client constructor.
28
     *
29
     * @param       $publicKey
30
     * @param       $privateKey
31
     * @param null  $baseUri
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $baseUri is correct as it would always require null to be passed?
Loading history...
32
     * @param array $headers
33
     *
34
     * @throws ConnectionErrorException
35
     * @throws Exception\ClientException
36
     */
37
    public function __construct($publicKey, $privateKey, $baseUri = null, $headers = array())
38
    {
39
        if (!function_exists("curl_init")) {
40
            throw new ConnectionErrorException("Curl module is not available on this system");
41
        }
42
43
        $apiConfiguration = new ApiConfiguration();
44
        $apiConfiguration
45
            ->setBaseUri($baseUri ? $baseUri : ApiConfiguration::BASE_URI)
0 ignored issues
show
introduced by
$baseUri is of type null, thus it always evaluated to false.
Loading history...
46
            ->setPrivateKey($privateKey)
47
            ->setPublicKey($publicKey)
48
            ->setHeaders($headers)
49
        ;
50
        $this->apiConfiguration = $apiConfiguration;
51
    }
52
53
    /**
54
     * @param Order $order
55
     * @param bool  $asJson
56
     *
57
     * @return bool|false|Order|string
58
     * @throws ConnectionErrorException
59
     * @throws Exception\HttpException
60
     * @throws Exception\ClientException
61
     */
62
    public function createOrder(Order $order, $asJson = false)
63
    {
64
        $createOrderMethod = new CreateOrderMethod($this->apiConfiguration);
65
        $createOrderMethod->setOrder($order);
66
        if ($asJson) {
67
            return $createOrderMethod->call()->getResponseAsJson();
68
        }
69
70
        return $createOrderMethod->call()->getOrder();
71
    }
72
73
    /**
74
     * @param      $orderId
75
     * @param bool $asJson
76
     *
77
     * @return bool|false|Order|string
78
     * @throws ConnectionErrorException
79
     * @throws Exception\HttpException
80
     * @throws Exception\ClientException
81
     */
82
    public function getOrder($orderId, $asJson = false)
83
    {
84
        $getOrderMethod = new GetOrderMethod($this->apiConfiguration);
85
        $getOrderMethod->setOrderId($orderId);
86
        if ($asJson) {
87
            return $getOrderMethod->call()->getResponseAsJson();
88
        }
89
90
        return $getOrderMethod->call()->getOrder();
91
    }
92
93
    /**
94
     * listOrders
95
     *
96
     * @param array|null $queryString
97
     * @param bool       $asJson
98
     *
99
     * @return array|bool|string
100
     * @throws ConnectionErrorException
101
     * @throws Exception\HttpException
102
     */
103
    public function listOrders(array $queryString = null, $asJson = false)
104
    {
105
        $listOrdersMethod = new ListOrdersMethod($this->apiConfiguration);
106
        $listOrdersMethod->setQueryParameters($queryString);
0 ignored issues
show
Bug introduced by
It seems like $queryString can also be of type null; however, parameter $queryParameters of Pagantis\OrdersApiClient...d::setQueryParameters() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

106
        $listOrdersMethod->setQueryParameters(/** @scrutinizer ignore-type */ $queryString);
Loading history...
107
        if ($asJson) {
108
            return $listOrdersMethod->call()->getResponseAsJson();
109
        }
110
111
        return $listOrdersMethod->call()->getOrders();
112
    }
113
114
    /**
115
     * @param      $orderId
116
     * @param bool $asJson
117
     *
118
     * @return bool|false|Order|string
119
     * @throws ConnectionErrorException
120
     * @throws Exception\HttpException
121
     * @throws Exception\ClientException
122
     */
123
    public function confirmOrder($orderId, $asJson = false)
124
    {
125
        $confirmOrderMethod = new ConfirmOrderMethod($this->apiConfiguration);
126
        $confirmOrderMethod->setOrderId($orderId);
127
        if ($asJson) {
128
            return $confirmOrderMethod->call()->getResponseAsJson();
129
        }
130
131
        return $confirmOrderMethod->call()->getOrder();
132
    }
133
134
    /**
135
     * @param              $orderId
136
     * @param Order\Refund $refund
137
     * @param bool         $asJson
138
     *
139
     * @return bool|false|Order\Refund|string
140
     * @throws ConnectionErrorException
141
     * @throws Exception\HttpException
142
     * @throws Exception\ClientException
143
     */
144
    public function refundOrder($orderId, Order\Refund $refund, $asJson = false)
145
    {
146
        $refundOrderMethod = new RefundOrderMethod($this->apiConfiguration);
147
        $refundOrderMethod->setOrderId($orderId);
148
        $refundOrderMethod->setRefund($refund);
149
        if ($asJson) {
150
            return $refundOrderMethod->call()->getResponseAsJson();
151
        }
152
153
        return $refundOrderMethod->call()->getRefund();
154
    }
155
}
156