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

DocumentProvider::create()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 44
Code Lines 26

Duplication

Lines 10
Ratio 22.73 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 26
nc 4
nop 1
dl 10
loc 44
ccs 13
cts 13
cp 1
crap 4
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace Sausin\Signere;
4
5
use BadMethodCallException;
6
use UnexpectedValueException;
7
8
class DocumentProvider extends BaseClass
9
{
10
    /** The URI of the action */
11
    const URI = 'https://api.signere.no/api/DocumentProvider';
12
13
    /**
14
     * Retrieves a document provider account.
15
     *
16
     * @param  string $providerId
17
     * @return object
18
     */
19 1
    public function getProviderAccount(string $providerId)
20
    {
21
        // make the URL for this request
22 1
        $url = $this->transformUrl(sprintf('%s/%s', self::URI, $providerId));
23
24
        // get the headers for this request
25 1
        $headers = $this->headers->make('GET', $url);
26
27
        // get the response
28 1
        $response = $this->client->get($url, [
29 1
            'headers' => $headers,
30
        ]);
31
32
        // return the response
33 1
        return $response;
34
    }
35
36
    /**
37
     * Gets the expires date for your BankID certificate. If you don't
38
     * have your own BankID certificate it will return Bad request.
39
     *
40
     * @return object
41
     */
42 1
    public function getCertExpiry()
43
    {
44
        // make the URL for this request
45 1
        $url = $this->transformUrl(sprintf('%s/CertificateExpires', self::URI));
46
47
        // get the headers for this request
48 1
        $headers = $this->headers->make('GET', $url);
49
50
        // get the response
51 1
        $response = $this->client->get($url, [
52 1
            'headers' => $headers,
53
        ]);
54
55
        // return the response
56 1
        return $response;
57
    }
58
59
    /**
60
     * Get the usage when using prepaid or demo account.
61
     *
62
     * @param  string       $providerId
63
     * @param  bool|bool $demo
64
     * @return object
65
     */
66 2
    public function getUsage(string $providerId, bool $demo = false)
67
    {
68
        // make the URL for this request
69 2
        $url = $this->transformUrl(sprintf(
70 2
            '%s/quota/%s?ProviderId=%s',
71 2
            self::URI,
72 2
            $demo ? 'demo' : 'prepaid',
73
            $providerId
74
        ));
75
76
        // get the headers for this request
77 2
        $headers = $this->headers->make('GET', $url);
78
79
        // get the response
80 2
        $response = $this->client->get($url, [
81 2
            'headers' => $headers,
82
        ]);
83
84
        // return the response
85 2
        return $response;
86
    }
87
88
    /**
89
     * Creates a new document provider.
90
     *
91
     * @param  array  $body
92
     * @return object
93
     */
94 1
    public function create(array $body)
95
    {
96
        // keys that are mandatory for this request
97
        $needKeys = [
98 1
            'BillingAddress1',
99
            'BillingCity',
100
            'BillingPostalCode',
101
            'CompanyEmail',
102
            'CompanyPhone',
103
            'DealerId',
104
            'LegalContactEmail',
105
            'LegalContactName',
106
            'LegalContactPhone',
107
            'MvaNumber',
108
            'Name',
109
        ];
110
111
        // if the body doesn't have needed fields, throw an exception
112 1 View Code Duplication
        if (! array_has_all_keys($body, $needKeys)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
            throw new BadMethodCallException(
114
                'Missing fields in input array. Need '.implode(', ', $needKeys)
115
            );
116 1
        } elseif (isset($body['BillingPlan'])) {
117 1
            $expected = ['Small', 'Medium', 'Large'];
118 1
            if (! in_array($body['BillingPlan'], $expected)) {
119 1
                throw new UnexpectedValueException('BillingPlan should be one of '.implode(', ', $expected));
120
            }
121
        }
122
123
        // make the URL for this request
124 1
        $url = $this->transformUrl(self::URI);
125
126
        // get the headers for this request
127 1
        $headers = $this->headers->make('POST', $url, $body);
128
129
        // get the response
130 1
        $response = $this->client->post($url, [
131 1
            'headers' => $headers,
132 1
            'json' => $body,
133
        ]);
134
135
        // return the response
136 1
        return $response;
137
    }
138
139
    /**
140
     * Updates a new document provider.
141
     *
142
     * @param  array  $body
143
     * @return object
144
     */
145 1
    public function update(array $body)
146
    {
147
        // keys that are mandatory for this request
148 1
        $needKeys = ['Mobile', 'ProviderId'];
149
150
        // if the body doesn't have needed fields, throw an exception
151 1 View Code Duplication
        if (! array_has_all_keys($body, $needKeys)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
152 1
            throw new BadMethodCallException(
153 1
                'Missing fields in input array. Need '.implode(', ', $needKeys)
154
            );
155 1
        } elseif (isset($body['BillingPlan'])) {
156
            $expected = ['Small', 'Medium', 'Large'];
157
            if (! in_array($body['BillingPlan'], $expected)) {
158
                throw new UnexpectedValueException('BillingPlan should be one of '.implode(', ', $expected));
159
            }
160
        }
161
162
        // make the URL for this request
163 1
        $url = $this->transformUrl(self::URI);
164
165
        // get the headers for this request
166 1
        $headers = $this->headers->make('PUT', $url, $body);
167
168
        // get the response
169 1
        $response = $this->client->put($url, [
170 1
            'headers' => $headers,
171 1
            'json' => $body,
172
        ]);
173
174
        // return the response
175 1
        return $response;
176
    }
177
}
178