Passed
Push — develop ( 0db571...fbac2a )
by Edwin
03:12
created

DiscountCode   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 21.74%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 95
ccs 5
cts 23
cp 0.2174
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 14 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(
74
            'GET',
75
            sprintf('/admin/price_rules/%s/batch/%s/discount_codes.json', $parentId, $id),
76
            [
77
                'body' => json_encode([
78
                    $this->resourceChildKeySingular => $params,
79
                ]),
80
            ]
81
        );
82
83
        return $response['discount_codes'];
84
    }
85
86
    /**
87
     * @param float $parentId
88
     * @param float $id
89
     * @param array $params
90
     * @return array
91
     */
92
    public function getBatch(float $parentId, float $id, array $params = [])
93
    {
94
        $response = $this->request('GET', sprintf('/admin/price_rules/%s/batch/%s.json', $parentId, $id), [
95
            'body' => json_encode([
96
                $this->resourceChildKeySingular => $params,
97
            ]),
98
        ]);
99
100
        return $response['discount_code_creation'];
101
    }
102
}
103