Completed
Push — master ( 23b0b8...d25f9a )
by Joachim
12:29
created

Discount::countPackageDiscounts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace Loevgaard\Dandomain\Api\Endpoint;
3
4
use Assert\Assert;
5
6
/**
7
 * @see http://4221117.shop53.dandomain.dk/admin/webapi/endpoints/v1_0/DiscountService/help
8
 */
9
class Discount extends Endpoint
10
{
11
    const TYPE_FREE_SHIPPING = 'freeShippingSalesDiscount';
12
    const TYPE_GENERAL = 'generalSalesDiscount';
13
    const TYPE_GROUP = 'groupSalesDiscount';
14
    const TYPE_PACKAGE = 'packageSalesDiscount';
15
    const TYPE_QUANTITY = 'quantitySalesDiscount';
16
17
    /***************************
18
     * Free shipping discounts *
19
     **************************/
20
21
    /**
22
     * @param array|\stdClass $obj
23
     * @return array
24
     */
25
    public function createFreeShippingDiscount($obj) : array
26
    {
27
        return $this->createDiscount($obj, self::TYPE_FREE_SHIPPING);
28
    }
29
30
    /**
31
     * @param array|\stdClass $obj
32
     * @return array
33
     */
34
    public function updateFreeShippingDiscount($obj) : array
35
    {
36
        return $this->updateDiscount($obj, self::TYPE_FREE_SHIPPING);
37
    }
38
39
    /**
40
     * @param int $id
41
     * @return array
42
     */
43
    public function getFreeShippingDiscount(int $id) : array
44
    {
45
        return $this->getDiscount($id, self::TYPE_FREE_SHIPPING);
46
    }
47
48
    /**
49
     * @param int $siteId
50
     * @param int $page
51
     * @param int $pageSize
52
     * @return array
53
     */
54
    public function getFreeShippingDiscounts(int $siteId, int $page = 1, int $pageSize = 100) : array
55
    {
56
        return $this->getDiscounts($siteId, $page, $pageSize, self::TYPE_FREE_SHIPPING);
57
    }
58
59
    /**
60
     * @param int $siteId
61
     * @return int
62
     */
63
    public function countFreeShippingDiscounts(int $siteId) : int
64
    {
65
        return $this->countDiscounts($siteId, self::TYPE_FREE_SHIPPING);
66
    }
67
68
    /*********************
69
     * General discounts *
70
     ********************/
71
72
    /**
73
     * @param array|\stdClass $obj
74
     * @return array
75
     */
76
    public function createGeneralDiscount($obj) : array
77
    {
78
        return $this->createDiscount($obj, self::TYPE_GENERAL);
79
    }
80
81
    /**
82
     * @param array|\stdClass $obj
83
     * @return array
84
     */
85
    public function updateGeneralDiscount($obj) : array
86
    {
87
        return $this->updateDiscount($obj, self::TYPE_GENERAL);
88
    }
89
90
    /**
91
     * @param int $id
92
     * @return array
93
     */
94
    public function getGeneralDiscount(int $id) : array
95
    {
96
        return $this->getDiscount($id, self::TYPE_GENERAL);
97
    }
98
99
    /**
100
     * @param int $siteId
101
     * @param int $page
102
     * @param int $pageSize
103
     * @return array
104
     */
105
    public function getGeneralDiscounts(int $siteId, int $page = 1, int $pageSize = 100) : array
106
    {
107
        return $this->getDiscounts($siteId, $page, $pageSize, self::TYPE_GENERAL);
108
    }
109
110
    /**
111
     * @param int $siteId
112
     * @return int
113
     */
114
    public function countGeneralDiscounts(int $siteId) : int
115
    {
116
        return $this->countDiscounts($siteId, self::TYPE_GENERAL);
117
    }
118
119
    /**
120
     * Alias of countGeneralDiscounts
121
     *
122
     * @param int $siteId
123
     * @return int
124
     */
125
    public function countGeneralSalesDiscounts(int $siteId) : int
126
    {
127
        return $this->countGeneralDiscounts($siteId);
128
    }
129
130
    /*******************
131
     * Group discounts *
132
     ******************/
133
134
    /**
135
     * @param array|\stdClass $obj
136
     * @return array
137
     */
138
    public function createGroupDiscount($obj) : array
139
    {
140
        return $this->createDiscount($obj, self::TYPE_GROUP);
141
    }
142
143
    /**
144
     * @param array|\stdClass $obj
145
     * @return array
146
     */
147
    public function updateGroupDiscount($obj) : array
148
    {
149
        return $this->updateDiscount($obj, self::TYPE_GROUP);
150
    }
151
152
    /**
153
     * @param int $id
154
     * @return array
155
     */
156
    public function getGroupDiscount(int $id) : array
157
    {
158
        return $this->getDiscount($id, self::TYPE_GROUP);
159
    }
160
161
    /**
162
     * @param int $siteId
163
     * @param int $page
164
     * @param int $pageSize
165
     * @return array
166
     */
167
    public function getGroupDiscounts(int $siteId, int $page = 1, int $pageSize = 100) : array
168
    {
169
        return $this->getDiscounts($siteId, $page, $pageSize, self::TYPE_GROUP);
170
    }
171
172
    /**
173
     * @param int $siteId
174
     * @return int
175
     */
176
    public function countGroupDiscounts(int $siteId) : int
177
    {
178
        return $this->countDiscounts($siteId, self::TYPE_GROUP);
179
    }
180
181
    /*********************
182
     * Package discounts *
183
     ********************/
184
185
    /**
186
     * @param array|\stdClass $obj
187
     * @return array
188
     */
189
    public function createPackageDiscount($obj) : array
190
    {
191
        return $this->createDiscount($obj, self::TYPE_PACKAGE);
192
    }
193
194
    /**
195
     * @param array|\stdClass $obj
196
     * @return array
197
     */
198
    public function updatePackageDiscount($obj) : array
199
    {
200
        return $this->updateDiscount($obj, self::TYPE_PACKAGE);
201
    }
202
203
    /**
204
     * @param int $id
205
     * @return array
206
     */
207
    public function getPackageDiscount(int $id) : array
208
    {
209
        return $this->getDiscount($id, self::TYPE_PACKAGE);
210
    }
211
212
    /**
213
     * @param int $siteId
214
     * @param int $page
215
     * @param int $pageSize
216
     * @return array
217
     */
218
    public function getPackageDiscounts(int $siteId, int $page = 1, int $pageSize = 100) : array
219
    {
220
        return $this->getDiscounts($siteId, $page, $pageSize, self::TYPE_PACKAGE);
221
    }
222
223
    /**
224
     * @param int $siteId
225
     * @return int
226
     */
227
    public function countPackageDiscounts(int $siteId) : int
228
    {
229
        return $this->countDiscounts($siteId, self::TYPE_PACKAGE);
230
    }
231
232
    /**
233
     * Alias of countPackageDiscounts
234
     *
235
     * @param int $siteId
236
     * @return int
237
     */
238
    public function countPackageSalesDiscounts(int $siteId) : int
239
    {
240
        return $this->countPackageDiscounts($siteId);
241
    }
242
243
    /**********************
244
     * Quantity discounts *
245
     *********************/
246
247
    /**
248
     * @param array|\stdClass $obj
249
     * @return array
250
     */
251
    public function createQuantityDiscount($obj) : array
252
    {
253
        return $this->createDiscount($obj, self::TYPE_QUANTITY);
254
    }
255
256
    /**
257
     * @param array|\stdClass $obj
258
     * @return array
259
     */
260
    public function updateQuantityDiscount($obj) : array
261
    {
262
        return $this->updateDiscount($obj, self::TYPE_QUANTITY);
263
    }
264
265
    /**
266
     * @param int $id
267
     * @return array
268
     */
269
    public function getQuantityDiscount(int $id) : array
270
    {
271
        return $this->getDiscount($id, self::TYPE_QUANTITY);
272
    }
273
274
    /**
275
     * @param int $siteId
276
     * @param int $page
277
     * @param int $pageSize
278
     * @return array
279
     */
280
    public function getQuantityDiscounts(int $siteId, int $page = 1, int $pageSize = 100) : array
281
    {
282
        return $this->getDiscounts($siteId, $page, $pageSize, self::TYPE_QUANTITY);
283
    }
284
285
    /**
286
     * @param int $siteId
287
     * @return int
288
     */
289
    public function countQuantityDiscounts(int $siteId) : int
290
    {
291
        return $this->countDiscounts($siteId, self::TYPE_QUANTITY);
292
    }
293
294
    /**
295
     * Alias of countQuantityDiscounts
296
     *
297
     * @param int $siteId
298
     * @return int
299
     */
300
    public function countQuantitySalesDiscounts(int $siteId) : int
301
    {
302
        return $this->countQuantityDiscounts($siteId);
303
    }
304
305
    /**
306
     * Deletes a sales discount
307
     *
308
     * @param int $id
309
     * @return bool
310
     */
311
    public function deleteSalesDiscount(int $id) : bool
312
    {
313
        Assert::that($id)->greaterThan(0, '$id must be positive');
314
315
        return (bool)$this->master->doRequest(
316
            'DELETE',
317
            sprintf('/admin/WEBAPI/Endpoints/v1_0/DiscountService/{KEY}/salesDiscount/%d', $id)
318
        );
319
    }
320
321
    /*
322
     * Helper methods
323
     */
324 View Code Duplication
    protected function createDiscount($obj, string $type) : array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
325
    {
326
        Assert::that($type)->choice(self::getTypes());
327
328
        return (array)$this->master->doRequest(
329
            'POST',
330
            sprintf('/admin/WEBAPI/Endpoints/v1_0/DiscountService/{KEY}/%s', $type),
331
            $obj
332
        );
333
    }
334
335 View Code Duplication
    protected function updateDiscount($obj, string $type) : array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
336
    {
337
        Assert::that($type)->choice(self::getTypes());
338
339
        return (array)$this->master->doRequest(
340
            'PUT',
341
            sprintf('/admin/WEBAPI/Endpoints/v1_0/DiscountService/{KEY}/%s', $type),
342
            $obj
343
        );
344
    }
345
346 View Code Duplication
    protected function getDiscount(int $id, string $type) : array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
347
    {
348
        Assert::that($id)->greaterThan(0, '$id must be positive');
349
        Assert::that($type)->choice(self::getTypes());
350
351
        return (array)$this->master->doRequest(
352
            'GET',
353
            sprintf('/admin/WEBAPI/Endpoints/v1_0/DiscountService/{KEY}/%s/%d', $type, $id)
354
        );
355
    }
356
357
    protected function getDiscounts(int $siteId, int $page, int $pageSize, string $type) : array
358
    {
359
        Assert::that($siteId)->greaterThan(0, '$siteId must be positive');
360
        Assert::that($page)->greaterThan(0, '$page must be positive');
361
        Assert::that($pageSize)->greaterThan(0, '$pageSize must be positive');
362
        Assert::that($type)->choice(self::getTypes());
363
364
        $type .= 's';
365
366
        return (array)$this->master->doRequest(
367
            'GET',
368
            sprintf('/admin/WEBAPI/Endpoints/v1_0/DiscountService/{KEY}/%s/%d/%d/%d', $type, $siteId, $page, $pageSize)
369
        );
370
    }
371
372 View Code Duplication
    protected function countDiscounts(int $siteId, string $type) : int
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
373
    {
374
        Assert::that($siteId)->greaterThan(0, '$siteId must be positive');
375
        Assert::that($type)->choice(self::getTypes());
376
377
        $type .= 'sCount';
378
379
        return (int)$this->master->doRequest(
380
            'GET',
381
            sprintf('/admin/WEBAPI/Endpoints/v1_0/DiscountService/{KEY}/%s/%d', $type, $siteId)
382
        );
383
    }
384
385
    protected static function getTypes() : array
386
    {
387
        return [
388
            self::TYPE_FREE_SHIPPING => self::TYPE_FREE_SHIPPING,
389
            self::TYPE_GENERAL => self::TYPE_GENERAL,
390
            self::TYPE_GROUP => self::TYPE_GROUP,
391
            self::TYPE_PACKAGE => self::TYPE_PACKAGE,
392
            self::TYPE_QUANTITY => self::TYPE_QUANTITY,
393
        ];
394
    }
395
}
396