Completed
Push — master ( 365164...87d4f7 )
by Saurabh
08:29 queued 30s
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.0378

Importance

Changes 0
Metric Value
cc 4
eloc 26
nc 4
nop 1
dl 10
loc 44
ccs 13
cts 15
cp 0.8667
crap 4.0378
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace Sausin\Signere;
4
5
use GuzzleHttp\Client;
6
use BadMethodCallException;
7
use UnexpectedValueException;
8
9
class DocumentProvider
10
{
11
    use AdjustUrl;
12
13
    /** @var \GuzzleHttp\Client */
14
    protected $client;
15
16
    /** @var Headers */
17
    protected $headers;
18
19
    /** @var string The environment this is being run in */
20
    protected $environment;
21
22
    /** The URI of the action */
23
    const URI = 'https://api.signere.no/api/DocumentProvider';
24
25
    /**
26
     * Instantiate the class.
27
     *
28
     * @param Client  $client
29
     * @param Headers $headers
30
     * @param string  $environment
31
     */
32 6
    public function __construct(Client $client, Headers $headers, $environment = null)
33
    {
34 6
        $this->client = $client;
35 6
        $this->headers = $headers;
36 6
        $this->environment = $environment;
37 6
    }
38
39
    /**
40
     * Retrieves a document provider account.
41
     *
42
     * @param  string $providerId
43
     * @return object
44
     */
45 1
    public function getProviderAccount(string $providerId)
46
    {
47
        // make the URL for this request
48 1
        $url = $this->transformUrl(sprintf('%s/%s', self::URI, $providerId));
49
50
        // get the headers for this request
51 1
        $headers = $this->headers->make('GET', $url);
52
53
        // get the response
54 1
        $response = $this->client->get($url, [
55 1
            'headers' => $headers,
56
        ]);
57
58
        // return the response
59 1
        return $response;
60
    }
61
62
    /**
63
     * Gets the expires date for your BankID certificate. If you don't
64
     * have your own BankID certificate it will return Bad request.
65
     *
66
     * @return object
67
     */
68 1
    public function getCertExpiry()
69
    {
70
        // make the URL for this request
71 1
        $url = $this->transformUrl(sprintf('%s/CertificateExpires', self::URI));
72
73
        // get the headers for this request
74 1
        $headers = $this->headers->make('GET', $url);
75
76
        // get the response
77 1
        $response = $this->client->get($url, [
78 1
            'headers' => $headers,
79
        ]);
80
81
        // return the response
82 1
        return $response;
83
    }
84
85
    /**
86
     * Get the usage when using prepaid or demo account.
87
     *
88
     * @param  string       $providerId
89
     * @param  bool|bool $demo
90
     * @return object
91
     */
92 2
    public function getUsage(string $providerId, bool $demo = false)
93
    {
94
        // make the URL for this request
95 2
        $url = $this->transformUrl(sprintf(
96 2
            '%s/quota/%s?ProviderId=%s',
97 2
            self::URI,
98 2
            $demo ? 'demo' : 'prepaid',
99 2
            $providerId
100
        ));
101
102
        // get the headers for this request
103 2
        $headers = $this->headers->make('GET', $url);
104
105
        // get the response
106 2
        $response = $this->client->get($url, [
107 2
            'headers' => $headers,
108
        ]);
109
110
        // return the response
111 2
        return $response;
112
    }
113
114
    /**
115
     * Creates a new document provider.
116
     *
117
     * @param  array  $body
118
     * @return object
119
     */
120 1
    public function create(array $body)
121
    {
122
        // keys that are mandatory for this request
123
        $needKeys = [
124 1
            'BillingAddress1',
125
            'BillingCity',
126
            'BillingPostalCode',
127
            'CompanyEmail',
128
            'CompanyPhone',
129
            'DealerId',
130
            'LegalContactEmail',
131
            'LegalContactName',
132
            'LegalContactPhone',
133
            'MvaNumber',
134
            'Name',
135
        ];
136
137
        // if the body doesn't have needed fields, throw an exception
138 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...
139
            throw new BadMethodCallException(
140
                'Missing fields in input array. Need '.implode(', ', $needKeys)
141
            );
142 1
        } elseif (isset($body['BillingPlan'])) {
143 1
            $expected = ['Small', 'Medium', 'Large'];
144 1
            if (! in_array($body['BillingPlan'], $expected)) {
145 1
                throw new UnexpectedValueException('BillingPlan should be one of '.implode(', ', $expected));
146
            }
147
        }
148
149
        // make the URL for this request
150 1
        $url = $this->transformUrl(self::URI);
151
152
        // get the headers for this request
153 1
        $headers = $this->headers->make('POST', $url, $body);
154
155
        // get the response
156 1
        $response = $this->client->post($url, [
157 1
            'headers' => $headers,
158 1
            'json' => $body,
159
        ]);
160
161
        // return the response
162 1
        return $response;
163
    }
164
165
    /**
166
     * Updates a new document provider.
167
     *
168
     * @param  array  $body
169
     * @return object
170
     */
171 1
    public function update(array $body)
172
    {
173
        // keys that are mandatory for this request
174 1
        $needKeys = ['Mobile', 'ProviderId'];
175
176
        // if the body doesn't have needed fields, throw an exception
177 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...
178 1
            throw new BadMethodCallException(
179 1
                'Missing fields in input array. Need '.implode(', ', $needKeys)
180
            );
181 1
        } elseif (isset($body['BillingPlan'])) {
182
            $expected = ['Small', 'Medium', 'Large'];
183
            if (! in_array($body['BillingPlan'], $expected)) {
184
                throw new UnexpectedValueException('BillingPlan should be one of '.implode(', ', $expected));
185
            }
186
        }
187
188
        // make the URL for this request
189 1
        $url = $this->transformUrl(self::URI);
190
191
        // get the headers for this request
192 1
        $headers = $this->headers->make('PUT', $url, $body);
193
194
        // get the response
195 1
        $response = $this->client->put($url, [
196 1
            'headers' => $headers,
197 1
            'json' => $body,
198
        ]);
199
200
        // return the response
201 1
        return $response;
202
    }
203
}
204