Skip to content

Commit 028ec92

Browse files
committed
app 证书
1 parent 982ffca commit 028ec92

File tree

7 files changed

+192
-55
lines changed

7 files changed

+192
-55
lines changed

src/AppConfig.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Wu\Agora;
4+
5+
class AppConfig
6+
{
7+
/**
8+
* @var string
9+
*/
10+
private $appID;
11+
12+
/**
13+
* @var string
14+
*/
15+
private $appCertificate;
16+
17+
/**
18+
* @return string
19+
*/
20+
public function getAppID(): string
21+
{
22+
return $this->appID;
23+
}
24+
25+
/**
26+
* @param string $appID
27+
* @return AppConfig
28+
*/
29+
public function setAppID(string $appID): AppConfig
30+
{
31+
$this->appID = $appID;
32+
return $this;
33+
}
34+
35+
/**
36+
* @return string
37+
*/
38+
public function getAppCertificate(): string
39+
{
40+
return $this->appCertificate;
41+
}
42+
43+
/**
44+
* @param string $appCertificate
45+
* @return AppConfig
46+
*/
47+
public function setAppCertificate(string $appCertificate): AppConfig
48+
{
49+
$this->appCertificate = $appCertificate;
50+
return $this;
51+
}
52+
}

src/Config.php

Lines changed: 127 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,68 @@
77

88
class Config
99
{
10-
protected $appID;
10+
/**
11+
* @var string app区分
12+
*/
13+
private $appSource = 'default';
1114

12-
protected $appCertificate;
15+
/**
16+
* @var AppConfig[] app 证书
17+
*/
18+
private $appKeyArray = [];
1319

20+
/**
21+
* @var string token版本
22+
*/
1423
protected $version = "006";
1524

1625
/**
17-
* @var Carbon
26+
* @var string api管理key
1827
*/
19-
public $date;
28+
protected $customerKey;
2029

30+
/**
31+
* @var string api管理密钥
32+
*/
33+
protected $customerSecret;
34+
35+
/**
36+
* @var int api请求超时时间
37+
*/
2138
protected $timeOut = 2;
2239

40+
/**
41+
* @var int api请求重试次数
42+
*/
43+
protected $retry = 1;
44+
45+
/**
46+
* @var null guzzle请求handler
47+
*/
2348
protected $handler;
2449

25-
protected $retry = 1;
2650

27-
public function __construct(string $appID = '', string $appCertificate = '')
51+
public function __construct(string $appID = '', string $appCertificate = '', string $customerKey = '', string $customerSecret = '')
2852
{
29-
$this->appID = $appID;
30-
$this->appCertificate = $appCertificate;
53+
$appConfig = new AppConfig();
54+
if ($appID || $appCertificate) {
55+
$appConfig->setAppID($appID);
56+
$appConfig->setAppCertificate($appCertificate);
57+
$this->appKeyArray[$this->appSource] = $appConfig;
58+
}
59+
$this->customerKey = $customerKey;
60+
$this->customerSecret = $customerSecret;
3161
}
3262

3363
/**
3464
* @throws AgoraException
3565
*/
3666
public function checkConfig()
3767
{
38-
if (! $this->appID) {
68+
if (! $this->getAppID()) {
3969
throw new AgoraException("appID check failed, should be a non-empty string");
4070
}
41-
if (! $this->appCertificate) {
71+
if (! $this->getAppCertificate()) {
4272
throw new AgoraException("appCertificate check failed, should be a non-empty string");
4373
}
4474
}
@@ -48,35 +78,23 @@ public function checkConfig()
4878
*/
4979
public function getAppID(): string
5080
{
51-
return $this->appID;
81+
return $this->appKeyArray[$this->appSource]->getAppID();
5282
}
5383

5484
/**
5585
* @return string
5686
*/
5787
public function getAppCertificate(): string
5888
{
59-
return $this->appCertificate;
89+
return $this->appKeyArray[$this->appSource]->getAppCertificate();
6090
}
6191

6292
/**
63-
* @param string $appID
64-
* @return Config
65-
*/
66-
public function setAppID(string $appID): Config
67-
{
68-
$this->appID = $appID;
69-
return $this;
70-
}
71-
72-
/**
73-
* @param string $appCertificate
74-
* @return Config
93+
* @return string
7594
*/
76-
public function setAppCertificate(string $appCertificate): Config
95+
public function getVersion(): string
7796
{
78-
$this->appCertificate = $appCertificate;
79-
return $this;
97+
return $this->version;
8098
}
8199

82100
/**
@@ -89,34 +107,39 @@ public function setVersion(string $version): Config
89107
return $this;
90108
}
91109

92-
93-
94110
/**
95111
* @return string
96112
*/
97-
public function getVersion(): string
98-
{
99-
return $this->version;
100-
}
101-
102-
public function getVersionLen(): int
113+
public function getCustomerKey(): string
103114
{
104-
return strlen($this->version);
115+
return $this->customerKey;
105116
}
106117

107-
public function getAppIdLen(): int
118+
/**
119+
* @param string $customerKey
120+
* @return Config
121+
*/
122+
public function setCustomerKey(string $customerKey): Config
108123
{
109-
return strlen($this->appID);
124+
$this->customerKey = $customerKey;
125+
return $this;
110126
}
111127

112-
public function getHandler()
128+
/**
129+
* @return string
130+
*/
131+
public function getCustomerSecret(): string
113132
{
114-
return $this->handler;
133+
return $this->customerSecret;
115134
}
116135

117-
public function setHandler(?callable $handler)
136+
/**
137+
* @param string $customerSecret
138+
* @return Config
139+
*/
140+
public function setCustomerSecret(string $customerSecret): Config
118141
{
119-
$this->handler = $handler;
142+
$this->customerSecret = $customerSecret;
120143
return $this;
121144
}
122145

@@ -155,4 +178,66 @@ public function setRetry(int $retry): Config
155178
$this->retry = $retry;
156179
return $this;
157180
}
181+
182+
183+
public function getHandler()
184+
{
185+
return $this->handler;
186+
}
187+
188+
/**
189+
* @param $handler
190+
* @return Config
191+
*/
192+
public function setHandler($handler): Config
193+
{
194+
$this->handler = $handler;
195+
return $this;
196+
}
197+
198+
/**
199+
* @return string
200+
*/
201+
public function getAppSource(): string
202+
{
203+
return $this->appSource;
204+
}
205+
206+
/**
207+
* @param string $appSource
208+
* @return Config
209+
*/
210+
public function setAppSource(string $appSource): Config
211+
{
212+
$this->appSource = $appSource;
213+
return $this;
214+
}
215+
216+
/**
217+
* @return AppConfig[]
218+
*/
219+
public function getAppKeyArray(): array
220+
{
221+
return $this->appKeyArray;
222+
}
223+
224+
/**
225+
* @param AppConfig[] $appKeyArray
226+
* @return Config
227+
*/
228+
public function setAppKeyArray(array $appKeyArray): Config
229+
{
230+
$this->appKeyArray = $appKeyArray;
231+
return $this;
232+
}
233+
234+
public function getVersionLen()
235+
{
236+
return strlen($this->version);
237+
}
238+
239+
public function getAppIdLen()
240+
{
241+
return strlen($this->getAppID());
242+
}
158243
}

src/Http.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ public function __construct(Config $config)
2323
*/
2424
public function authorization()
2525
{
26-
$appId = $this->config->getAppID();
27-
$customerSecret = $this->config->getAppCertificate();
28-
$credentials = $appId . ":" . $customerSecret;
26+
$customerKey = $this->config->getCustomerKey();
27+
$customerSecret = $this->config->getCustomerSecret();
28+
$credentials = $customerKey . ":" . $customerSecret;
2929
return "Basic " . base64_encode($credentials);
3030
}
3131

src/Token/AccessToken.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,17 @@ public function setChannelName($channelName)
7676
return $this;
7777
}
7878

79+
/**
80+
* @param Config $config
81+
* @param null $message
82+
* @return AccessToken
83+
* @throws AgoraException
84+
*/
7985
public static function make(Config $config, $message = null)
8086
{
8187
$accessToken = new AccessToken();
8288
$accessToken->config = $config;
89+
$config->checkConfig();
8390
if (! ($message instanceof Message)) {
8491
$message = new Message();
8592
}

src/Token/TokenBuilder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Wu\Agora\Token;
44

55
use Wu\Agora\Config;
6+
use Wu\Agora\Exception\AgoraException;
67

78
class TokenBuilder
89
{
@@ -51,7 +52,7 @@ public function rtmTokenBuilder($uidOrAccount, $privilegeExpireTs)
5152
/**
5253
* @param $token
5354
* @return Message
54-
* @throws Exception\AgoraException
55+
* @throws AgoraException
5556
*/
5657
public function messageByToken($token): Message
5758
{

test/BaseTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function getConfig()
1515
{
1616
$dotenv = Dotenv::createImmutable(__DIR__ . "/..");
1717
$dotenv->safeLoad();
18-
return new Config($_ENV['APP_ID'], $_ENV['APP_KEY']);
18+
return new Config($_ENV['APP_ID'], $_ENV['APP_CERTIFICATE'], $_ENV['CUSTOMER_KEY'], $_ENV['CUSTOMER_SECRET']);
1919
}
2020

2121
public function getModel()

test/TokenTest.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,6 @@
1010

1111
class TokenTest extends BaseTest
1212
{
13-
/**
14-
* @return Config
15-
*/
16-
public function getConfig()
17-
{
18-
return new Config('970CA35de60c44645bbae8a215061b33', '5CFd2fd1755d40ecb72977518be15d3b');
19-
}
20-
2113
public function testCreate()
2214
{
2315
$channelName = "hdkhfdkasjhfakhf";

0 commit comments

Comments
 (0)