Completed
Push — master ( fa3e73...f2d77a )
by Jasper
07:02
created

Repository   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
eloc 18
dl 0
loc 127
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getClient() 0 3 1
A all() 0 3 1
A __construct() 0 3 1
A getEndpoint() 0 3 1
A saveExisting() 0 5 1
A save() 0 7 2
A deleteById() 0 3 1
A delete() 0 3 1
A take() 0 3 1
A find() 0 3 1
A saveNew() 0 3 1
1
<?php
2
3
namespace Swis\JsonApi\Client;
4
5
use Swis\JsonApi\Client\Interfaces\DocumentClientInterface;
6
use Swis\JsonApi\Client\Interfaces\ItemDocumentInterface;
7
use Swis\JsonApi\Client\Interfaces\RepositoryInterface;
8
9
class Repository implements RepositoryInterface
10
{
11
    /**
12
     * @var \Swis\JsonApi\Client\Interfaces\DocumentClientInterface
13
     */
14
    protected $client;
15
16
    /**
17
     * @var string
18
     */
19
    protected $endpoint = '';
20
21
    /**
22
     * @param \Swis\JsonApi\Client\Interfaces\DocumentClientInterface $client
23
     */
24 45
    public function __construct(DocumentClientInterface $client)
25
    {
26 45
        $this->client = $client;
27 45
    }
28
29
    /**
30
     * @return \Swis\JsonApi\Client\Interfaces\DocumentClientInterface
31
     */
32 40
    public function getClient()
33
    {
34 40
        return $this->client;
35
    }
36
37
    /**
38
     * @return string
39
     */
40 40
    public function getEndpoint()
41
    {
42 40
        return $this->endpoint;
43
    }
44
45
    /**
46
     * @param array $parameters
47
     *
48
     * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
49
     */
50 5
    public function all(array $parameters = [])
51
    {
52 5
        return $this->getClient()->get($this->getEndpoint().'?'.http_build_query($parameters));
53
    }
54
55
    /**
56
     * @param array $parameters
57
     *
58
     * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
59
     */
60 5
    public function take(array $parameters = [])
61
    {
62 5
        return $this->getClient()->get($this->getEndpoint().'?'.http_build_query($parameters));
63
    }
64
65
    /**
66
     * @param       $id
67
     * @param array $parameters
68
     *
69
     * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
70
     */
71 5
    public function find($id, array $parameters = [])
72
    {
73 5
        return $this->getClient()->get($this->getEndpoint().'/'.urlencode($id).'?'.http_build_query($parameters));
74
    }
75
76
    /**
77
     * @param \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface $document
78
     * @param array                                                 $parameters
79
     *
80
     * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
81
     */
82 10
    public function save(ItemDocumentInterface $document, array $parameters = [])
83
    {
84 10
        if ($document->getData()->isNew()) {
85 5
            return $this->saveNew($document, $parameters);
86
        }
87
88 5
        return $this->saveExisting($document, $parameters);
89
    }
90
91
    /**
92
     * @param \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface $document
93
     * @param array                                                 $parameters
94
     *
95
     * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
96
     */
97 5
    protected function saveNew(ItemDocumentInterface $document, array $parameters = [])
98
    {
99 5
        return $this->getClient()->post($this->getEndpoint().'?'.http_build_query($parameters), $document);
100
    }
101
102
    /**
103
     * @param \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface $document
104
     * @param array                                                 $parameters
105
     *
106
     * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
107
     */
108 5
    protected function saveExisting(ItemDocumentInterface $document, array $parameters = [])
109
    {
110 5
        return $this->getClient()->patch(
111 5
            $this->getEndpoint().'/'.urlencode($document->getData()->getId()).'?'.http_build_query($parameters),
112 5
            $document
113
        );
114
    }
115
116
    /**
117
     * @param \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface $document
118
     * @param array                                                 $parameters
119
     *
120
     * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
121
     */
122 5
    public function delete(ItemDocumentInterface $document, array $parameters = [])
123
    {
124 5
        return $this->deleteById($document->getData()->getId(), $parameters);
125
    }
126
127
    /**
128
     * @param       $id
129
     * @param array $parameters
130
     *
131
     * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
132
     */
133 10
    public function deleteById($id, array $parameters = [])
134
    {
135 10
        return $this->getClient()->delete($this->getEndpoint().'/'.urlencode($id).'?'.http_build_query($parameters));
136
    }
137
}
138