KeyPairAuthentication::getToken()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
3
namespace VideoPublisher\Authentication;
4
5
use VideoPublisher\Connection\ConnectionInterface;
6
use VideoPublisher\Connection\ResponseException;
7
use VideoPublisher\Exception\TokenNotFoundException;
8
use VideoPublisher\Exception\UnableToCreateHashException;
9
use VideoPublisher\Payload\PayloadFactory;
10
use VideoPublisher\Security\Token;
11
use VideoPublisher\Security\TokenCreator;
12
use VideoPublisher\Security\TokenVault;
13
14
/**
15
 * Class KeyPairAuthentication.
16
 *
17
 * @author Bart Malestein <[email protected]>
18
 */
19
class KeyPairAuthentication implements Authentication
20
{
21
    /**
22
     * @var string
23
     */
24
    private $consumerKey;
25
26
    /**
27
     * @var string
28
     */
29
    private $privateKey;
30
31
    /**
32
     * @var ConnectionInterface
33
     */
34
    private $connection;
35
36
    /**
37
     * @var TokenVault
38
     */
39
    private $tokenVault;
40
41
    /**
42
     * @var PayloadFactory
43
     */
44
    private $payloadFactory;
45
46
    /**
47
     * KeyPairAuthentication constructor.
48
     * @param string $consumerKey - Your consumer key, can be found at http://my.isset.net/
49
     * @param string $privateKey - Your private key, can be found at http://my.isset.net/
50
     */
51
    public function __construct($consumerKey, $privateKey)
52
    {
53
        $this->consumerKey = $consumerKey;
54
        $this->privateKey = $privateKey;
55
    }
56
57
    /**
58
     * Inject services so we can handle the connection to login
59
     * @param ConnectionInterface $connection
60
     * @param TokenVault $tokenVault
61
     * @param PayloadFactory $payloadFactory
62
     */
63
    public function injectServices(ConnectionInterface $connection, TokenVault $tokenVault, PayloadFactory $payloadFactory)
64
    {
65
        $this->connection = $connection;
66
        $this->tokenVault = $tokenVault;
67
        $this->payloadFactory = $payloadFactory;
68
    }
69
70
    /**
71
     * @return Token
72
     * @throws ResponseException
73
     * @throws TokenNotFoundException
74
     * @throws UnableToCreateHashException
75
     */
76
    public function getToken()
77
    {
78
        if ($this->tokenVault->hasToken($this->consumerKey)) {
79
80
            return $this->tokenVault->getToken($this->consumerKey);
81
        }
82
        $tokenCreator = new TokenCreator($this->connection, $this->payloadFactory);
83
        $token = $tokenCreator->requestTokenFromMyIsset($this->consumerKey, $this->privateKey);
84
        $this->tokenVault->addToken($this->consumerKey, $token);
85
86
        return $token;
87
    }
88
89
90
}