Passed
Push — develop ( 3f3953...375edc )
by Edwin
03:01
created

AbstractCrudResource::create()   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 1
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 11
    public function get(float $id, array $fields = [])
23
    {
24 11
        $response = $this->request('GET', sprintf('/admin/%s/%s.json', $this->resourceKeyPleural, $id), [
25
            'query' => [
26 11
                'fields' => $fields
27
            ]
28
        ]);
29
30 10
        return $response[$this->resourceKeySingular];
31
    }
32
33
    /**
34
     * @param array $query
35
     * @return array
36
     */
37 8
    public function all(array $query = [])
38
    {
39 8
        $response = $this->request('GET', sprintf('/admin/%s.json', $this->resourceKeyPleural), [
40 8
            'query' => $query
41
        ]);
42
43 8
        return $response[$this->resourceKeyPleural];
44
    }
45
46
    /**
47
     * @param array $params
48
     * @return array
49
     */
50 9
    public function create(array $params = [])
51
    {
52 9
        $response = $this->request('POST', sprintf('/admin/%s.json', $this->resourceKeyPleural), [
53 9
            'body' => json_encode([
54 9
                $this->resourceKeySingular => $params,
55
            ]),
56
        ]);
57
58 8
        return $response[$this->resourceKeySingular];
59
    }
60
61
    /**
62
     * @param float $id
63
     * @param array $params
64
     * @return array
65
     */
66 7
    public function update(float $id, array $params = [])
67
    {
68 7
        $response = $this->request('PUT', sprintf('/admin/%s/%s.json', $this->resourceKeyPleural, $id), [
69 7
            'body' => json_encode([
70 7
                $this->resourceKeySingular => $params,
71
            ]),
72
        ]);
73
74 7
        return $response[$this->resourceKeySingular];
75
    }
76
77
    /**
78
     * @param float $id
79
     */
80 8
    public function delete(float $id)
81
    {
82 8
        $this->request('DELETE', sprintf('/admin/%s/%s.json', $this->resourceKeyPleural, $id));
83 8
    }
84
}
85