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