Orders::getUrl()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * Copyright (c) 2017 Salah Alkhwlani <[email protected]>
5
 *
6
 * For the full copyright and license information, please view
7
 * the LICENSE file that was distributed with this source code.
8
 */
9
10
namespace Yemenifree\PickServices\Services;
11
12
use Yemenifree\PickServices\ApiClient;
13
use Yemenifree\PickServices\Model\Order;
14
use Zttp\ZttpResponse;
15
16
class Orders
17
{
18
    /**
19
     * @var ApiClient
20
     */
21
    protected $client;
22
    /**
23
     * @var int
24
     */
25
    private $merchantId;
26
27
    /**
28
     * Orders constructor.
29
     *
30
     * @param ApiClient $client
31
     * @param int       $merchantId
32
     */
33
    public function __construct(ApiClient $client, int $merchantId = 0)
34
    {
35
        $this->client = $client;
36
        $this->merchantId = $merchantId;
37
    }
38
39
    /**
40
     * Returns order with requested id.
41
     *
42
     * @param int $orderID
43
     *
44
     * @return ZttpResponse
45
     */
46
    public function view(int $orderID): ZttpResponse
47
    {
48
        return $this->client->get($this->getUrl('orders/' . $orderID . '/'));
49
    }
50
51
    /**
52
     * get url base merchant id or normal request.
53
     *
54
     * @param $request
55
     *
56
     * @return string
57
     */
58
    protected function getUrl($request): string
59
    {
60
        return $this->merchantId ? $this->merchantId . '/' . $request : $request;
61
    }
62
63
    /**
64
     * Returns order with requested id.
65
     *
66
     * @param int $orderID
67
     *
68
     * @return ZttpResponse
69
     */
70
    public function pdf(int $orderID): ZttpResponse
71
    {
72
        return $this->client->get($this->getUrl('orders/' . $orderID . '/airway_bill/'));
73
    }
74
75
    /**
76
     * Get orders.
77
     *
78
     * @param string $search   A search term.
79
     * @param string $ordering Which field to use when ordering the results.
80
     *
81
     * @return \Zttp\ZttpResponse
82
     */
83
    public function search(string $search = null, string $ordering = null): ZttpResponse
84
    {
85
        return $this->client->get($this->getUrl('orders/'), \compact('search', 'ordering'));
86
    }
87
88
    /**
89
     * create new order.
90
     *
91
     * @param Order $order
92
     *
93
     * @return \Zttp\ZttpResponse
94
     */
95
    public function create(Order $order): ZttpResponse
96
    {
97
        return $this->client->post($this->getUrl('orders/'), $order->getModel());
98
    }
99
100
    /**
101
     * create new order.
102
     *
103
     * @param int   $orderId
104
     * @param Order $order
105
     *
106
     * @return ZttpResponse
107
     */
108
    public function edit(int $orderId, Order $order): ZttpResponse
109
    {
110
        return $this->client->put($this->getUrl('orders/' . $orderId . '/'), $order->getModel());
111
    }
112
113
    /**
114
     * cancel order.
115
     *
116
     * @param int   $orderId
117
     * @param Order $order
118
     *
119
     * @return ZttpResponse
120
     */
121
    public function cancel(int $orderId, Order $order): ZttpResponse
122
    {
123
        return $this->client->post($this->getUrl('orders/' . $orderId . '/cancel/'), $order->getModel());
124
    }
125
126
    /**
127
     * Cancels an Order.
128
     *
129
     * @param int $orderId
130
     *
131
     * @return \Zttp\ZttpResponse
132
     */
133
    public function delete(int $orderId): ZttpResponse
134
    {
135
        return $this->client->delete($this->getUrl('orders/' . $orderId . '/'));
136
    }
137
}
138