Guard.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /*
  3. * This file is part of the overtrue/wechat.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. /**
  11. * Guard.php.
  12. *
  13. * Part of Overtrue\WeChat.
  14. *
  15. * For the full copyright and license information, please view the LICENSE
  16. * file that was distributed with this source code.
  17. *
  18. * @author mingyoung <mingyoungcheung@gmail.com>
  19. * @author lixiao <leonlx126@gmail.com>
  20. * @copyright 2016
  21. *
  22. * @see https://github.com/overtrue
  23. * @see http://overtrue.me
  24. */
  25. namespace EasyWeChat\OpenPlatform;
  26. use EasyWeChat\Server\Guard as ServerGuard;
  27. use EasyWeChat\Support\Collection;
  28. use EasyWeChat\Support\Log;
  29. use Symfony\Component\HttpFoundation\Response;
  30. class Guard extends ServerGuard
  31. {
  32. const EVENT_AUTHORIZED = 'authorized';
  33. const EVENT_UNAUTHORIZED = 'unauthorized';
  34. const EVENT_UPDATE_AUTHORIZED = 'updateauthorized';
  35. const EVENT_COMPONENT_VERIFY_TICKET = 'component_verify_ticket';
  36. /**
  37. * Event handlers.
  38. *
  39. * @var \EasyWeChat\Support\Collection
  40. */
  41. protected $handlers;
  42. /**
  43. * Set handlers.
  44. *
  45. * @param array $handlers
  46. */
  47. public function setHandlers(array $handlers)
  48. {
  49. $this->handlers = new Collection($handlers);
  50. return $this;
  51. }
  52. /**
  53. * Get handlers.
  54. *
  55. * @return \EasyWeChat\Support\Collection
  56. */
  57. public function getHandlers()
  58. {
  59. return $this->handlers;
  60. }
  61. /**
  62. * Get handler.
  63. *
  64. * @param string $type
  65. *
  66. * @return \EasyWeChat\OpenPlatform\EventHandlers\EventHandler|null
  67. */
  68. public function getHandler($type)
  69. {
  70. return $this->handlers->get($type);
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function serve()
  76. {
  77. $message = $this->getMessage();
  78. // Handle Messages.
  79. if (isset($message['MsgType'])) {
  80. return parent::serve();
  81. }
  82. Log::debug('OpenPlatform Request received:', [
  83. 'Method' => $this->request->getMethod(),
  84. 'URI' => $this->request->getRequestUri(),
  85. 'Query' => $this->request->getQueryString(),
  86. 'Protocal' => $this->request->server->get('SERVER_PROTOCOL'),
  87. 'Content' => $this->request->getContent(),
  88. ]);
  89. // If sees the `auth_code` query parameter in the url, that is,
  90. // authorization is successful and it calls back, meanwhile, an
  91. // `authorized` event, which also includes the auth code, is sent
  92. // from WeChat, and that event will be handled.
  93. if ($this->request->get('auth_code')) {
  94. return new Response(self::SUCCESS_EMPTY_RESPONSE);
  95. }
  96. $this->handleEventMessage($message);
  97. return new Response(self::SUCCESS_EMPTY_RESPONSE);
  98. }
  99. /**
  100. * Handle event message.
  101. *
  102. * @param array $message
  103. */
  104. protected function handleEventMessage(array $message)
  105. {
  106. Log::debug('OpenPlatform Event Message detail:', $message);
  107. $message = new Collection($message);
  108. $infoType = $message->get('InfoType');
  109. if ($handler = $this->getHandler($infoType)) {
  110. $handler->handle($message);
  111. } else {
  112. Log::notice("No existing handler for '{$infoType}'.");
  113. }
  114. if ($messageHandler = $this->getMessageHandler()) {
  115. call_user_func_array($messageHandler, [$message]);
  116. }
  117. }
  118. }