Completed
Push — master ( e41c6a...6a7f08 )
by Saurabh
03:07
created

Receiver::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 2
dl 0
loc 20
ccs 8
cts 8
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Sausin\Signere;
4
5
class Receiver extends BaseClass
6
{
7
    /** The URI of the action */
8
    const URI = 'https://api.signere.no/api/Receiver';
9
10
    /**
11
     * Get a specific or all receivers.
12
     *
13
     * @param  string      $provider
14
     * @param  string|null $receiver
15
     * @return object
16
     */
17 1
    public function get(string $provider, string $receiver = null)
18
    {
19
        // make the URL for this request
20 1
        if (is_null($receiver)) {
21 1
            $url = $this->transformUrl(sprintf('%s?ProviderId=%s', self::URI, $provider));
22
        } else {
23 1
            $url = $this->transformUrl(sprintf('%s/%s?ProviderId=%s', self::URI, $receiver, $provider));
24
        }
25
26
        // get the headers for this request
27 1
        $headers = $this->headers->make('GET', $url);
28
29
        // get the response
30 1
        $response = $this->client->get($url, [
31 1
            'headers' => $headers,
32
        ]);
33
34
        // return the response
35 1
        return $response;
36
    }
37
38
    /**
39
     * Create a receiver.
40
     *
41
     * @param  array  $receiver
42
     * @return object
43
     */
44 2
    public function create(array $receiver)
45
    {
46
        // make the URL for this request
47 2
        $url = $this->transformUrl(self::URI);
48
49
        // get the headers for this request
50 2
        $headers = $this->headers->make('POST', $url, $receiver);
51
52
        // get the response
53 2
        $response = $this->client->post($url, [
54 2
            'headers' => $headers,
55 2
            'json' => $receiver,
56
        ]);
57
58
        // return the response
59 2
        return $response;
60
    }
61
62
    /**
63
     * Create many receivers.
64
     *
65
     * @param  array  $receivers
66
     * @return object
67
     */
68 1
    public function createMany(array $receivers)
69
    {
70
        // instantiate empty responses array
71 1
        $responses = [];
72
73
        // loop through the receivers to create them all
74 1
        foreach ($receivers as $receiver) {
75 1
            $responses[] = $this->create($receiver);
76
        }
77
78
        // return them all
79 1
        return $responses;
80
    }
81
82
    /**
83
     * Delete a receiver.
84
     *
85
     * @param  string $provider
86
     * @param  string $receiver
87
     * @return object
88
     */
89 2
    public function delete(string $provider, string $receiver)
90
    {
91
        // make the URL for this request
92 2
        $url = $this->transformUrl(sprintf('%s/%s/%s', self::URI, $provider, $receiver));
93
94
        // get the headers for this request
95 2
        $headers = $this->headers->make('DELETE', $url);
96
97
        // get the response
98 2
        $response = $this->client->delete($url, [
99 2
            'headers' => $headers,
100
        ]);
101
102
        // return the response
103 2
        return $response;
104
    }
105
106
    /**
107
     * Delete many receivers.
108
     *
109
     * @param  string $provider
110
     * @param  array  $receivers
111
     * @return object
112
     */
113 1
    public function deleteMany(string $provider, array $receivers)
114
    {
115
        // instantiate empty responses array
116 1
        $responses = [];
117
118
        // loop through the receivers to create them all
119 1
        foreach ($receivers as $receiver) {
120 1
            $responses[] = $this->delete($provider, $receiver);
121
        }
122
123
        // return them all
124 1
        return $responses;
125
    }
126
127
    /**
128
     * Delete all receivers.
129
     *
130
     * @param  string $provider
131
     * @return object
132
     */
133 1
    public function deleteAll(string $provider)
134
    {
135
        // make the URL for this request
136 1
        $url = $this->transformUrl(sprintf('%s/%s', self::URI, $provider));
137
138
        // get the headers for this request
139 1
        $headers = $this->headers->make('DELETE', $url);
140
141
        // get the response
142 1
        $response = $this->client->delete($url, [
143 1
            'headers' => $headers,
144
        ]);
145
146
        // return the response
147 1
        return $response;
148
    }
149
}
150