Passed
Push — develop ( e30f69...0db571 )
by Edwin
03:07
created

DiscountCode   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 23.81%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 92
ccs 5
cts 21
cp 0.2381
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A lookUp() 0 14 2
A createBatch() 0 10 1
A getBatchAll() 0 11 1
A getBatch() 0 10 1
1
<?php
2
3
namespace ShopifyClient\Resource;
4
5
/**
6
 * https://help.shopify.com/api/reference/discountcode
7
 */
8
class DiscountCode extends AbstractNestedCountableCrudResource
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $resourceParentEndpointPleural = 'price_rules';
14
15
    /**
16
     * @var string
17
     */
18
    protected $resourceChildEndpointPleural = 'discount_codes';
19
20
    /**
21
     * @var string
22
     */
23
    protected $resourceChildKeySingular = 'discount_code';
24
25
    /**
26
     * @var string
27
     */
28
    protected $resourceChildKeyPleural = 'discount_codes';
29
30
    /**
31
     * @param string $code
32
     * @return array|null
33
     */
34 1
    public function lookUp(string $code)
35
    {
36 1
        $response = $this->request('GET', sprintf('/admin/discount_codes/lookup.json'), [
37
            'query' => [
38 1
                'code' => $code,
39
            ],
40
        ]);
41
42 1
        if (empty($response)) {
43 1
            return null;
44
        }
45
46
        return $response['discount_code'];
47
    }
48
49
    /**
50
     * @param float $parentId
51
     * @param array $params
52
     * @return array
53
     */
54
    public function createBatch(float $parentId, array $params = [])
55
    {
56
        $response = $this->request('GET', sprintf('/admin/price_rules/%s/batch.json', $parentId), [
57
            'body' => json_encode([
58
                $this->resourceChildKeySingular => $params,
59
            ]),
60
        ]);
61
62
        return $response['discount_code_creation'];
63
    }
64
65
    /**
66
     * @param float $parentId
67
     * @param float $id
68
     * @param array $params
69
     * @return array
70
     */
71
    public function getBatchAll(float $parentId, float $id, array $params = [])
72
    {
73
        $response = $this->request('GET', sprintf('/admin/price_rules/%s/batch/%s/discount_codes.json', $parentId, $id),
74
            [
75
                'body' => json_encode([
76
                    $this->resourceChildKeySingular => $params,
77
                ]),
78
            ]);
79
80
        return $response['discount_codes'];
81
    }
82
83
    /**
84
     * @param float $parentId
85
     * @param float $id
86
     * @param array $params
87
     * @return array
88
     */
89
    public function getBatch(float $parentId, float $id, array $params = [])
90
    {
91
        $response = $this->request('GET', sprintf('/admin/price_rules/%s/batch/%s.json', $parentId, $id), [
92
            'body' => json_encode([
93
                $this->resourceChildKeySingular => $params,
94
            ]),
95
        ]);
96
97
        return $response['discount_code_creation'];
98
    }
99
}
100