Completed
Push — master ( 93b826...c2eec1 )
by Taosikai
11:32
created

NestCrudable::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
/*
4
 * This file is part of the slince/shopify-api-php
5
 *
6
 * (c) Slince <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Slince\Shopify\Common\Manager;
13
14
use Doctrine\Common\Inflector\Inflector;
15
use Slince\Shopify\Common\CursorBasedPagination;
16
use Slince\Shopify\Common\Model\ModelInterface;
17
18
abstract class NestCrudable extends AbstractManager
19
{
20
    /**
21
     * Gets the parent resource name.
22
     *
23
     * @return string
24
     */
25
    abstract public function getParentResourceName();
26
27
    /**
28
     * Finds the resources by given query condition.
29
     *
30
     * @param int   $parentId
31
     * @param array $query
32
     *
33
     * @return ModelInterface[]
34
     */
35
    public function findAll($parentId, array $query = [])
36
    {
37
        $resource = $this->createPartialResourceUrlForList($parentId);
38
        $data = $this->client->get($resource, $query);
39
40
        return $this->createMany(reset($data));
41
    }
42
43
    /**
44
     * Create a paging query.
45
     *
46
     * @param int   $parentId
47
     * @param array $query
48
     *
49
     * @return CursorBasedPagination
50
     * @codeCoverageIgnore
51
     */
52
    public function paginate($parentId, array $query = [])
53
    {
54
        $resource = $this->createPartialResourceUrlForList($parentId);
55
56
        return new CursorBasedPagination($this, $resource, $query);
57
    }
58
59
    /**
60
     * Finds the resource by given id.
61
     *
62
     * @param int $parentId
63
     * @param int $id
64
     *
65
     * @return ModelInterface
66
     */
67
    public function find($parentId, $id)
68
    {
69
        $resource = $this->createPartialResourceUrlForView($parentId, $id);
70
        $data = $this->client->get($resource);
71
72
        return $this->fromArray(reset($data));
73
    }
74
75
    /**
76
     * Delete a resource.
77
     *
78
     * @param int $parentId
79
     * @param int $id
80
     */
81
    public function remove($parentId, $id)
82
    {
83
        $resource = $this->createPartialResourceUrlForView($parentId, $id);
84
        $this->client->delete($resource);
85
    }
86
87
    /**
88
     * Updates a resource.
89
     *
90
     * @param int   $parentId
91
     * @param int   $id
92
     * @param array $data
93
     *
94
     * @return ModelInterface
95
     */
96
    public function update($parentId, $id, array $data)
97
    {
98
        $resource = $this->createPartialResourceUrlForView($parentId, $id);
99
        $data = $this->client->put($resource, [$this->getResourceName() => $data]);
100
101
        return $this->fromArray(reset($data));
102
    }
103
104
    /**
105
     * Creates a resource.
106
     *
107
     * @param int   $parentId
108
     * @param array $data
109
     *
110
     * @return ModelInterface
111
     */
112
    public function create($parentId, array $data)
113
    {
114
        $resource = $this->createPartialResourceUrlForList($parentId);
115
        $data = $this->client->post($resource, [$this->getResourceName() => $data]);
116
117
        return $this->fromArray(reset($data));
118
    }
119
120
    /**
121
     * Gets the number of resource with given query.
122
     *
123
     * @param int   $parentId
124
     * @param array $query
125
     *
126
     * @return int
127
     */
128
    public function count($parentId, array $query = [])
129
    {
130
        $resource = $this->createPartialResourceUrlForList($parentId).'/count';
131
132
        return $this->client->get($resource, $query)['count'];
133
    }
134
135
    protected function createPartialResourceUrlForList($parentId)
136
    {
137
        return Inflector::pluralize($this->getParentResourceName())
138
            .'/'.$parentId
139
            .'/'.Inflector::pluralize($this->getResourceName());
140
    }
141
142
    protected function createPartialResourceUrlForView($parentId, $id)
143
    {
144
        return  $this->createPartialResourceUrlForList($parentId)
145
            .'/'.$id;
146
    }
147
}