Passed
Push — develop ( 375edc...65434e )
by Edwin
02:48
created

AbstractNestedCrudResource::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 12
cts 12
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 2
crap 1
1
<?php
2
3
namespace ShopifyClient\Resource;
4
5
abstract class AbstractNestedCrudResource extends AbstractResource
6
{
7
    /**
8
     * @var string
9
     */
10
    protected $resourceParentEndpointPleural;
11
12
    /**
13
     * @var string
14
     */
15
    protected $resourceChildEndpointPleural;
16
17
    /**
18
     * @var string
19
     */
20
    protected $resourceChildKeySingular;
21
22
    /**
23
     * @var string
24
     */
25
    protected $resourceChildKeyPleural;
26
27
    /**
28
     * @param float $parentId
29
     * @param float $id
30
     * @param array $fields
31
     * @return array
32
     */
33 4
    public function get(float $parentId, float $id, array $fields = [])
34
    {
35 4
        $response = $this->request('GET', sprintf('/admin/%s/%s/%s/%s.json',
36 4
            $this->resourceParentEndpointPleural,
37 4
            $parentId,
38 4
            $this->resourceChildEndpointPleural,
39 4
            $id
40
        ), [
41
            'query' => [
42 4
                'fields' => $fields
43
            ]
44
        ]);
45
46 4
        return $response[$this->resourceChildKeySingular];
47
    }
48
49
    /**
50
     * @param float $parentId
51
     * @param array $query
52
     * @return array
53
     */
54 4
    public function all(float $parentId, array $query = [])
55
    {
56 4
        $response = $this->request('GET', sprintf('/admin/%s/%s/%s.json',
57 4
            $this->resourceParentEndpointPleural,
58 4
            $parentId,
59 4
            $this->resourceChildEndpointPleural
60
        ), [
61 4
            'query' => $query
62
        ]);
63
64 4
        return $response[$this->resourceChildKeyPleural];
65
    }
66
67
    /**
68
     * @param float $parentId
69
     * @param array $params
70
     * @return array
71
     */
72 4
    public function create(float $parentId, array $params = [])
73
    {
74 4
        print_r(sprintf('/admin/%s/%s/%s.json',
75 4
            $this->resourceParentEndpointPleural,
76 4
            $parentId,
77 4
            $this->resourceChildEndpointPleural
78
        ));
79 4
        $response = $this->request('POST', sprintf('/admin/%s/%s/%s.json',
80 4
            $this->resourceParentEndpointPleural,
81 4
            $parentId,
82 4
            $this->resourceChildEndpointPleural
83
        ), [
84 4
            'body' => json_encode([
85 4
                $this->resourceChildKeySingular => $params,
86
            ]),
87
        ]);
88
89 4
        return $response[$this->resourceChildKeySingular];
90
    }
91
92
    /**
93
     * @param float $parentId
94
     * @param float $id
95
     * @param array $params
96
     * @return array
97
     */
98 4
    public function update(float $parentId, float $id, array $params = [])
99
    {
100 4
        $response = $this->request('PUT', sprintf('/admin/%s/%s/%s/%s.json',
101 4
            $this->resourceParentEndpointPleural,
102 4
            $parentId,
103 4
            $this->resourceChildEndpointPleural,
104 4
            $id
105
        ), [
106 4
            'body' => json_encode([
107 4
                $this->resourceChildKeySingular => $params,
108
            ]),
109
        ]);
110
111 4
        return $response[$this->resourceChildKeySingular];
112
    }
113
114
    /**
115
     * @param float $parentId
116
     * @param float $id
117
     */
118 4
    public function delete(float $parentId, float $id)
119
    {
120 4
        $this->request('DELETE', sprintf('/admin/%s/%s/%s/%s.json',
121 4
            $this->resourceParentEndpointPleural,
122 4
            $parentId,
123 4
            $this->resourceChildEndpointPleural,
124 4
            $id
125
        ));
126 4
    }
127
}
128