Completed
Push — master ( 96dc87...3752b6 )
by Saurabh
12:32 queued 10:52
created

ExternalSign   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 237
Duplicated Lines 46.84 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
dl 111
loc 237
ccs 63
cts 70
cp 0.9
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getUrlForSign() 0 16 1
B getUrlForApplet() 0 26 3
A getSessionStatus() 0 16 1
B createRequest() 57 57 6
B createAppUrl() 27 27 2
B startMobile() 27 27 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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