Completed
Push — master ( eac57c...9ff768 )
by Laurens
02:04
created

AccessTokenFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 67
Duplicated Lines 40.3 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 27
loc 67
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getFromUserLogin() 14 14 1
A refresh() 13 13 1
A makeRequest() 0 10 1
A getAccessTokenFromData() 0 8 1

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
declare(strict_types=1);
4
5
namespace LauLamanApps\iZettleApi\Client;
6
7
use DateTime;
8
use GuzzleHttp\Client as GuzzleClient;
9
10
final class AccessTokenFactory
11
{
12
    const API_ACCESS_TOKEN_REQUEST_URL = 'https://oauth.izettle.net/token';
13
    const API_ACCESS_TOKEN_PASSWORD_GRANT = 'password';
14
    const API_ACCESS_TOKEN_REFRESH_TOKEN_URL = 'https://oauth.izettle.net/token';
15
    const API_ACCESS_TOKEN_REFRESH_TOKEN_GRANT = 'refresh_token';
16
17
    private $guzzleClient;
18
    private $clientId;
19
    private $clientSecret;
20
21 2
    public function __construct(GuzzleClient $guzzleClient, string $clientId, string $clientSecret)
22
    {
23 2
        $this->guzzleClient = $guzzleClient;
24 2
        $this->clientId = $clientId;
25 2
        $this->clientSecret = $clientSecret;
26 2
    }
27
28 1 View Code Duplication
    public function getFromUserLogin(string $username, string $password): AccessToken
29
    {
30
        $options = [
31
            'form_params' => [
32 1
                'grant_type' => self::API_ACCESS_TOKEN_PASSWORD_GRANT,
33 1
                'client_id' => $this->clientId,
34 1
                'client_secret' => $this->clientSecret,
35 1
                'username' => $username,
36 1
                'password' => $password
37
            ],
38
        ];
39
40 1
        return $this->getAccessTokenFromData($this->makeRequest(self::API_ACCESS_TOKEN_REQUEST_URL, $options));
41
    }
42
43 1 View Code Duplication
    public function refresh(AccessToken $accessToken): AccessToken
44
    {
45
        $options = [
46
            'form_params' => [
47 1
                'grant_type' => self::API_ACCESS_TOKEN_REFRESH_TOKEN_GRANT,
48 1
                'client_id' => $this->clientId,
49 1
                'client_secret' => $this->clientSecret,
50 1
                'refresh_token' => $accessToken->getRefreshToken()
51
            ],
52
        ];
53
54 1
        return $this->getAccessTokenFromData($this->makeRequest(self::API_ACCESS_TOKEN_REFRESH_TOKEN_URL, $options));
55
    }
56
57 2
    private function makeRequest($url, $options): array
58
    {
59 2
        $headers = ['headers' => ['Content-Type' => 'application/x-www-form-urlencoded']];
60 2
        $options = array_merge($headers, $options);
61
62 2
        $response = $this->guzzleClient->request('POST', $url, $options);
63 2
        $data = json_decode($response->getBody()->getContents(), true);
64
65 2
        return $data;
66
    }
67
68 2
    private function getAccessTokenFromData(array $data): AccessToken
69
    {
70 2
        return new AccessToken(
71 2
            $data['access_token'],
72 2
            new DateTime(sprintf('+%d second', $data['expires_in'])),
73 2
            $data['refresh_token']
74
        );
75
    }
76
}
77