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

AbstractCrudResource   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 10 1
A all() 0 8 1
A create() 0 10 1
A update() 0 10 1
A delete() 0 4 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