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

ExternalSign::getSessionStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 16
ccs 6
cts 6
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Sausin\Signere;
4
5
use BadMethodCallException;
6
use UnexpectedValueException;
7
8
class ExternalSign extends BaseClass
9
{
10
    /** The URI of the action */
11
    const URI = 'https://api.signere.no/api/externalsign';
12
13
    /**
14
     * Return the login information from the login.
15
     *
16
     * @param  string $documentId
17
     * @return object
18
     */
19 1
    public function getUrlForSign(string $documentId)
20
    {
21
        // make the URL for this request
22 1
        $url = $this->transformUrl(sprintf('%s/%s', self::URI, $documentId));
23
24
        // get the headers for this request
25 1
        $headers = $this->headers->make('GET', $url, [], true);
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
     * Get the URLs to a viewerapplet showing
38
     * documents in an iframe on website.
39
     *
40
     * @param  string $documentId
41
     * @param  array  $params
42
     * @return object
43
     */
44 1
    public function getUrlForApplet(string $documentId, array $params)
45
    {
46 1
        if (! isset($params['Domain']) || ! isset($params['Language'])) {
47 1
            throw new BadMethodCallException('Params should contain "Domain" and "Language" keys');
48
        }
49
50
        // make the URL for this request
51 1
        $url = $this->transformUrl(sprintf(
52 1
            '%s/ViewerUrl/%s/%s/%s',
53 1
            self::URI,
54
            $documentId,
55 1
            $params['Domain'],
56 1
            $params['Language']
57
        ));
58
59
        // get the headers for this request
60 1
        $headers = $this->headers->make('GET', $url, [], true);
61
62
        // get the response
63 1
        $response = $this->client->get($url, [
64 1
            'headers' => $headers,
65
        ]);
66
67
        // return the response
68 1
        return $response;
69
    }
70
71
    /**
72
     * Get status of BankId mobile sign session.
73
     *
74
     * @param  string $signeeRefId
75
     * @return object
76
     */
77 1
    public function getSessionStatus(string $signeeRefId)
78
    {
79
        // make the URL for this request
80 1
        $url = $this->transformUrl(sprintf('%s/BankIDMobileSign/Status/%s', self::URI, $signeeRefId));
81
82
        // get the headers for this request
83 1
        $headers = $this->headers->make('GET', $url, [], true);
84
85
        // get the response
86 1
        $response = $this->client->get($url, [
87 1
            'headers' => $headers,
88
        ]);
89
90
        // return the response
91 1
        return $response;
92
    }
93
94
    /**
95
     * Creates a externalsign request to integrate
96
     * signing of documents in a website.
97
     *
98
     * @param  array  $body
99
     * @return object
100
     */
101 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...
102
    {
103
        // keys that are mandatory for this request
104
        $needKeys = [
105 3
            'Description',
106
            'ExternalDocumentId',
107
            'FileContent',
108
            'Filename',
109
            'ReturnUrlError',
110
            'ReturnUrlSuccess',
111
            'ReturnUrlUserAbort',
112
            'SigneeRefs',
113
            'Title',
114
        ];
115
116
        // keys that need to be present in each signeeref
117
        $needSubKeys = [
118 3
            'UniqueRef',
119
            'FirstName',
120
            'LastName',
121
            'Email',
122
        ];
123
124
        // if the body doesn't have needed fields, throw an exception
125 3
        if (! array_has_all_keys($body, $needKeys)) {
126 3
            throw new BadMethodCallException(
127 3
                'Missing fields in input array. Need '.implode(', ', $needKeys)
128
            );
129 1
        } elseif (! is_array($body['SigneeRefs'])) {
130
            throw new UnexpectedValueException('SigneeRefs key in input should be an array');
131
        } else {
132 1
            foreach ($body['SigneeRefs'] as $ref) {
133 1
                if (! is_array($ref)) {
134
                    throw new UnexpectedValueException('Each item in SigneeRefs should be an array');
135 1
                } elseif (! array_has_all_keys($ref, $needSubKeys)) {
136
                    throw new BadMethodCallException(
137 1
                        'Missing fields in SigneeRefs item. Need '.implode(', ', $needSubKeys)
138
                    );
139
                }
140
            }
141
        }
142
143
        // make the URL for this request
144 1
        $url = $this->transformUrl(self::URI);
145
146
        // get the headers for this request
147 1
        $headers = $this->headers->make('POST', $url, $body, true);
148
149
        // get the response
150 1
        $response = $this->client->post($url, [
151 1
            'headers' => $headers,
152 1
            'json' => $body,
153
        ]);
154
155
        // return the response
156 1
        return $response;
157
    }
158
159
    /**
160
     * Creates a app launch uri for the BankID app.
161
     *
162
     * @param  array  $body
163
     * @return object
164
     */
165 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...
166
    {
167
        // keys that are mandatory for this request
168 1
        $needKeys = ['DocumentId', 'SigneeRefId', 'UserAgent'];
169
170
        // if the body doesn't have needed fields, throw an exception
171 1
        if (array_intersect(array_keys($body), $needKeys) !== $needKeys) {
172
            throw new BadMethodCallException(
173
                'Missing fields in input array. Need '.implode(', ', $needKeys)
174
            );
175
        }
176
177
        // make the URL for this request
178 1
        $url = $this->transformUrl(sprintf('%s/BankIDAppUrl', self::URI));
179
180
        // get the headers for this request
181 1
        $headers = $this->headers->make('PUT', $url, $body, true);
182
183
        // get the response
184 1
        $response = $this->client->put($url, [
185 1
            'headers' => $headers,
186 1
            'json' => $body,
187
        ]);
188
189
        // return the response
190 1
        return $response;
191
    }
192
193
    /**
194
     * Starts a BankID mobile sign session for the given document
195
     * with given mobilenumber and date of birth.
196
     *
197
     * @param  array  $body
198
     * @return object
199
     */
200 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...
201
    {
202
        // keys that are mandatory for this request
203 1
        $needKeys = ['DateOfBirth', 'DocumentId', 'Mobile', 'SigneeRefId'];
204
205
        // if the body doesn't have needed fields, throw an exception
206 1
        if (array_intersect(array_keys($body), $needKeys) !== $needKeys) {
207
            throw new BadMethodCallException(
208
                'Missing fields in input array. Need '.implode(', ', $needKeys)
209
            );
210
        }
211
212
        // make the URL for this request
213 1
        $url = $this->transformUrl(sprintf('%s/BankIDMobileSign', self::URI));
214
215
        // get the headers for this request
216 1
        $headers = $this->headers->make('PUT', $url, $body, true);
217
218
        // get the response
219 1
        $response = $this->client->put($url, [
220 1
            'headers' => $headers,
221 1
            'json' => $body,
222
        ]);
223
224
        // return the response
225 1
        return $response;
226
    }
227
}
228