1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bunq\Service; |
4
|
|
|
|
5
|
|
|
use Bunq\Certificate\CertificateType; |
6
|
|
|
use Bunq\Certificate\DefaultCertificate; |
7
|
|
|
use Bunq\Certificate\Storage\CertificateStorage; |
8
|
|
|
use Bunq\Token\DefaultToken; |
9
|
|
|
use Bunq\Token\Storage\TokenNotFoundException; |
10
|
|
|
use Bunq\Token\Storage\TokenStorage; |
11
|
|
|
use Bunq\Token\Token; |
12
|
|
|
use Bunq\Token\TokenType; |
13
|
|
|
|
14
|
|
|
final class DefaultTokenService implements TokenService |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var InstallationService |
18
|
|
|
*/ |
19
|
|
|
private $installation; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var TokenStorage |
23
|
|
|
*/ |
24
|
|
|
private $tokenStorage; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var CertificateStorage |
28
|
|
|
*/ |
29
|
|
|
private $certificateStorage; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param InstallationService $installation |
33
|
|
|
* @param TokenStorage $tokenStorage |
34
|
|
|
* @param CertificateStorage $certificateStorage |
35
|
|
|
*/ |
36
|
|
|
public function __construct( |
37
|
|
|
InstallationService $installation, |
38
|
|
|
TokenStorage $tokenStorage, |
39
|
|
|
CertificateStorage $certificateStorage |
40
|
|
|
) { |
41
|
|
|
$this->installation = $installation; |
42
|
|
|
$this->tokenStorage = $tokenStorage; |
43
|
|
|
$this->certificateStorage = $certificateStorage; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return Token |
48
|
|
|
*/ |
49
|
|
|
public function sessionToken() |
50
|
|
|
{ |
51
|
|
|
try { |
52
|
|
|
return $this->tokenStorage->load(TokenType::SESSION_TOKEN()); |
53
|
|
|
} catch (TokenNotFoundException $exception) { |
54
|
|
|
return $this->obtainNewSessionToken(); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return Token |
60
|
|
|
*/ |
61
|
|
|
private function obtainNewSessionToken() |
62
|
|
|
{ |
63
|
|
|
$sessionResponse = $this->installation->createSession($this->installationToken()); |
64
|
|
|
$sessionToken = DefaultToken::fromString($sessionResponse); |
|
|
|
|
65
|
|
|
|
66
|
|
|
$this->tokenStorage->save($sessionToken, TokenType::SESSION_TOKEN()); |
67
|
|
|
|
68
|
|
|
return $sessionToken; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return Token |
73
|
|
|
*/ |
74
|
|
|
private function installationToken() |
75
|
|
|
{ |
76
|
|
|
try { |
77
|
|
|
return $this->tokenStorage->load(TokenType::INSTALLATION_TOKEN()); |
78
|
|
|
} catch (TokenNotFoundException $exception) { |
79
|
|
|
$token = $this->obtainNewInstallationToken(); |
80
|
|
|
|
81
|
|
|
//registers the device |
82
|
|
|
$this->installation->registerDevice($token); |
83
|
|
|
|
84
|
|
|
return $token; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return Token |
90
|
|
|
*/ |
91
|
|
|
private function obtainNewInstallationToken() |
92
|
|
|
{ |
93
|
|
|
$installationResponse = $this->installation->install(); |
94
|
|
|
|
95
|
|
|
$installationToken = DefaultToken::fromString($installationResponse['token']); |
96
|
|
|
$this->tokenStorage->save($installationToken, TokenType::INSTALLATION_TOKEN()); |
97
|
|
|
|
98
|
|
|
$certificate = DefaultCertificate::fromString($installationResponse['public_key']); |
99
|
|
|
$this->certificateStorage->save($certificate, CertificateType::BUNQ_SERVER_KEY()); |
100
|
|
|
|
101
|
|
|
return $installationToken; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: