Skip to content

Commit a088022

Browse files
committed
refactor(message): unify ZohoCliq message classes and enhance message payload handling
- Remove redundant toPayload() overrides in BotMessage, ChannelMessage, ChatMessage, and UserMessage classes - Add new setter methods for channelUniqueName, botUniqueName, chatId, and emailId in respective message classes - Make ZohoCliq Messages\Message an abstract class with common options, validation, and helper methods like addSlide() and addButton() - Simplify ZohoCliqWebHook Messages\Message by extending ZohoCliq Messages\Message and removing duplicated configuration - Update Authenticator to use imported Client class and add explicit return type in retry method closure - Comment out addMethodsOfListTypeOption call in HasOptionsRector and adjust method visibility for better maintainability - Remove psr/simple-cache from composer dependency analyser configuration to avoid shadow dependency warnings Signed-off-by: guanguans <ityaozm@gmail.com>
1 parent 55878f5 commit a088022

File tree

9 files changed

+127
-134
lines changed

9 files changed

+127
-134
lines changed

composer-dependency-analyser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
'guzzlehttp/psr7',
4747
'psr/http-factory',
4848
'psr/http-message',
49-
'psr/simple-cache',
49+
// 'psr/simple-cache',
5050
],
5151
[ErrorType::SHADOW_DEPENDENCY]
5252
)

src/Foundation/Rectors/HasOptionsRector.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -129,31 +129,20 @@ public function refactor(Node $node): ?Node
129129
return null;
130130
}
131131

132-
$this->addMethodsOfListTypeOption($node);
132+
// $this->addMethodsOfListTypeOption($node);
133133
$this->sortProperties($node);
134134
$this->addPhpDocTagNodesOfMethod($node);
135135

136136
return $node;
137137
}
138138

139-
private function isSubclassesOf(string $object): bool
140-
{
141-
foreach ($this->classes as $class) {
142-
if (is_subclass_of($object, $class)) {
143-
return true;
144-
}
145-
}
146-
147-
return false;
148-
}
149-
150139
/**
151140
* @noinspection PhpPossiblePolymorphicInvocationInspection
152141
* @noinspection D
153142
*
154143
* @throws \ReflectionException
155144
*/
156-
private function addMethodsOfListTypeOption(Class_ $class): void
145+
public function addMethodsOfListTypeOption(Class_ $class): void
157146
{
158147
collect($this->allowedTypesFor($this->getName($class)))
159148
->filter(
@@ -231,6 +220,17 @@ public function add%s(array $%s): self
231220
});
232221
}
233222

223+
private function isSubclassesOf(string $object): bool
224+
{
225+
foreach ($this->classes as $class) {
226+
if (is_subclass_of($object, $class)) {
227+
return true;
228+
}
229+
}
230+
231+
return false;
232+
}
233+
234234
private function sortProperties(Class_ $class): void
235235
{
236236
usort($class->stmts, static function (Stmt $a, Stmt $b): int {

src/ZohoCliq/Authenticator.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
namespace Guanguans\Notify\ZohoCliq;
1515

1616
use Guanguans\Notify\Foundation\Authenticators\NullAuthenticator;
17+
use Guanguans\Notify\Foundation\Client;
1718
use Guanguans\Notify\Foundation\Concerns\AsFormParams;
1819
use Guanguans\Notify\Foundation\Exceptions\RequestException;
1920
use Guanguans\Notify\Foundation\Message;
2021
use GuzzleHttp\Middleware;
2122
use Psr\Http\Message\RequestInterface;
2223
use Psr\Http\Message\ResponseInterface;
23-
use Psr\SimpleCache\CacheInterface;
2424

2525
/**
2626
* ```
@@ -47,7 +47,6 @@ class Authenticator extends NullAuthenticator
4747
public function __construct(
4848
private string $clientId,
4949
private string $clientSecret,
50-
private ?CacheInterface $cache = null,
5150
) {}
5251

5352
public function __invoke(callable $handler): callable
@@ -59,7 +58,7 @@ public function __invoke(callable $handler): callable
5958

6059
public function retry(callable $handler): callable
6160
{
62-
return Middleware::retry(function (int $retries, RequestInterface &$request, ?ResponseInterface $response = null) {
61+
return Middleware::retry(function (int $retries, RequestInterface &$request, ?ResponseInterface $response = null): bool {
6362
if (1 <= $retries) {
6463
return false;
6564
}
@@ -105,7 +104,7 @@ private function getAccessToken(): string
105104
*/
106105
private function fetchAccessToken(): string
107106
{
108-
$response = (new \Guanguans\Notify\Foundation\Client)
107+
$response = (new Client)
109108
->send(
110109
new class([
111110
'client_id' => $this->clientId,

src/ZohoCliq/Messages/BotMessage.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313

1414
namespace Guanguans\Notify\ZohoCliq\Messages;
1515

16-
use Guanguans\Notify\Foundation\Support\Arr;
17-
1816
/**
1917
* @method self bot(array $bot)
18+
* @method self botUniqueName(mixed $botUniqueName)
2019
* @method self buttons(array $buttons)
2120
* @method self card(array $card)
2221
* @method self slides(array $slides)
@@ -40,9 +39,4 @@ public function toHttpUri(): string
4039
{
4140
return "api/v2/bots/{$this->getOption('bot_unique_name')}/message";
4241
}
43-
44-
protected function toPayload(): array
45-
{
46-
return Arr::except(parent::toPayload(), ['bot_unique_name']);
47-
}
4842
}

src/ZohoCliq/Messages/ChannelMessage.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@
1313

1414
namespace Guanguans\Notify\ZohoCliq\Messages;
1515

16-
use Guanguans\Notify\Foundation\Support\Arr;
17-
1816
/**
1917
* @method self bot(array $bot)
2018
* @method self buttons(array $buttons)
2119
* @method self card(array $card)
20+
* @method self channelUniqueName(mixed $channelUniqueName)
2221
* @method self slides(array $slides)
2322
* @method self styles(array $styles)
2423
* @method self text(mixed $text)
@@ -40,9 +39,4 @@ public function toHttpUri(): string
4039
{
4140
return "api/v2/channelsbyname/{$this->getOption('channel_unique_name')}/message";
4241
}
43-
44-
protected function toPayload(): array
45-
{
46-
return Arr::except(parent::toPayload(), ['channel_unique_name']);
47-
}
4842
}

src/ZohoCliq/Messages/ChatMessage.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@
1313

1414
namespace Guanguans\Notify\ZohoCliq\Messages;
1515

16-
use Guanguans\Notify\Foundation\Support\Arr;
17-
1816
/**
1917
* @method self bot(array $bot)
2018
* @method self buttons(array $buttons)
2119
* @method self card(array $card)
20+
* @method self chatId(mixed $chatId)
2221
* @method self slides(array $slides)
2322
* @method self styles(array $styles)
2423
* @method self text(mixed $text)
@@ -40,9 +39,4 @@ public function toHttpUri(): string
4039
{
4140
return "api/v2/chats/{$this->getOption('chat_id')}/message";
4241
}
43-
44-
protected function toPayload(): array
45-
{
46-
return Arr::except(parent::toPayload(), ['chat_id']);
47-
}
4842
}

src/ZohoCliq/Messages/Message.php

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
namespace Guanguans\Notify\ZohoCliq\Messages;
1515

16+
use Guanguans\Notify\Foundation\Support\Arr;
17+
use Symfony\Component\OptionsResolver\OptionsResolver;
18+
1619
/**
1720
* @method self bot(array $bot)
1821
* @method self buttons(array $buttons)
@@ -21,4 +24,105 @@
2124
* @method self styles(array $styles)
2225
* @method self text(mixed $text)
2326
*/
24-
class Message extends \Guanguans\Notify\ZohoCliqWebHook\Messages\Message {}
27+
abstract class Message extends \Guanguans\Notify\Foundation\Message
28+
{
29+
protected array $required = [
30+
// 'channel_unique_name',
31+
// 'bot_unique_name',
32+
// 'chat_id',
33+
// 'email_id',
34+
// 'text',
35+
];
36+
protected array $defined = [
37+
'text',
38+
'bot',
39+
'card',
40+
'styles',
41+
'slides',
42+
'buttons',
43+
];
44+
protected array $allowedTypes = [
45+
'bot' => 'array',
46+
'card' => 'array',
47+
'styles' => 'array',
48+
'slides' => 'array[]',
49+
'buttons' => 'array[]',
50+
];
51+
protected array $options = [
52+
'slides' => [],
53+
'buttons' => [],
54+
];
55+
56+
final public function addSlide(array $slide): self
57+
{
58+
$this->options['slides'][] = $slide;
59+
60+
return $this;
61+
}
62+
63+
final public function addButton(array $button): self
64+
{
65+
$this->options['buttons'][] = $button;
66+
67+
return $this;
68+
}
69+
70+
protected function toPayload(): array
71+
{
72+
return Arr::except(parent::toPayload(), [
73+
'channel_unique_name',
74+
'bot_unique_name',
75+
'chat_id',
76+
'email_id',
77+
]);
78+
}
79+
80+
protected function configureOptionsResolver(OptionsResolver $optionsResolver): void
81+
{
82+
$optionsResolver
83+
->setDefault('bot', static function (OptionsResolver $optionsResolver): void {
84+
$optionsResolver
85+
->setDefined([
86+
'name',
87+
'image',
88+
]);
89+
})
90+
->setDefault('card', static function (OptionsResolver $optionsResolver): void {
91+
$optionsResolver
92+
->setDefined([
93+
'title',
94+
'theme',
95+
'thumbnail',
96+
'icon',
97+
'preview',
98+
]);
99+
})
100+
->setDefault('styles', static function (OptionsResolver $optionsResolver): void {
101+
$optionsResolver
102+
->setDefined([
103+
'highlight',
104+
])
105+
->setAllowedTypes('highlight', 'bool');
106+
})
107+
->setDefault('slides', static function (OptionsResolver $optionsResolver): void {
108+
$optionsResolver
109+
->setPrototype(true)
110+
->setDefined([
111+
'type',
112+
'title',
113+
'data',
114+
]);
115+
})
116+
->setDefault('buttons', static function (OptionsResolver $optionsResolver): void {
117+
$optionsResolver
118+
->setPrototype(true)
119+
->setDefined([
120+
'label',
121+
'hint',
122+
'key',
123+
'type',
124+
'action',
125+
]);
126+
});
127+
}
128+
}

src/ZohoCliq/Messages/UserMessage.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@
1313

1414
namespace Guanguans\Notify\ZohoCliq\Messages;
1515

16-
use Guanguans\Notify\Foundation\Support\Arr;
17-
1816
/**
1917
* @method self bot(array $bot)
2018
* @method self buttons(array $buttons)
2119
* @method self card(array $card)
20+
* @method self emailId(mixed $emailId)
2221
* @method self slides(array $slides)
2322
* @method self styles(array $styles)
2423
* @method self text(mixed $text)
@@ -40,9 +39,4 @@ public function toHttpUri(): string
4039
{
4140
return "api/v2/buddies/{$this->getOption('email_id')}/message";
4241
}
43-
44-
protected function toPayload(): array
45-
{
46-
return Arr::except(parent::toPayload(), ['email_id']);
47-
}
4842
}

0 commit comments

Comments
 (0)