TokenGenerator::getToken()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
c 0
b 0
f 0
ccs 6
cts 6
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Xsolve\SalesforceClient\Generator;
4
5
use Xsolve\SalesforceClient\Security\Authentication\AuthenticatorInterface;
6
use Xsolve\SalesforceClient\Security\Authentication\Credentials;
7
use Xsolve\SalesforceClient\Security\Token\TokenInterface;
8
use Xsolve\SalesforceClient\Storage\TokenStorageInterface;
9
10
class TokenGenerator implements TokenGeneratorInterface
11
{
12
    /**
13
     * @var Credentials
14
     */
15
    protected $credentials;
16
17
    /**
18
     * @var AuthenticatorInterface
19
     */
20
    protected $authenticator;
21
22
    /**
23
     * @var TokenStorageInterface
24
     */
25
    protected $tokenStorage;
26
27
    /**
28
     * @param Credentials            $credentials
29
     * @param AuthenticatorInterface $authenticator
30
     * @param TokenStorageInterface  $tokenStorage
31
     */
32 3
    public function __construct(
33
        Credentials $credentials,
34
        AuthenticatorInterface $authenticator,
35
        TokenStorageInterface $tokenStorage
36
    ) {
37 3
        $this->credentials = $credentials;
38 3
        $this->authenticator = $authenticator;
39 3
        $this->tokenStorage = $tokenStorage;
40 3
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 2
    public function getToken(): TokenInterface
46
    {
47 2
        if ($this->tokenStorage->has()) {
48 1
            return $this->tokenStorage->get();
49
        }
50
51 1
        $token = $this->authenticator->authenticate($this->credentials);
52 1
        $this->tokenStorage->save($token);
53
54 1
        return $token;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 1
    public function regenerateToken(TokenInterface $token): TokenInterface
61
    {
62 1
        $newToken = $this->authenticator->regenerate($this->credentials, $token);
63 1
        $this->tokenStorage->save($newToken);
64
65 1
        return $newToken;
66
    }
67
}
68