diff --git a/config/dingtalk_robot.php b/config/dingtalk_robot.php index 79e1a2b..db5547f 100644 --- a/config/dingtalk_robot.php +++ b/config/dingtalk_robot.php @@ -7,6 +7,6 @@ 'access_token' => env('DINGTALK_ROBOT_ACCESS_TOKEN', ''), // 请求的超时时间 - 'timeout' => env('DINGTALK_ROBOT_TIMEOUT', 2.0) - ] -]; \ No newline at end of file + 'timeout' => env('DINGTALK_ROBOT_TIMEOUT', 2.0), + ], +]; diff --git a/src/DingtalkRobot.php b/src/DingtalkRobot.php index c4a2736..066250e 100644 --- a/src/DingtalkRobot.php +++ b/src/DingtalkRobot.php @@ -5,14 +5,13 @@ use Calchen\LaravelDingtalkRobot\Exception\Exception; use Calchen\LaravelDingtalkRobot\Exception\HttpException; use Calchen\LaravelDingtalkRobot\Exception\InvalidConfigurationException; -use GuzzleHttp\Client; use Calchen\LaravelDingtalkRobot\Message\Message; +use GuzzleHttp\Client; /** - * 钉钉群消息机器 API + * 钉钉群消息机器 API. * * Class DingtalkRobot - * @package Calchen\LaravelDingtalkRobot */ class DingtalkRobot { @@ -20,11 +19,11 @@ class DingtalkRobot /** * @var string */ - protected $accessToken = ""; + protected $accessToken = ''; /** * @var string */ - protected $robotUrl = "https://oapi.dingtalk.com/robot/send"; + protected $robotUrl = 'https://oapi.dingtalk.com/robot/send'; /** * 消息对象 @@ -42,12 +41,13 @@ public function __construct() } /** - * 指定机器人名称,默认为 default + * 指定机器人名称,默认为 default. * * @param string $name * - * @return $this * @throws \Exception + * + * @return $this */ public function robot($name = 'default'): self { @@ -68,18 +68,20 @@ public function robot($name = 'default'): self * * @param Message $message * - * @return $this * @throws \Exception + * + * @return $this */ public function setMessage(Message $message): self { $this->message = $message; $this->robot($message->getRobot()); + return $this; } /** - * 获取 message 对象的内容 + * 获取 message 对象的内容. * * @return array */ @@ -89,7 +91,7 @@ public function getMessage(): array } /** - * 获取附带 access_token 的 webhook Url + * 获取附带 access_token 的 webhook Url. * * @return string */ @@ -99,10 +101,11 @@ protected function getRobotUrl(): string } /** - * 发起请求,返回的内容与直接调用钉钉接口返回的内容一致 + * 发起请求,返回的内容与直接调用钉钉接口返回的内容一致. * - * @return bool|string * @throws Exception + * + * @return bool|string */ public function send(): string { @@ -118,15 +121,16 @@ public function send(): string $response = $client->post( $this->getRobotUrl(), [ - 'json' => $this->message->getMessage(), + 'json' => $this->message->getMessage(), 'headers' => [ 'Content-Type' => 'application/json', - ] + ], ] ); + return $response->getBody()->getContents(); } catch (Exception $e) { throw new HttpException($e->getMessage(), $e->getCode(), $e); } } -} \ No newline at end of file +} diff --git a/src/DingtalkRobotChannel.php b/src/DingtalkRobotChannel.php index 9157a86..e17f88a 100644 --- a/src/DingtalkRobotChannel.php +++ b/src/DingtalkRobotChannel.php @@ -6,10 +6,9 @@ use Illuminate\Notifications\Notification; /** - * 自定义的钉钉群机器人消息发送渠道 + * 自定义的钉钉群机器人消息发送渠道. * * Class DingtalkRobotChannel - * @package Calchen\LaravelDingtalkRobot */ class DingtalkRobotChannel { @@ -32,4 +31,4 @@ public function send($notifiable, Notification $notification) $ding = new DingtalkRobot(); $ding->setMessage($message)->send(); } -} \ No newline at end of file +} diff --git a/src/DingtalkRobotNoticeServiceProvider.php b/src/DingtalkRobotNoticeServiceProvider.php index b4f402f..6f1f869 100644 --- a/src/DingtalkRobotNoticeServiceProvider.php +++ b/src/DingtalkRobotNoticeServiceProvider.php @@ -7,7 +7,7 @@ use Laravel\Lumen\Application as LumenApplication; /** - * DingNotice SDK 的 ServiceProvider 只支持 Laravel + * DingNotice SDK 的 ServiceProvider 只支持 Laravel. */ class DingtalkRobotNoticeServiceProvider extends ServiceProvider { @@ -22,20 +22,19 @@ public function boot() } /** - * 处理配置项 + * 处理配置项. * * @return void */ protected function setupConfig(): void { - $source = realpath($raw = __DIR__ . '/../config/dingtalk_robot.php') ?: $raw; + $source = realpath($raw = __DIR__.'/../config/dingtalk_robot.php') ?: $raw; if ($this->app instanceof LaravelApplication && $this->app->runningInConsole()) { $this->publishes([ - $source => config_path('dingtalk_robot.php') + $source => config_path('dingtalk_robot.php'), ]); - } - elseif ($this->app instanceof LumenApplication) { + } elseif ($this->app instanceof LumenApplication) { $this->app->configure('dingtalk_robot'); } diff --git a/src/Exceptions/Exception.php b/src/Exceptions/Exception.php index 47686b7..ac806bd 100644 --- a/src/Exceptions/Exception.php +++ b/src/Exceptions/Exception.php @@ -3,12 +3,11 @@ namespace Calchen\LaravelDingtalkRobot\Exception; /** - * 异常基类 + * 异常基类. * * Class Exception - * @package Calchen\LaravelDingtalkRobot\Exception */ class Exception extends \Exception { // -} \ No newline at end of file +} diff --git a/src/Exceptions/HttpException.php b/src/Exceptions/HttpException.php index df4f6fe..317d26e 100644 --- a/src/Exceptions/HttpException.php +++ b/src/Exceptions/HttpException.php @@ -3,12 +3,11 @@ namespace Calchen\LaravelDingtalkRobot\Exception; /** - * Http 请求异常 + * Http 请求异常. * * Class HttpException - * @package Calchen\LaravelDingtalkRobot\Exception */ class HttpException extends Exception { // -} \ No newline at end of file +} diff --git a/src/Exceptions/InvalidArgumentException.php b/src/Exceptions/InvalidArgumentException.php index e2512fd..3e10181 100644 --- a/src/Exceptions/InvalidArgumentException.php +++ b/src/Exceptions/InvalidArgumentException.php @@ -3,12 +3,11 @@ namespace Calchen\LaravelDingtalkRobot\Exception; /** - * 入参异常 + * 入参异常. * * Class InvalidArgumentException - * @package Calchen\LaravelDingtalkRobot\Exception */ class InvalidArgumentException extends Exception { // -} \ No newline at end of file +} diff --git a/src/Exceptions/InvalidConfigurationException.php b/src/Exceptions/InvalidConfigurationException.php index 661d257..f8faa85 100644 --- a/src/Exceptions/InvalidConfigurationException.php +++ b/src/Exceptions/InvalidConfigurationException.php @@ -3,12 +3,11 @@ namespace Calchen\LaravelDingtalkRobot\Exception; /** - * 配置文件异常 + * 配置文件异常. * * Class InvalidConfigurationException - * @package Calchen\LaravelDingtalkRobot\Exception */ class InvalidConfigurationException extends Exception { // -} \ No newline at end of file +} diff --git a/src/Message/ActionCardMessage.php b/src/Message/ActionCardMessage.php index 14427ab..2b24e29 100644 --- a/src/Message/ActionCardMessage.php +++ b/src/Message/ActionCardMessage.php @@ -5,10 +5,9 @@ use Calchen\LaravelDingtalkRobot\Exception\InvalidConfigurationException; /** - * ActionCard类型,包含整体跳转和独立跳转 + * ActionCard类型,包含整体跳转和独立跳转. * * Class DingtalkActionCardMessage - * @package Calchen\LaravelDingtalkRobot */ class ActionCardMessage extends Message { @@ -32,7 +31,7 @@ public function __construct(string $title, string $text, int $hideAvatar = 0, in */ const HIDE_AVATAR_VALUES = [ 0, // 按钮竖直排列 - 1 // 按钮横向排列 + 1, // 按钮横向排列 ]; /** @@ -40,11 +39,11 @@ public function __construct(string $title, string $text, int $hideAvatar = 0, in */ const BTN_ORIENTATION_VALUES = [ 0, // 正常发消息者头像 - 1 // 隐藏发消息者头像 + 1, // 隐藏发消息者头像 ]; /** - * ActionCard 的整体跳转和独立跳转两种类型中 title text hideAvatar btnOrientation 都是共同拥有的 + * ActionCard 的整体跳转和独立跳转两种类型中 title text hideAvatar btnOrientation 都是共同拥有的. * * @param string $title 首屏会话透出的展示内容 * @param string $text markdown 格式的消息 @@ -56,25 +55,25 @@ public function __construct(string $title, string $text, int $hideAvatar = 0, in public function setMessage(string $title, string $text, int $hideAvatar = 0, int $btnOrientation = 0): void { if (array_search($hideAvatar, self::HIDE_AVATAR_VALUES) === false) { - throw new InvalidConfigurationException("hideAvatar value can only be 0 or 1"); + throw new InvalidConfigurationException('hideAvatar value can only be 0 or 1'); } if (array_search($btnOrientation, self::BTN_ORIENTATION_VALUES) === false) { - throw new InvalidConfigurationException("btnOrientation value can only be 0 or 1"); + throw new InvalidConfigurationException('btnOrientation value can only be 0 or 1'); } $this->message = [ - 'msgtype' => 'actionCard', + 'msgtype' => 'actionCard', 'actionCard' => [ - 'title' => $title, - 'text' => $text, - 'hideAvatar' => $hideAvatar, - 'btnOrientation' => $btnOrientation - ] + 'title' => $title, + 'text' => $text, + 'hideAvatar' => $hideAvatar, + 'btnOrientation' => $btnOrientation, + ], ]; } /** - * 设置整体跳转类型参数 + * 设置整体跳转类型参数. * * @param string $singleTitle 单个按钮的方案。(设置此项和singleURL后btns无效。) * @param string $singleUrl 点击singleTitle按钮触发的URL @@ -85,11 +84,12 @@ public function setSingle(string $singleTitle, string $singleUrl): self { $this->message['actionCard']['singleTitle'] = $singleTitle; $this->message['actionCard']['singleURL'] = $singleUrl; + return $this; } /** - * 设置独立跳转类型按钮参数 + * 设置独立跳转类型按钮参数. * * @param string $title 按钮方案 * @param string $actionUrl 点击按钮触发的URL @@ -99,9 +99,10 @@ public function setSingle(string $singleTitle, string $singleUrl): self public function addButton(string $title, string $actionUrl): self { $this->message['actionCard']['btns'][] = [ - 'title' => $title, - 'actionURL' => $actionUrl + 'title' => $title, + 'actionURL' => $actionUrl, ]; + return $this; } -} \ No newline at end of file +} diff --git a/src/Message/FeedCardMessage.php b/src/Message/FeedCardMessage.php index 94760dc..b6c63b5 100644 --- a/src/Message/FeedCardMessage.php +++ b/src/Message/FeedCardMessage.php @@ -10,24 +10,20 @@ class FeedCardMessage extends Message public function __construct() { $this->setMessage(); - } - /** - * - */ public function setMessage(): void { $this->message = [ 'feedCard' => [ - 'links' => [] + 'links' => [], ], - 'msgtype' => 'feedCard' + 'msgtype' => 'feedCard', ]; } /** - * 增加链接 + * 增加链接. * * @param string $title 单条信息文本 * @param string $messageUrl 点击单条信息到跳转链接 @@ -38,10 +34,11 @@ public function setMessage(): void public function addLink(string $title, string $messageUrl, string $picUrl): self { $this->message['feedCard']['links'][] = [ - 'title' => $title, + 'title' => $title, 'messageURL' => $messageUrl, - 'picURL' => $picUrl + 'picURL' => $picUrl, ]; + return $this; } -} \ No newline at end of file +} diff --git a/src/Message/LinkMessage.php b/src/Message/LinkMessage.php index 4affbd1..012ed49 100644 --- a/src/Message/LinkMessage.php +++ b/src/Message/LinkMessage.php @@ -3,10 +3,9 @@ namespace Calchen\LaravelDingtalkRobot\Message; /** - * link类型 + * link类型. * * Class DingtalkLinkMessage - * @package Calchen\LaravelDingtalkRobot */ class LinkMessage extends Message { @@ -33,12 +32,12 @@ public function setMessage(string $title, string $text, string $messageUrl, stri { $this->message = [ 'msgtype' => 'link', - 'link' => [ - 'title' => $title, - 'text' => $text, - 'picUrl' => $picUrl, - 'messageUrl' => $messageUrl - ] + 'link' => [ + 'title' => $title, + 'text' => $text, + 'picUrl' => $picUrl, + 'messageUrl' => $messageUrl, + ], ]; } -} \ No newline at end of file +} diff --git a/src/Message/MarkdownMessage.php b/src/Message/MarkdownMessage.php index 4f9ec2a..c1e637a 100644 --- a/src/Message/MarkdownMessage.php +++ b/src/Message/MarkdownMessage.php @@ -5,10 +5,9 @@ use Calchen\LaravelDingtalkRobot\Exception\InvalidArgumentException; /** - * markdown类型 + * markdown类型. * * Class DingtalkMarkdownMessage - * @package Calchen\LaravelDingtalkRobot */ class MarkdownMessage extends Message { @@ -30,21 +29,22 @@ public function __construct(string $title, string $text) public function setMessage(string $title, string $text): void { $this->message = [ - 'msgtype' => 'markdown', + 'msgtype' => 'markdown', 'markdown' => [ 'title' => $title, - 'text' => $text - ] + 'text' => $text, + ], ]; } /** - * 通过手机号码指定“被@人列表” + * 通过手机号码指定“被@人列表”. * * @param string|array $mobiles 被@人的手机号(在text内容里要有@手机号) * - * @return MarkdownMessage * @throws InvalidArgumentException + * + * @return MarkdownMessage */ public function at($mobiles): self { @@ -56,8 +56,8 @@ public function at($mobiles): self $this->at = [ 'at' => [ - 'atMobiles' => $mobiles - ] + 'atMobiles' => $mobiles, + ], ]; return $this; @@ -72,10 +72,10 @@ public function atAll(): self { $this->at = [ 'at' => [ - 'isAtAll' => true - ] + 'isAtAll' => true, + ], ]; return $this; } -} \ No newline at end of file +} diff --git a/src/Message/Message.php b/src/Message/Message.php index 26b6716..644d2d3 100644 --- a/src/Message/Message.php +++ b/src/Message/Message.php @@ -3,10 +3,9 @@ namespace Calchen\LaravelDingtalkRobot\Message; /** - * 机器人群消息的基类 + * 机器人群消息的基类. * * Class Message - * @package Calchen\LaravelDingtalkRobot\Message */ abstract class Message { @@ -19,9 +18,8 @@ abstract class Message // 设置机器人名称,默认为 default protected $robot = 'default'; - /** - * 获取消息请求的请求体内容 + * 获取消息请求的请求体内容. * * @return array */ @@ -31,7 +29,7 @@ public function getMessage(): array } /** - * 设置接受消息的机器人名称 + * 设置接受消息的机器人名称. * * @param $robot * @@ -40,11 +38,12 @@ public function getMessage(): array public function setRobot($robot): self { $this->robot = $robot; + return $this; } /** - * 获取机器人名称 + * 获取机器人名称. * * @return string */ @@ -52,4 +51,4 @@ public function getRobot(): string { return $this->robot; } -} \ No newline at end of file +} diff --git a/src/Message/TextMessage.php b/src/Message/TextMessage.php index 0aa7efb..c06aad9 100644 --- a/src/Message/TextMessage.php +++ b/src/Message/TextMessage.php @@ -5,10 +5,9 @@ use Calchen\LaravelDingtalkRobot\Exception\InvalidArgumentException; /** - * 文本类型消息 + * 文本类型消息. * * Class DingtalkTextMessage - * @package Calchen\LaravelDingtalkRobot */ class TextMessage extends Message { @@ -29,19 +28,20 @@ public function setMessage(string $content): void { $this->message = [ 'msgtype' => 'text', - 'text' => [ - 'content' => $content - ] + 'text' => [ + 'content' => $content, + ], ]; } /** - * 通过手机号码指定“被@人列表” + * 通过手机号码指定“被@人列表”. * * @param string|array $mobiles 被@人的手机号(在text内容里要有@手机号) * - * @return TextMessage * @throws InvalidArgumentException + * + * @return TextMessage */ public function at($mobiles): self { @@ -53,8 +53,8 @@ public function at($mobiles): self $this->at = [ 'at' => [ - 'atMobiles' => $mobiles - ] + 'atMobiles' => $mobiles, + ], ]; return $this; @@ -69,10 +69,10 @@ public function atAll(): self { $this->at = [ 'at' => [ - 'isAtAll' => true - ] + 'isAtAll' => true, + ], ]; return $this; } -} \ No newline at end of file +} diff --git a/src/Robot.php b/src/Robot.php index fb33802..ec62082 100644 --- a/src/Robot.php +++ b/src/Robot.php @@ -6,10 +6,9 @@ /** * 该类是为了方便以机器人的身份发送群消息,无其他作用 - * 对于其他使用了 Notifiable Trait 的类都可以用来发送消息 + * 对于其他使用了 Notifiable Trait 的类都可以用来发送消息. * * Class Robot - * @package Calchen\LaravelDingtalkRobot */ class Robot { @@ -38,4 +37,4 @@ public function setName(string $name): void { $this->name = $name; } -} \ No newline at end of file +} diff --git a/src/helpers.php b/src/helpers.php index 906be9b..fcdca55 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -4,10 +4,11 @@ if (!function_exists('dingtalk_robot')) { /** - * 获取钉钉群机器人接口 + * 获取钉钉群机器人接口. * - * @return DingtalkRobot * @throws Exception + * + * @return DingtalkRobot */ function dingtalk_robot() { @@ -22,4 +23,4 @@ function dingtalk_robot() return $dingTalk->robot($arguments[0]); } -} \ No newline at end of file +}