Passed
Push — develop ( d429ec...3e77ee )
by Edwin
02:54
created

AbstractCrudResource::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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