Skip to content

Commit 0693e1e

Browse files
committed
perf: Perf clients
1 parent 7a9c56d commit 0693e1e

4 files changed

Lines changed: 56 additions & 18 deletions

File tree

src/Clients/ChanifyClient.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,47 @@ class ChanifyClient extends Client
2323
protected $defined = [
2424
'token',
2525
'message',
26-
'base_uri',
26+
'baseUri',
2727
];
2828

2929
/**
3030
* @var string[]
3131
*/
3232
protected $options = [
33-
'base_uri' => 'https://api.chanify.net/v1/sender',
33+
'baseUri' => 'https://api.chanify.net/v1/sender',
3434
];
3535

3636
protected function configureOptionsResolver(OptionsResolver $resolver): OptionsResolver
3737
{
3838
$resolver = parent::configureOptionsResolver($resolver);
3939

4040
return tap($resolver, function (OptionsResolver $resolver) {
41-
$resolver->setNormalizer('base_uri', function (Options $options, $value) {
41+
$resolver->setNormalizer('baseUri', function (Options $options, $value) {
4242
return trim($value, '/');
4343
});
4444
});
4545
}
4646

47+
/**
48+
* @return $this
49+
*/
50+
public function setBaseUri(string $baseUri)
51+
{
52+
$this->setOption('baseUri', $baseUri);
53+
54+
return $this;
55+
}
56+
57+
public function getBaseUri(): string
58+
{
59+
return $this->getOptions('baseUri');
60+
}
61+
4762
public function getRequestUrl(): string
4863
{
49-
return sprintf(static::REQUEST_URL_TEMPLATE, $this->getOptions('base_uri'), $this->getToken());
64+
return sprintf(
65+
static::REQUEST_URL_TEMPLATE,
66+
$this->getBaseUri(),
67+
$this->getToken());
5068
}
5169
}

src/Clients/Client.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ abstract class Client implements GatewayInterface, RequestInterface
3737
'message',
3838
];
3939

40+
/**
41+
* @var string[]
42+
*/
43+
protected $required = [];
44+
45+
/**
46+
* @var array
47+
*/
48+
protected $allowedTypes = [];
49+
50+
/**
51+
* @var array
52+
*/
53+
protected $allowedValues = [];
54+
4055
/**
4156
* Client constructor.
4257
*/
@@ -47,8 +62,17 @@ public function __construct(array $options = [])
4762

4863
protected function configureOptionsResolver(OptionsResolver $resolver): OptionsResolver
4964
{
50-
return tap($resolver, function ($resolver) {
65+
return tap($resolver, function (OptionsResolver $resolver) {
5166
$resolver->setDefined($this->defined);
67+
$resolver->setRequired($this->required);
68+
69+
foreach ($this->allowedTypes as $option => $allowedType) {
70+
$resolver->setAllowedTypes($option, $allowedType);
71+
}
72+
73+
foreach ($this->allowedValues as $option => $allowedValue) {
74+
$resolver->setAllowedValues($option, $allowedValue);
75+
}
5276
});
5377
}
5478

src/Clients/FeiShuClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ public function getRequestParams(): array
6060
*/
6161
protected function getSign(string $secret, int $timestamp)
6262
{
63-
$stringToSign = sprintf("%s\n%s", $timestamp, $secret);
63+
$key = sprintf("%s\n%s", $timestamp, $secret);
6464

65-
$hash = hash_hmac('sha256', '', $stringToSign, true);
65+
$hash = hash_hmac('sha256', '', $key, true);
6666

6767
return base64_encode($hash);
6868
}

src/Clients/XiZhiClient.php

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

1111
namespace Guanguans\Notify\Clients;
1212

13-
use Symfony\Component\OptionsResolver\OptionsResolver;
14-
1513
class XiZhiClient extends Client
1614
{
1715
public const REQUEST_URL_TEMPLATE = [
@@ -35,14 +33,12 @@ class XiZhiClient extends Client
3533
'type' => 'single',
3634
];
3735

38-
protected function configureOptionsResolver(OptionsResolver $resolver): OptionsResolver
39-
{
40-
$resolver = parent::configureOptionsResolver($resolver);
41-
42-
return tap($resolver, function ($resolver) {
43-
$resolver->setAllowedValues('type', ['single', 'channel']);
44-
});
45-
}
36+
/**
37+
* @var \string[][]
38+
*/
39+
protected $allowedValues = [
40+
'type' => ['single', 'channel'],
41+
];
4642

4743
public function getType(): string
4844
{
@@ -61,6 +57,6 @@ public function setType(string $type)
6157

6258
public function getRequestUrl(): string
6359
{
64-
return sprintf(static::REQUEST_URL_TEMPLATE[$this->getOptions('type')], $this->getToken());
60+
return sprintf(static::REQUEST_URL_TEMPLATE[$this->getType()], $this->getToken());
6561
}
6662
}

0 commit comments

Comments
 (0)