ExternalLogin   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 153
Duplicated Lines 30.07 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 97.73%

Importance

Changes 0
Metric Value
dl 46
loc 153
ccs 43
cts 44
cp 0.9773
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getLoginInfo() 0 16 1
A createAppLaunchUri() 23 23 1
A createMobile() 23 23 1
A startMobileSession() 0 23 3
B invalidateLogin() 0 25 3

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 BadMethodCallException;
6
use UnexpectedValueException;
7
8
class ExternalLogin extends BaseClass
9
{
10
    /** The URI of the action */
11
    const URI = 'https://api.signere.no/api/ExternalLogin';
12
13
    /**
14
     * Return the login information from the login.
15
     *
16
     * @param  string $requestId
17
     * @return object
18
     */
19 1
    public function getLoginInfo(string $requestId)
20
    {
21
        // make the URL for this request
22 1
        $url = sprintf('%s/%s', $this->getBaseUrl(), $requestId);
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
     * Creates a app login response which contains
38
     * the launchuri to launch the BankID app.
39
     *
40
     * @param  array  $body
41
     * @return object
42
     */
43 1 View Code Duplication
    public function createAppLaunchUri(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...
44
    {
45
        // keys that are mandatory for this request
46 1
        $needKeys = ['ExternalId', 'ReturnUrl'];
47
48
        // if the body doesn't have needed fields, throw an exception
49 1
        $this->validateHasKeys($body, $needKeys);
50
51
        // make the URL for this request
52 1
        $url = sprintf('%s/AppLogin', $this->getBaseUrl());
53
54
        // get the headers for this request
55 1
        $headers = $this->headers->make('POST', $url, $body);
56
57
        // get the response
58 1
        $response = $this->client->post($url, [
59 1
            'headers' => $headers,
60 1
            'json' => $body,
61
        ]);
62
63
        // return the response
64 1
        return $response;
65
    }
66
67
    /**
68
     * Creates a BankID mobile login response.
69
     *
70
     * @param  array  $body
71
     * @return object
72
     */
73 1 View Code Duplication
    public function createMobile(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...
74
    {
75
        // keys that are mandatory for this request
76 1
        $needKeys = ['DateOfBirth', 'Mobile'];
77
78
        // if the body doesn't have needed fields, throw an exception
79 1
        $this->validateHasKeys($body, $needKeys);
80
81
        // make the URL for this request
82 1
        $url = sprintf('%s/BankIDMobileLogin/Create', $this->getBaseUrl());
83
84
        // get the headers for this request
85 1
        $headers = $this->headers->make('POST', $url, $body);
86
87
        // get the response
88 1
        $response = $this->client->post($url, [
89 1
            'headers' => $headers,
90 1
            'json' => $body,
91
        ]);
92
93
        // return the response
94 1
        return $response;
95
    }
96
97
    /**
98
     * Starts the BankID mobile session that
99
     * was created by the 'create' method.
100
     *
101
     * @param  array  $body
102
     * @return object
103
     */
104 1
    public function startMobileSession(array $body = [])
105
    {
106
        // let the user know that if he is sending some data
107
        // it should be RequestId. Nothing else goes here.
108 1
        if (! empty($body) && ! isset($body['RequestId'])) {
109 1
            throw new UnexpectedValueException('Input should only have RequestId');
110
        }
111
112
        // make the URL for this request
113 1
        $url = sprintf('%s/BankIDMobileLogin/Start', $this->getBaseUrl());
114
115
        // get the headers for this request
116 1
        $headers = $this->headers->make('POST', $url, $body);
117
118
        // get the response
119 1
        $response = $this->client->post($url, [
120 1
            'headers' => $headers,
121 1
            'json' => $body,
122
        ]);
123
124
        // return the response
125 1
        return $response;
126
    }
127
128
    /**
129
     * Invalidate the login request to prevent
130
     * any replay attacks.
131
     *
132
     * @param  array  $body
133
     * @return object
134
     */
135 1
    public function invalidateLogin(array $body)
136
    {
137
        // let the user know that if he is sending some data
138
        // it should be RequestId. Nothing else goes here.
139 1
        if (! isset($body['RequestId'])) {
140 1
            throw new BadMethodCallException('Input should have RequestId');
141 1
        } elseif (count($body) > 1) {
142
            throw new UnexpectedValueException('Input should only have RequestId');
143
        }
144
145
        // make the URL for this request
146 1
        $url = sprintf('%s/InvalidateLogin', $this->getBaseUrl());
147
148
        // get the headers for this request
149 1
        $headers = $this->headers->make('PUT', $url, $body);
150
151
        // get the response
152 1
        $response = $this->client->put($url, [
153 1
            'headers' => $headers,
154 1
            'json' => $body,
155
        ]);
156
157
        // return the response
158 1
        return $response;
159
    }
160
}
161