1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Fns\Auth; |
5
|
|
|
|
6
|
|
|
use Psr\SimpleCache\CacheInterface; |
7
|
|
|
use SoapClient; |
8
|
|
|
use UnexpectedValueException; |
9
|
|
|
|
10
|
|
|
class AuthRequest |
11
|
|
|
{ |
12
|
|
|
private $wsdl = 'https://openapi.nalog.ru:8090/open-api/AuthService/0.1?wsdl'; |
13
|
|
|
private $client; |
14
|
|
|
private $masterToken; |
15
|
|
|
private $xmlResponse; |
16
|
|
|
private $storage; |
17
|
|
|
private $nameToken = 'temp_token'; |
18
|
|
|
|
19
|
|
|
public function __construct(string $masterToken, CacheInterface $cache) |
20
|
|
|
{ |
21
|
|
|
$this->masterToken = $masterToken; |
22
|
|
|
$this->storage = $cache; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
private function exceptionGetToken() |
26
|
|
|
{ |
27
|
|
|
throw new UnexpectedValueException('Temp token has not got'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
private function getBodyXml() : array |
31
|
|
|
{ |
32
|
|
|
return [[ |
33
|
|
|
'Message' => [ |
34
|
|
|
'any' => "<tns:AuthRequest xmlns:tns=\"urn://x-artefacts-gnivc-ru/ais3/kkt/AuthService/types/1.0\"> |
35
|
|
|
<tns:AuthAppInfo> |
36
|
|
|
<tns:MasterToken>{$this->masterToken}</tns:MasterToken> |
37
|
|
|
</tns:AuthAppInfo> |
38
|
|
|
</tns:AuthRequest>" |
39
|
|
|
] |
40
|
|
|
]]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function setToken(string $token, string $expireTime) : bool |
44
|
|
|
{ |
45
|
|
|
if ($this->isTokenExist() === false) { |
46
|
|
|
return $this->storage->set($this->nameToken, $token, strtotime($expireTime) - time()); |
47
|
|
|
} |
48
|
|
|
return true; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function authenticate() : void |
52
|
|
|
{ |
53
|
|
|
$response = $this->client->__soapCall("GetMessage", $this->getBodyXml()); |
54
|
|
|
|
55
|
|
|
$this->xmlResponse = simplexml_load_string($response->Message->any, AuthXmlResponse::class); |
56
|
|
|
|
57
|
|
|
if ($this->xmlResponse->isError()) { |
|
|
|
|
58
|
|
|
$this->exceptionGetToken(); |
59
|
|
|
} |
60
|
|
|
$this->setToken($this->xmlResponse->getToken(), $this->xmlResponse->getTime()); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function isTokenExist() : bool |
64
|
|
|
{ |
65
|
|
|
return $this->storage->has($this->nameToken); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getTempToken() : string |
69
|
|
|
{ |
70
|
|
|
if ($this->isTokenExist()) { |
71
|
|
|
return $this->storage->get($this->nameToken); |
72
|
|
|
} |
73
|
|
|
return ''; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getSoapClient() : SoapClient |
77
|
|
|
{ |
78
|
|
|
return $this->client; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function setSoapClient(SoapClient $client = null) |
82
|
|
|
{ |
83
|
|
|
if (is_null($client)) { |
84
|
|
|
$client = new SoapClient($this->wsdl); |
85
|
|
|
} |
86
|
|
|
$this->client = $client; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|