-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathFileTokensStorageApi.php
117 lines (97 loc) · 3.01 KB
/
FileTokensStorageApi.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* PHP SDK for Cardpay API v3. All rights reserved.
*/
namespace Cardpay\api;
use Cardpay\ApiException;
use Cardpay\Configuration;
use Cardpay\model\ApiTokens;
class FileTokensStorageApi implements TokensStorageApi
{
const API_TOKENS_TMP_FILENAME_PREFIX = 'cardpay_apiv3_tokens_';
private $apiTokensTempFilepath;
/**
* FileTokensStorageApi constructor
* @param $host
* @param $terminalCode
* @throws ApiException
*/
public function __construct($host, $terminalCode)
{
$this->validateInputParams($terminalCode);
$this->terminalCode = $terminalCode;
$configuration = new Configuration($host);
$tempFolderPath = $configuration->getTempFolderPath();
$this->apiTokensTempFilepath = $tempFolderPath . DIRECTORY_SEPARATOR . self::API_TOKENS_TMP_FILENAME_PREFIX . $terminalCode;
}
/**
* @param $terminalCode
* @throws ApiException
*/
private function validateInputParams($terminalCode)
{
if (empty($terminalCode)) {
throw new ApiException('Empty terminal code');
}
}
/**
* @param ApiTokens $apiTokens
* @return bool
* @throws ApiException
*/
public function saveApiTokens(ApiTokens $apiTokens)
{
$serializedApiTokens = serialize($apiTokens);
/** @var bool $areTokensSaved */
$areTokensSaved = file_put_contents($this->apiTokensTempFilepath, $serializedApiTokens);
if (false === $areTokensSaved) {
throw new ApiException('Unable to write API tokens to temporary file');
}
return true;
}
/**
* @return bool
*/
public function areApiTokensSaved()
{
return file_exists($this->apiTokensTempFilepath);
}
/**
* @return ApiTokens|mixed
* @throws ApiException
*/
public function readApiTokens()
{
$apiTokensFileContents = file_get_contents($this->apiTokensTempFilepath);
if (false === $apiTokensFileContents) {
throw new ApiException('Unable to read API tokens from temporary file');
}
if (empty($apiTokensFileContents)) {
throw new ApiException('Empty API tokens file');
}
$apiTokens = unserialize($apiTokensFileContents);
if (false === $apiTokens) {
throw new ApiException('Unable to unserialize API tokens');
}
if (false === ($apiTokens instanceof ApiTokens)) {
throw new ApiException('Invalid temporary API tokens file');
}
return $apiTokens;
}
/**
* @return bool
* @throws ApiException
*/
public function deleteApiTokens()
{
if (false === $this->areApiTokensSaved()) {
return false;
}
/** @var bool $isTempFileDeleted */
$isTempFileDeleted = unlink($this->apiTokensTempFilepath);
if (false === $isTempFileDeleted) {
throw new ApiException("Unable to delete temporary API tokens file");
}
return true;
}
}