Completed
Push — master ( 365164...87d4f7 )
by Saurabh
08:29 queued 30s
created

ExternalSign::createAppUrl()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 11

Duplication

Lines 27
Ratio 100 %

Code Coverage

Tests 9
CRAP Score 2.024

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 1
dl 27
loc 27
ccs 9
cts 11
cp 0.8182
crap 2.024
rs 8.8571
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 ExternalSign
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/externalsign';
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
     * Return the login information from the login.
41
     *
42
     * @param  string $documentId
43
     * @return object
44
     */
45 1
    public function getUrlForSign(string $documentId)
46
    {
47
        // make the URL for this request
48 1
        $url = $this->transformUrl(sprintf('%s/%s', self::URI, $documentId));
49
50
        // get the headers for this request
51 1
        $headers = $this->headers->make('GET', $url, [], true);
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
     * Get the URLs to a viewerapplet showing
64
     * documents in an iframe on website.
65
     *
66
     * @param  string $documentId
67
     * @param  array  $params
68
     * @return object
69
     */
70 1
    public function getUrlForApplet(string $documentId, array $params)
71
    {
72 1
        if (! isset($params['Domain']) || ! isset($params['Language'])) {
73 1
            throw new BadMethodCallException('Params should contain "Domain" and "Language" keys');
74
        }
75
76
        // make the URL for this request
77 1
        $url = $this->transformUrl(sprintf(
78 1
            '%s/ViewerUrl/%s/%s/%s',
79 1
            self::URI,
80 1
            $documentId,
81 1
            $params['Domain'],
82 1
            $params['Language']
83
        ));
84
85
        // get the headers for this request
86 1
        $headers = $this->headers->make('GET', $url, [], true);
87
88
        // get the response
89 1
        $response = $this->client->get($url, [
90 1
            'headers' => $headers,
91
        ]);
92
93
        // return the response
94 1
        return $response;
95
    }
96
97
    /**
98
     * Get status of BankId mobile sign session.
99
     *
100
     * @param  string $signeeRefId
101
     * @return object
102
     */
103 1
    public function getSessionStatus(string $signeeRefId)
104
    {
105
        // make the URL for this request
106 1
        $url = $this->transformUrl(sprintf('%s/BankIDMobileSign/Status/%s', self::URI, $signeeRefId));
107
108
        // get the headers for this request
109 1
        $headers = $this->headers->make('GET', $url, [], true);
110
111
        // get the response
112 1
        $response = $this->client->get($url, [
113 1
            'headers' => $headers,
114
        ]);
115
116
        // return the response
117 1
        return $response;
118
    }
119
120
    /**
121
     * Creates a externalsign request to integrate
122
     * signing of documents in a website.
123
     *
124
     * @param  array  $body
125
     * @return object
126
     */
127 3 View Code Duplication
    public function createRequest(array $body)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
128
    {
129
        // keys that are mandatory for this request
130
        $needKeys = [
131 3
            'Description',
132
            'ExternalDocumentId',
133
            'FileContent',
134
            'Filename',
135
            'ReturnUrlError',
136
            'ReturnUrlSuccess',
137
            'ReturnUrlUserAbort',
138
            'SigneeRefs',
139
            'Title',
140
        ];
141
142
        // keys that need to be present in each signeeref
143
        $needSubKeys = [
144 3
            'UniqueRef',
145
            'FirstName',
146
            'LastName',
147
            'Email',
148
        ];
149
150
        // if the body doesn't have needed fields, throw an exception
151 3
        if (! array_has_all_keys($body, $needKeys)) {
152 3
            throw new BadMethodCallException(
153 3
                'Missing fields in input array. Need '.implode(', ', $needKeys)
154
            );
155 1
        } elseif (! is_array($body['SigneeRefs'])) {
156
            throw new UnexpectedValueException('SigneeRefs key in input should be an array');
157
        } else {
158 1
            foreach ($body['SigneeRefs'] as $ref) {
159 1
                if (! is_array($ref)) {
160
                    throw new UnexpectedValueException('Each item in SigneeRefs should be an array');
161 1
                } elseif (! array_has_all_keys($ref, $needSubKeys)) {
162
                    throw new BadMethodCallException(
163 1
                        'Missing fields in SigneeRefs item. Need '.implode(', ', $needSubKeys)
164
                    );
165
                }
166
            }
167
        }
168
169
        // make the URL for this request
170 1
        $url = $this->transformUrl(self::URI);
171
172
        // get the headers for this request
173 1
        $headers = $this->headers->make('POST', $url, $body, true);
174
175
        // get the response
176 1
        $response = $this->client->post($url, [
177 1
            'headers' => $headers,
178 1
            'json' => $body,
179
        ]);
180
181
        // return the response
182 1
        return $response;
183
    }
184
185
    /**
186
     * Creates a app launch uri for the BankID app.
187
     *
188
     * @param  array  $body
189
     * @return object
190
     */
191 1 View Code Duplication
    public function createAppUrl(array $body)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
192
    {
193
        // keys that are mandatory for this request
194 1
        $needKeys = ['DocumentId', 'SigneeRefId', 'UserAgent'];
195
196
        // if the body doesn't have needed fields, throw an exception
197 1
        if (array_intersect(array_keys($body), $needKeys) !== $needKeys) {
198
            throw new BadMethodCallException(
199
                'Missing fields in input array. Need '.implode(', ', $needKeys)
200
            );
201
        }
202
203
        // make the URL for this request
204 1
        $url = $this->transformUrl(sprintf('%s/BankIDAppUrl', self::URI));
205
206
        // get the headers for this request
207 1
        $headers = $this->headers->make('PUT', $url, $body, true);
208
209
        // get the response
210 1
        $response = $this->client->put($url, [
211 1
            'headers' => $headers,
212 1
            'json' => $body,
213
        ]);
214
215
        // return the response
216 1
        return $response;
217
    }
218
219
    /**
220
     * Starts a BankID mobile sign session for the given document
221
     * with given mobilenumber and date of birth.
222
     *
223
     * @param  array  $body
224
     * @return object
225
     */
226 1 View Code Duplication
    public function startMobile(array $body)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
227
    {
228
        // keys that are mandatory for this request
229 1
        $needKeys = ['DateOfBirth', 'DocumentId', 'Mobile', 'SigneeRefId'];
230
231
        // if the body doesn't have needed fields, throw an exception
232 1
        if (array_intersect(array_keys($body), $needKeys) !== $needKeys) {
233
            throw new BadMethodCallException(
234
                'Missing fields in input array. Need '.implode(', ', $needKeys)
235
            );
236
        }
237
238
        // make the URL for this request
239 1
        $url = $this->transformUrl(sprintf('%s/BankIDMobileSign', self::URI));
240
241
        // get the headers for this request
242 1
        $headers = $this->headers->make('PUT', $url, $body, true);
243
244
        // get the response
245 1
        $response = $this->client->put($url, [
246 1
            'headers' => $headers,
247 1
            'json' => $body,
248
        ]);
249
250
        // return the response
251 1
        return $response;
252
    }
253
}
254