1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lifeboat\Services; |
4
|
|
|
|
5
|
|
|
use Lifeboat\Exceptions\ApiException; |
6
|
|
|
use Lifeboat\Exceptions\InvalidArgumentException; |
7
|
|
|
use Lifeboat\Exceptions\OAuthException; |
8
|
|
|
use Lifeboat\Models\Order; |
9
|
|
|
use Lifeboat\Resource\ListResource; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Orders |
13
|
|
|
* @package Lifeboat\Services |
14
|
|
|
*/ |
15
|
|
|
class Orders extends ApiService { |
16
|
|
|
|
17
|
|
|
const PERIOD_ALL = 'all'; |
18
|
|
|
const PERIOD_DAY = '1'; |
19
|
|
|
const PERIOD_7 = '7'; |
20
|
|
|
const PERIOD_30 = '30'; |
21
|
|
|
const PERIOD_90 = '90'; |
22
|
|
|
const PERIOD_120 = '120'; |
23
|
|
|
const VALID_PERIODS = [ |
24
|
|
|
self::PERIOD_ALL, self::PERIOD_DAY, self::PERIOD_7, |
25
|
|
|
self::PERIOD_30, self::PERIOD_90, self::PERIOD_120 |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
const STATUS_OPEN = 1; |
29
|
|
|
const STATUS_PAID = 2; |
30
|
|
|
const VALID_STATUSES = [ |
31
|
|
|
self::STATUS_OPEN, self::STATUS_PAID |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
const FULFILLMENT_PENDING = 1; |
35
|
|
|
const FULFILLMENT_FULFILLED = 2; |
36
|
|
|
const FULFILLMENT_DELIVERED = 3; |
37
|
|
|
const VALID_FULFILLMENT_STATUSES = [ |
38
|
|
|
self::FULFILLMENT_PENDING, self::FULFILLMENT_FULFILLED, self::FULFILLMENT_DELIVERED |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
const FULFILLMENT_SHIP = 0; |
42
|
|
|
const FULFILLMENT_DELIVER = 1; |
43
|
|
|
const FULFILLMENT_PICKUP = 2; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param int $id |
47
|
|
|
* @return Order|null |
48
|
|
|
* @throws ApiException |
49
|
|
|
* @throws OAuthException |
50
|
|
|
*/ |
51
|
|
|
public function fetch(int $id): ?Order |
52
|
|
|
{ |
53
|
|
|
/** @var Order|null $fetch */ |
54
|
|
|
$fetch = $this->_get('api/orders/order/' . $id); |
55
|
|
|
return $fetch; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param array $data |
60
|
|
|
* @return Order|null |
61
|
|
|
* @throws ApiException |
62
|
|
|
* @throws OAuthException |
63
|
|
|
*/ |
64
|
|
|
public function create(array $data): ?Order |
65
|
|
|
{ |
66
|
|
|
/** @var Order|null $create */ |
67
|
|
|
$create = $this->_post('api/orders/order', $data); |
68
|
|
|
return $create; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param int $id |
73
|
|
|
* @param array $data |
74
|
|
|
* @return Order|null |
75
|
|
|
* @throws ApiException |
76
|
|
|
* @throws OAuthException |
77
|
|
|
*/ |
78
|
|
|
public function update(int $id, array $data): ?Order |
79
|
|
|
{ |
80
|
|
|
/** @var Order|null $post */ |
81
|
|
|
$post = $this->_post('api/orders/order/' . $id, $data); |
82
|
|
|
return $post; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $period |
87
|
|
|
* @param int $status |
88
|
|
|
* @param int $fulfillment |
89
|
|
|
* @param string $search |
90
|
|
|
* @param int $page_length |
91
|
|
|
* @param string $url |
92
|
|
|
* @return ListResource |
93
|
|
|
*/ |
94
|
|
|
public function all( |
95
|
|
|
string $period = self::PERIOD_7, |
96
|
|
|
int $status = self::STATUS_PAID, |
97
|
|
|
int $fulfillment = self::FULFILLMENT_PENDING, |
98
|
|
|
string $search = '', |
99
|
|
|
int $page_length = 20, |
100
|
|
|
string $url = 'api/orders/all' |
101
|
|
|
): ListResource { |
102
|
|
|
$data = $this->parse_filters($period, $status, $fulfillment, $search); |
103
|
|
|
return new ListResource($this->getClient(), $url, $data, $page_length); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $period |
108
|
|
|
* @param int $fulfillment |
109
|
|
|
* @param string $search |
110
|
|
|
* @param int $page_length |
111
|
|
|
* @return ListResource |
112
|
|
|
*/ |
113
|
|
|
public function deliveries( |
114
|
|
|
string $period = self::PERIOD_7, |
115
|
|
|
int $fulfillment = self::FULFILLMENT_PENDING, |
116
|
|
|
string $search = '', |
117
|
|
|
int $page_length = 20 |
118
|
|
|
): ListResource { |
119
|
|
|
return $this->all($period, self::STATUS_PAID, $fulfillment, $search, $page_length, 'api/orders/delivery'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $period |
124
|
|
|
* @param int $fulfillment |
125
|
|
|
* @param string $search |
126
|
|
|
* @param int $page_length |
127
|
|
|
* @return ListResource |
128
|
|
|
*/ |
129
|
|
|
public function pickups( |
130
|
|
|
string $period = self::PERIOD_7, |
131
|
|
|
int $fulfillment = self::FULFILLMENT_PENDING, |
132
|
|
|
string $search = '', |
133
|
|
|
int $page_length = 20 |
134
|
|
|
): ListResource |
135
|
|
|
{ |
136
|
|
|
return $this->all($period, self::STATUS_PAID, $fulfillment, $search, $page_length, 'api/orders/pickup'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $period |
141
|
|
|
* @param int $status |
142
|
|
|
* @param int $fulfillment |
143
|
|
|
* @param string $search |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
|
|
private function parse_filters( |
147
|
|
|
string $period = self::PERIOD_7, |
148
|
|
|
int $status = self::STATUS_PAID, |
149
|
|
|
int $fulfillment = self::FULFILLMENT_PENDING, |
150
|
|
|
string $search = '' |
151
|
|
|
): array { |
152
|
|
|
if (!in_array($period, self::VALID_PERIODS)) { |
153
|
|
|
throw new InvalidArgumentException("Orders::all expects parameter 1 to be a valid period"); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if (!in_array($status, self::VALID_STATUSES)) { |
157
|
|
|
throw new InvalidArgumentException("Orders::all expects parameter 2 to be a valid status"); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if (!in_array($fulfillment, self::VALID_FULFILLMENT_STATUSES)) { |
161
|
|
|
throw new InvalidArgumentException("Orders::all expects parameter 3 to be a valid fulfillment status"); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return [ |
165
|
|
|
'period' => $period, |
166
|
|
|
'status' => $status, |
167
|
|
|
'fulfillment' => $fulfillment, |
168
|
|
|
'search' => $search |
169
|
|
|
]; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|