iDokladTokenFactory   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 98
Duplicated Lines 7.14 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 7
dl 7
loc 98
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A getToken() 0 8 3
A createToken() 0 15 1
B extractToken() 7 28 6

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 Fousky\Component\iDoklad;
4
5
use Fousky\Component\iDoklad\Exception\InvalidTokenException;
6
use Fousky\Component\iDoklad\Model\Auth\AccessToken;
7
use Fousky\Component\iDoklad\Storage\AccessTokenStorageInterface;
8
use Fousky\Component\iDoklad\Storage\AccessTokenVoidStorage;
9
use GuzzleHttp\Client;
10
use Psr\Http\Message\ResponseInterface;
11
12
/**
13
 * @author Lukáš Brzák <[email protected]>
14
 */
15
class iDokladTokenFactory
16
{
17
    /** @var AccessTokenStorageInterface $storage */
18
    protected $storage;
19
20
    /**
21
     * @param AccessTokenStorageInterface $storage
22
     */
23
    public function __construct(AccessTokenStorageInterface $storage = null)
24
    {
25
        if (null === $storage) {
26
            $storage = new AccessTokenVoidStorage();
27
        }
28
29
        $this->storage = $storage;
30
    }
31
32
    /**
33
     * @param Client $client
34
     * @param array  $config
35
     *
36
     * @throws \RuntimeException
37
     * @throws InvalidTokenException
38
     *
39
     * @return AccessToken
40
     */
41
    public function getToken(Client $client, array $config): AccessToken
42
    {
43
        if ($this->storage->hasToken() && !$this->storage->isTokenExpired()) {
44
            return $this->storage->getToken();
45
        }
46
47
        return $this->createToken($client, $config);
48
    }
49
50
    /**
51
     * @param Client $client
52
     * @param array  $config
53
     *
54
     * @throws \RuntimeException
55
     * @throws InvalidTokenException
56
     *
57
     * @return AccessToken
58
     */
59
    protected function createToken(Client $client, array $config): AccessToken
60
    {
61
        return $this->extractToken(
62
            $client->request('POST', $config['token_endpoint'], [
63
                'form_params' => [
64
                    'scope' => $config['scope'],
65
                    'grant_type' => 'client_credentials',
66
                    'client_id' => $config['client_id'],
67
                    'client_secret' => $config['client_secret'],
68
                ],
69
                'debug' => (bool) $config['debug'],
70
            ]),
71
            new \DateTime()
72
        );
73
    }
74
75
    /**
76
     * @param ResponseInterface $response
77
     * @param \DateTime         $requestedAt
78
     *
79
     * @throws \RuntimeException
80
     * @throws InvalidTokenException
81
     *
82
     * @return AccessToken
83
     */
84
    protected function extractToken(ResponseInterface $response, \DateTime $requestedAt): AccessToken
85
    {
86
        if (200 !== $response->getStatusCode()) {
87
            throw new InvalidTokenException($response, 'ERR_IDOKLAD___INVALID_TOKEN_CALL');
88
        }
89
90
        $content = \GuzzleHttp\json_decode($response->getBody()->getContents());
91
        $content = (array) $content;
92
93 View Code Duplication
        if (!\is_array($content) ||
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
94
            !array_key_exists('access_token', $content) ||
95
            !array_key_exists('expires_in', $content) ||
96
            !array_key_exists('token_type', $content)
97
        ) {
98
            throw new InvalidTokenException($response, 'ERR_IDOKLAD___INVALID_TOKEN_RESPONSE');
99
        }
100
101
        $token = new AccessToken(
102
            (string) $content['access_token'],
103
            (int) $content['expires_in'],
104
            (string) $content['token_type'],
105
            $requestedAt
106
        );
107
108
        $this->storage->setToken($token);
109
110
        return $token;
111
    }
112
}
113